why is this vbscript not fetching any value? - dom

i am trying to write a vbscript that extracts the value from src attribute of an <img> tag that has a class attribute as this cs-poster-big
here is the code i have tried so far,
'Initializing object with Internet Explorer Application
set IE = WScript.CreateObject("InternetExplorer.Application", "IE_")
'setting properties of Internet Explorer to the newly create object
with IE
.Visible = 0
.navigate "http://www.roku.com/channels/#!details/12" 'INSERT WEBPAGE HERE
end with
'waiting for IE to load the page
'tried using IE.busy or IE.readyState <> 4 also
while IE.busy
wScript.sleep 500
wend
wScript.sleep 500
'getting all image tags from the webpage
Set imgTags = IE.document.getElementsByTagName("IMG")
'iterating through the image tags to find the one with the class name specified
For Each imgTag In imgTags
'tried imgTag.className also
If imgTag.getAttribute("class") = "cs-poster-big" Then MsgBox "src is " & imgTag.src
next
IE.quit
set IE= Nothing
MsgBox "End of script"
and it is not displaying any value but you can view the source of the page here and you can see it has an <img> tag with class cs-poster-big
i don't understand why it is not displaying in my script

Do While IE.Busy Or IE.ReadyState <> 4
WScript.Sleep 500
Loop
Wait until page is completely loaded.
EDIT - While this works in IE10, IE8 fails to locate the image. Not tested in other versions.
In this case, try to change your url to
http://www.roku.com/channels?_escaped_fragment_=details/12/netflix#!details/12/netflix
to avoid problems with the dynamic generate content.
Also, in IE8, code needs to be changed to get class name of the image. It should be
Do While IE.Busy Or IE.ReadyState <> 4
WScript.Sleep 100
Loop
Set imgTags = IE.document.getElementsByTagName("IMG")
For Each imgTag In imgTags
imgClass = imgtag.getAttribute("class")
If IsNull( imgClass ) Then
imgClass = imgTag.className
End If
If imgclass = "cs-poster-big" Then
MsgBox "src is " & imgTag.src
End If
Next
But not a solution, just a workaround.

Related

Add a new line in message body using the following vbscript

