I have a vbscript that opens an webpage, then clicks a button. That button opens a new popup webpage on a new window (notice: window not tab).
I want after my script press the button and open a new window, to detect and click the button called id="pagesHeaderLikeButton" in that new window, then close that window.
My script so far:
Set IE = createobject("internetexplorer.application")
strURL = "website"
Do While True
IE.navigate strURL
IE.Visible = true
Do While (IE.Busy Or IE.READYSTATE <> 4)
WScript.Sleep 1000
Loop
Set Popupbutton = IE.Document.GetElementsByClassName("single_like_button btn3-wrap")
for each button in Popupbutton
button.click
WScript.Sleep 5000
exit for
objWindow.Quit
WScript.Sleep 5000
next
Loop
Also to notice. I want to do it like this because the popup window allways will have a new url.
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
The above script will find any opened IE with the URL: facebook.com/*** and will set IE2 to be that opened IE.
So to press the button:
Set Butlike = IE2.Document.getElementsById("pagesHeaderLikeButton")
For Each btn In Butlike
button.click
Next
Related
The web application that displays database information is much more readable in full-screen mode rather than a small window. Problem is, I cannot get the window to maximize from UFT/QTP.
I've tried running the browser object from Wscript.Shell, but the application returns to UFT and maximizes that window instead of the newly created browser window.
siteA = "https://google.com"
Const max_window = 3
Set browobj = CreateObject("Wscript.Shell")
Set oShell = CreateObject("WScript.Shell")
'browobj.Run "chrome -k -incognito -url "&siteA
browobj.Run "chrome -incognito -url "&siteA, max_window
oShell.SendKeys "% x"
browobj.sendkeys "{F9}"
browobj.sendkeys "(% )X"
browobj.SendKeys "% x"
Set browobj = Nothing
Any solutions to maximizing the window with the focus object of the new browser?
Edit:
Even below will not maximize taken from this List of Controls is not working.
SystemUtil.Run "chrome.exe" , siteA ,,,3
chrome.exe --incognito --start-maximized
If you shut down all instances of chrome these controls work fine, but if you have an active window in chrome it will take those properties.
We have a function defined during our browser login process that grabs the window and maximises it so that our UFT processes run with a (nearly) full screen browser - we don't need to hide the menu bar etc. This is the function we are using:
Public Function MISC_MaximiseBrowser(ByVal oBrowser, ByRef ErrorMsg)
LOG_Write vbNewLine & "MISC_MaximiseBrowser"
LOG_Write "oBrowser: " & oBrowser.GetROProperty("TestObjectName")
Dim Hwnd
dim bIsMax, bIsMaxable
MISC_MaximiseBrowser = True
Hwnd = oBrowser.Object.HWND
If Window("hwnd:=" & Hwnd).GetROProperty("maximized") Then
bIsMax = True
Else
bIsMax = False
End If
If Window("hwnd:=" & Hwnd).GetROProperty("maximizable") Then
bIsMaxable = True
Else
bIsMaxable = False
End If
If Not bIsMax And bIsMaxable Then
Window("hwnd:=" & Hwnd).Maximize
End If
End Function
What this does is accepts the Browser UFT object you're working with and grabs the handle for it. If the browser is not already maximized and it's possible to maximize it, it will do so.
You can ignore the LOG_Write statements at the start of the function - those are for our internal logging steps
I am new to VBScript and trying to measure the performance of a website I have created. In my website, when a button is clicked, that item will be added to a shopping cart. There are 6 items to be added to the cart. If my script click the first item to be added to the cart, I want it to wait till proceeding to the next instruction (without putting a random sleep number). In my program, it only adds the 1st and the last item to my cart:
set webbrowser = createobject("internetexplorer.application")
webbrowser.visible = true
webbrowser.navigate("https://www.mywebsite")
Do While webbrowser.busy 'waiting till the webpage is loaded
wscript.sleep(1)
Loop
buttonID = "item1"
Demo(buttonID)'Program should wait till the first button is clicked before going to the statement below'
buttonID = "item2"
Demo(buttonID)
buttonID = "item3"
Demo(buttonID)
buttonID = "item4"
Demo(buttonID)
buttonID = "item5"
Demo(buttonID)
buttonID = "item6"
Demo(buttonID)
Sub Demo(buttonID)
Do
set x = webbrowser.Document.getElementById(buttonID)
If x is nothing then
wscript.sleep 1
else
webbrowser.Document.getElementById(buttonID).click
Exit Do
end if
Loop
End Sub
You can do something like this:
set objie = createobject("internetexplorer.application")
objie.visible=true
objie.Navigate "https://www.swrm2017.org/TimeMeasurementOnlineShoppingSystem/BrowseCatalog/Catalog.php"
swait()
for i = 1 to 6
id = "item"&i
set button = objie.document.getElementById(id)
button.click
swait()
set button = nothing
next
set ie = nothing
sub swait()
while(objie.readystate<>4)
wscript.sleep 10
wend
while objie.document.readystate<>"complete"
wscript.sleep 10
wend
end sub
I am able to open a browser and the page I want to work with. But the actual point of interest is the link called "Next". I want to click on that link but I tried unsuccessfully to get that element in my code. Here is what I have done so far.
Dim URL
Dim IE
Set IE = CreateObject("internetexplorer.application")
URL = "http://mylink.com/"
IE.Visible = True
IE.Navigate URL
Dim a
Set a = IE.Document.GetElementsByTagName("arrowRight")
For i = 0 To a.Length - 1
MsgBox "Found it"
a.Click
Exit For
Next
This is how the "Next" hyperlink is embedded in the page code:
<a class="arrowRight" href="http://SomeURL.com/150.html">Next</a>
arrowRight is a (CSS) class name, not a tag name, so you need to check for a tags with a class arrowRight:
For Each a In ie.document.getElementsByTagName("a")
If a.getAttribute("class") = "arrowRight" And a.innerText = "Next" Then
a.Click
Exit For
End If
Next
I am new to vbscript. I want to save a snapshot taken using vbscript of a internet explorer window opened by vbscript.
Code to load the page
Dim IE, stateString
Set IE = WScript.CreateObject("InternetExplorer.Application")
ie.toolbar = 1
ie.statusbar = 1
ie.width = 999
ie.height = 500
ie.left = 20
ie.theatermode = false
ie.theatermode = true
ie.theatermode = false
ie.top = 50
ie.navigate("file:///C:\Users\Vinit_Tiwari\Documents\vbscripts\someform.html")
'stateString = cstr(ie.readystate)
waitforload(ie)
ie.visible = 1
Set wshShell = CreateObject("Wscript.Shell")
wshShell.AppActivate "Some Form"
wshShell.SendKeys "% x"
Code to take snapshot
Dim oWordBasic
set oWordBasic = CreateObject("Word.Basic")
oWordBasic.sendkeys "{prtsc}"
oWordBasic.AppClose "Microsoft Word"
Set oWordBasic = Nothing
Wscript.Sleep 2000
Saving the snapshot
dim paint
set paint = wshShell.exec("mspaint")
do while paint.status = 0:loop
wshShell.appactivate("untitled-Paint")'this returns false
Wscript.sleep 500
WshShell.SendKeys "^v"
wscript.sleep 500
wshshell.sendkeys "^s"
wscript.sleep 500
wshshell.sendkeys "d:\test.png"
wscript.sleep 500
wshell.sendkeys "{Enter}"
Set wshshell = Nothing
Actually the previously opened ie window is having focus and the keystrokes are sent to the ie instead of paint. so is there any function which performs opposite work of AppActivate.
This won't work. Microsoft Word is in focus while the Print Screen button is pressed. You can easily eliminate this problem by not using Microsoft Word at all. It's not necessary. The Print Screen function is a system hotkey and has nothing to do with Microsoft Word. You should be using the Alt+PrintScreen combination which captures a screenshot of the currently focused window to the clipboard.
Second, if Paint is not in focus instead of IE, it's because you have the window title wrong. You are doing that part correctly. AppActivate returns false if it cannot find a window with the specified title. To be honest, Paint should alread have focus to begin with but it is good practice to make sure the window is activated first.
Also, is this a very old system? Why are you using the Word.Basic automation object anyway out of curiosity?
I'm trying to run an automated vbs script that clicks on a link on a page. I have things of the form:
Const READYSTATE_COMPLETE = 4
Set IE = CreateObject("INTERNETEXPLORER.APPLICATION")
IE.Visible = true
IE.navigate ("http://mywebpage.com")
How do I then make it click on a link on that page that doesn't have an ID but is like
ClickMe!
Thanks!
Along the lines of
Dim LinkHref
Dim a
LinkHref = "link"
For Each a In IE.Document.GetElementsByTagName("A")
If LCase(a.GetAttribute("href")) = LCase(LinkHref) Then
a.Click
Exit For ''# to stop after the first hit
End If
Next
Instead of LCase(…) = LCase(…) you could also use StrComp(…, …, vbTextCompare) (see StrComp() on the MSDN).