I have created a txt file to support the message body of the vbscript but it only reads the last line of the messagebody.txt
WScript.Sleep 100
Set WshShell=WScript.CreateObject("WScript.Shell")
Set objShell=WScript.CreateObject("WScript.Shell")
set objOutlook=CreateObject("Outlook.Application")
Set objMail=CreateObject("CDO.Message")
Set objMail=objOutlook.CreateItem(0)
strDesktop = WshShell.SpecialFolders("Desktop")
Set objFileToReadTo = CreateObject("Scripting.FileSystemObject").OpenTextFile(strDesktop + "\\send email with attachment\List_To.txt",1)
Set objFileToReadCC = CreateObject("Scripting.FileSystemObject").OpenTextFile(strDesktop + "\\send email with attachment\List_CC.txt",1)
Set objFileToReadSubject = CreateObject("Scripting.FileSystemObject").OpenTextFile(strDesktop + "\\send email with attachment\List_Subject.txt",1)
Set objFileToReadBody = CreateObject("Scripting.FileSystemObject").OpenTextFile(strDesktop + "\\send email with attachment\Email Body.txt",1)
Set objFileToReadAttachments = CreateObject("Scripting.FileSystemObject").OpenTextFile(strDesktop + "\\send email with attachment\List_Attachments_withFileExtension.txt",1)
Dim strLineTo
Dim strLineCC
Dim strLineSubject
Dim strLineBody
Dim strLineAttachments
objMail.Display
WScript.Sleep 10
do while not objFileToReadTo.AtEndOfStream
strLineTo = objFileToReadTo.ReadLine()
objMail.To=strLineTo
loop
objFileToReadTo.Close
WScript.Sleep 10
do while not objFileToReadCC.AtEndOfStream
strLineCC = objFileToReadCC.ReadLine()
objMail.cc = strLineCC
loop
objFileToReadCC.Close
'41
WScript.Sleep 10
do while not objFileToReadSubject.AtEndOfStream
strLineSubject = objFileToReadSubject.ReadLine()
objMail.Subject = strLineSubject
loop
objFileToReadSubject.Close
'48
WScript.Sleep 10
do while not objFileToReadBody.AtEndOfStream
strLineBody = objFileToReadBody.ReadLine()
objMail.Body = strLineBody & vbCRLF
loop
objFileToReadBody.Close
'55
WScript.Sleep 10
do while not objFileToReadAttachments.AtEndOfStream
strLineAttachments = objFileToReadAttachments.ReadLine()
objMail.Attachments.Add(strLineAttachments)
loop
objFileToReadAttachments.Close
'62
'objShell.Sendkeys "%s"
WScript.Sleep 40
'objShell.SendKeys "{TAB}"
'objShell.SendKeys "{UP}"
'objShell.SendKeys "{Enter}"
'set MyEmail=nothing
'objOutlook.Quit
'Set objMail = Nothing
'Set objOutlook = Nothing
and here is my messagebody.txt
Hi,
Testing vbscript
Regards,
abcd
It only reads the last ABCD and displays the same on the oulook window.
How do I make the scipt understand multiple lines?
I really don't know why you have used different text files for storing ToList, CCList, body etc. but if you are sure about using this approach, I won't change it.
I am just pointing out why you are not getting the full text in the email body. Replace the following code:
do while not objFileToReadBody.AtEndOfStream
strLineBody = objFileToReadBody.ReadLine() 'Here you are just overwriting the value contained in strLineBody in each loop iteration. Hence, in the end, only last line is left in this variable
objMail.Body = strLineBody & vbCRLF
loop
WITH
objMail.Body = objFileToReadBody.readAll
In the loop you replace the Body with each line you read, when you should be appending to it. Switch this line;
objMail.Body = strLineBody & vbCRLF
to be;
objMail.Body = objMail.Body & strLineBody & vbNewLine
If you forgo the loop and use ReadAll (as #Gurman has suggested), bear in mind that while this will be fine for minimal text, larger text files will make the process less efficient then looping through each line as you have started to do already.

VBScript Help clicking facebook menu item

Need help clicking a "friends" name in a drop down on Facebook via vbs. Full code below. I included pictures and referenced the images in the code to show what each part of the code does and the part I'm having trouble with. I've spent a full day and deep into the night on this one problem and from what I can tell, it SHOULD be working...but it's not... I disabled the "submit" feature of the script to prevent accidental submitting of the shared link while testing. Also, I used a series of wscript.sleep lines as opposed to ie.busy or readystate lines. I plan to fix this up later...
You'll need to replace "friendsName1" with an actual name from your friend list to test. You can add multiple separated by commas if you want it to iterate through multiple, but for testing, one is quickest:
DistNames = Array("friendsName1")
Any help would be awesome. Very stuck at the moment...
Image1|Image2|Image3|Image4|Image5
This is how it looks after you've clicked your friends name and it's been successfully added. I'm trying to achieve this with code...and failing.
Image6
Set WshShell = WScript.CreateObject("WScript.Shell")
Set fso = CreateObject("Scripting.FileSystemObject")
set IE = createobject("internetexplorer.application")
IE.Visible = True
on error resume next
'===================================================================
'Navigates to login page
ie.navigate "https://www.facebook.com/"
wscript.sleep 2000
'===================================================================
'Logs into FB if you are not already
if not ie.document.all.item("email") is nothing then
ie.document.all.item("email").value = "username"
wscript.sleep 500
ie.document.all.item("pass").value = "password"
wscript.sleep 500
ie.document.all.item("loginbutton").click
wscript.sleep 2000
else
end if
DistNames = Array("friendsName1")
For each item in DistNames
ie.navigate "https://www.facebook.com/"
'===================================================================
wscript.sleep 4000
'===================================================================
'IMAGE1
'Clicks "share" to open menu
for each share in ie.document.getElementsbytagname("a")
if share.title = "Send this to friends or post it on your timeline." then
share.click
exit for
end if
next
'===================================================================
wscript.sleep 2000
'===================================================================
'IMAGE2
'Clicks "Share..."
for each share1 in ie.document.getElementsbytagname("a")
if instr(share1.getAttribute("ajaxify"),"story_container") > 0 then
share1.click
exit for
end if
next
'===================================================================
wscript.sleep 5000
'===================================================================
'IMAGE3
'Opens menu to select who you want it to go to
for each share2 in ie.document.getElementsbytagname("span")
if share2.innertext = "On your own Timeline" then
share2.click
exit for
end if
next
'===================================================================
wscript.sleep 1000
'===================================================================
'IMAGE4
'Selects "On a friend's Timeline"
for each share3 in ie.document.getElementsbytagname("span")
if share3.innertext = "On a friend's Timeline" then
share3.click
exit for
end if
next
'===================================================================
wscript.sleep 1000
'===================================================================
'IMAGE5 (Displays friend selection menu - this part works ok)
'Inserts name from array and opens list to click in the next step
for each share4 in ie.document.getElementsbytagname("input")
if share4.getAttribute("class") = "inputtext textInput" and share4.type = "text" then
share4.value = item
wscript.sleep 100
share4.blur
wscript.sleep 100
share4.click
exit for
end if
next
'===================================================================
wscript.sleep 2000
'===================================================================
'IMAGE5 cont... (Cannot click the name!)
'Clicks DistList name (not working!!!!)
for each share5 in ie.document.getElementsbytagname("span")
if share5.getAttribute("class") = "text" and share5.innertext = item then
share5.click
exit for
end if
next
'===================================================================
wscript.sleep 2000
'===================================================================
'Post body aka "Say something about this..."
for each share6 in ie.document.getElementsbytagname("textarea")
if share6.name = "message_text" then
share6.value = "Check out this post, " & item & "..."
exit for
end if
next
'===================================================================
wscript.sleep 3000
'===================================================================
'Clicks "Share Link"
'for each share6 in ie.document.getElementsbytagname("button")
'if share6.type = "submit" then
'share6.click
'exit for
'end if
'next
next
'===================================================================
wscript.sleep 3000
'===================================================================
wscript.sleep 500
wscript.echo "done"
wscript.quit

VBScript to detect Facebook IE window, press Like and close the window

I tryed to make a script to detect the IE window that is opened on Facebook and hit the Like button.
Let's say I have 10 IE opened. Only one of them is on a page on Facebook.
I want my script to detect that IE window, click this button:
IE.Document.getElementById("pagesHeaderLikeButton")
(The above button is the Like button)
and close that IE window.
I tryed to get that IE window by:
For Each wnd In CreateObject("Shell.Application").Windows
If InStr(1, wnd.FullName, "iexplore.exe", vbTextCompare) > 0 Then
Set IE = wnd
Exit For
End If
Next
But this will only set my VBscript to the first opened IE and it will not find the Facebook window.
I tryed this:
Dim objInstances, item
Set objInstances = CreateObject("Shell.Application").windows
For Each item In objInstances
If Item.Name Like "*Internet*" And Item.document.URL Like "*facebook.com*" Then
IE.Document.getElementById("pagesHeaderLikeButton").Click
End If
Next
But I get "Sub or function not defined"
Next code snippet could help:
Set shApp = CreateObject( "shell.application")
With shApp
For Each wnd In .Windows
If InStr(1, wnd.FullName, "iexplore.exe", vbTextCompare) > 0 Then
If InStr(1, wnd.document.URL, "facebook.com", vbTextCompare) > 0 Then
Wscript.Echo "THIS:"
End If
Wscript.Echo Left( wnd.document.URL, 70)
End If
Next
End With
Output example (with more facebook.com matches):
==>cscript D:\VB_scripts\SO\30717779a.vbs
http://www.msn.com/?ocid=iehp
THIS:
https://www.facebook.com/login.php?api_key=127760087237610&skip_api_lo
THIS:
https://www.facebook.com/literarnifestival1
THIS:
https://cs-cz.facebook.com/mgvsetin
http://www.bing.com/search?q=Xmaster+Official&qs=n&form=QBRE&pq=xmaste
http://www.bing.com/search?q=%22Xmaster+Official%22&qs=n&form=QBRE&pq=
==>
The following code will search for open IE windows that have "facebook.com" in its URL and save them in a collection:
Dim getIE As New Collection
For Each Item In CreateObject("Shell.Application").Windows
If Item.Name Like "*Internet*" And Item.document.URL Like "*facebook.com*" Then
getIE.Add Item
End If
Next
Then you can loop through the collection and do what you want with the IE items:
For Each itemIE In getIE
itemIE.Document.getElementById("pagesHeaderLikeButton").Click ' For example
Next itemIE
Hope this does what you wanted! ;)
For Each wnd In CreateObject("Shell.Application").Windows
If InStr(wnd.Name,"Internet") Then
if InStr(wnd.Document.URL,"facebook.com") Then
Set IE2 = wnd
Exit For
End If
End If
Next
So to press the button will be like this:
Set Butlike = IE2.Document.getElementsByTagName("button")
For Each btn In Butlike
If btn.type = "submit" Then btn.Click()
Next

Using VBScript, how do I update Microsoft Outlook Inbox folder before accessing it in VBS?

The following is a VBScript (VBS) that I use the check for and process certain Outlook emails and attachments. The script finds the emails via their email address and subject. It then saves the attachment in a folder and moves the email to a folder within Outlook. (Most of this code was adapted from a stackoverflow.com post, but I have since forgotten which one.)
My issue: Sometimes this script has to be run before the user has opened Outlook for the day; therefore, none of the Outlook folders have been updated and the script can't find emails that have been sent to the user since the user shut Outlook down last.
My question: How do I update the Outlook inbox then proceed with the rest of the script ensuring that the Inbox is (or all Outlook folders are) completely updated? I'm not sure if VBS will wait for the folder update to happen, but if it won't, I, of course, need it to. I don't know how to update the inbox or wait for it to update if waiting is applicable.
Other tips on how to make the script better are welcome.
My VBScript:
Const olFolderInbox = 6
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objOutlook = CreateObject("Outlook.Application")
Set objNamespace = objOutlook.GetNamespace("MAPI")
Set objFolder = objNamespace.GetDefaultFolder(olFolderInbox)
Call SaveAndMoveAttachment("'subject 1'", objFolder)
Call SaveAndMoveAttachment("'subject 2'", objFolder)
Call SaveAndMoveAttachment("'subject 3'", objFolder)
Set objFSO = Nothing
Set objOutlook = Nothing
Set objNamespace = Nothing
WScript.Quit
Sub SaveAndMoveAttachment(sSubject, objFolder)
Set colItems = objFolder.Items
Set colFilteredItems = colItems.Restrict("[Subject] = " & sSubject)
If colFilteredItems.count = 0 then
Msgbox "An email with subject " & sSubject & " in it was not found in your Outlook Inbox"
WScript.Quit
end if
For Each objMessage In colFilteredItems
Set colAttachments = objMessage.Attachments
intCount = colAttachments.Count
If intCount <> 0 and objMessage.Sender.Address = "support#somesite.com" Then
For i = 1 To intCount
strFileName = "Z:\somepath\" & objMessage.Attachments.Item(i).FileName
objMessage.Attachments.Item(i).SaveAsFile strFileName
'move the message to somefolder folder
Set objFoldersomefolder = objNamespace.GetDefaultFolder(olFolderInbox).Folders("somefolder")
objMessage.Move objFoldersomefolder
Next
End If
Next
Set colFilteredItems = Nothing
Set colAttachments = Nothing
Set colItems = Nothing
End Sub
Add logon step between above 2 lines
WSCript.Sleep 2000
objNamespace.Logon
objNamespace.SendAndReceive(True)
Below this line:
Set objNamespace = objOutlook.GetNamespace("MAPI")
Add this:
WSCript.Sleep 2000
objNamespace.SendAndReceive(True)
I found it here:
http://www.experts-exchange.com/Software/Office_Productivity/Groupware/Outlook/Q_28215854.html

Excel will not close processes

So, I'm using (after modification) this code, from here: How to set recurring schedule for xlsm file using Windows Task Scheduler
My error: Runtime error: Unknown runtime error.
I've searched far and wide to find an way to close the Excel process, but almost everybody uses .Quit sadly it gives the above error. I've also tried .Close, but that is not recognized
' Create a WshShell to get the current directory
Dim WshShell
Set WshShell = CreateObject("WScript.Shell")
' Create an Excel instance
Dim myExcelWorker
Set myExcelWorker = CreateObject("Excel.Application")
' Disable Excel UI elements
myExcelWorker.DisplayAlerts = False
myExcelWorker.AskToUpdateLinks = False
myExcelWorker.AlertBeforeOverwriting = False
myExcelWorker.FeatureInstall = msoFeatureInstallNone
' Tell Excel what the current working directory is
Dim strSaveDefaultPath
Dim strPath
strSaveDefaultPath = myExcelWorker.DefaultFilePath
strPath = "C:\Users\hviid00m\Desktop"
myExcelWorker.DefaultFilePath = strPath
' Open the Workbook specified on the command-line
Dim oWorkBook
Dim strWorkerWB
strWorkerWB = strPath & "\Status Report (Boxplots) TEST.xlsm"
Set oWorkBook = myExcelWorker.Workbooks.Open (strWorkerWB, , , , , , True)
' Build the macro name with the full path to the workbook
Dim strMacroName
strMacroName = "Refresh"
on error resume next
myExcelWorker.Run strMacroName
if err.number <> 0 Then
WScript.Echo "Fejl i macro"
End If
err.clear
on error goto 0
oWorkBook.Save
' Clean up and shut down
' Don’t Quit() Excel if there are other Excel instances
' running, Quit() will shut those down also
myExcelWorker.Quit <--- ERROR
Set oWorkBook = Nothing
Set myExcelWorker = Nothing
Set WshShell = Nothing
Found some code on a different side.
The reason why (as far as I understood) is that .Quit and .Close is for VBA not VBS.
' Clean up and shut down
' Don’t Quit() Excel if there are other Excel instances
' running, Quit() will shut those down also
Dim objWMIService, objProcess, colProcess
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\.\root\cimv2")
Set colProcess = objWMIService.ExecQuery ("Select * from Win32_Process Where Name = " & "'EXCEL.EXE'")
For Each objProcess in colProcess
objProcess.Terminate()
Next`
Set oWorkBook = Nothing
Set myExcelWorker = Nothing
Set WshShell = Nothing