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
Related
I would like to control the zoom in and out feature of my webpage of the application under test using UFT. This is required as the zoom level changes dynamically and it becomes difficult to identify the objects.
I have found a code but it is useful if you need to change the zoom level at one instance or at the start. below is the code
Function ChangeIEZoom
Dim intZoomLevel, objIE
intZoomLevel = 110
Const OLECMDID_OPTICAL_ZOOM = 63
Const OLECMDEXECOPT_DONTPROMPTUSER = 2
Set objIE = CreateObject("InternetExplorer.Application")
objIE.Visible = True
objIE.Navigate ("www.google.com")
While objIE.Busy = True
wait 5
Wend
objIE.ExecWB OLECMDID_OPTICAL_ZOOM, OLECMDEXECOPT_DONTPROMPTUSER, CLng(intZoomLevel), vbNull
End Function
with this code, it opens up a new browser and navigates it to a URL.
I do not want it to create a new instance of the browser.
What I need is that it changes the zoom level on the same page which is already under test execution, also the page where zoom level change is required not known at the start and it may or may not require change based on the fact that it identifies certain objects.
Has anyone faced the same issue or has a solution to it ?
I found a solution - combining what you mentioned in comments. this works if you want to change the zoom level on current webpage you are working on. helps when you want to zoom in and out at multiple instances
Dim ShellApp
Set ShellApp = CreateObject("Shell.Application")
Dim ShellWindows
Set ShellWindows = ShellApp.Windows()
Dim intZoomLevel
intZoomLevel = 110
Const OLECMDID_OPTICAL_ZOOM = 63
Const OLECMDEXECOPT_DONTPROMPTUSER = 2
Dim i
For i = 0 To ShellWindows.Count - 1
If InStr(ShellWindows.Item(i).FullName, "iexplore.exe") <> 0 Then
Set IEObject = ShellWindows.Item(i)
End If
If IEObject.Visible = True Then
While IEObject.Busy = True
wait 5
Wend
IEObject.ExecWB OLECMDID_OPTICAL_ZOOM, OLECMDEXECOPT_DONTPROMPTUSER, CLng(intZoomLevel), vbNull
End If
Next
print "it works"
I have a VBA UserForm in Excel, with very simple code. It displays a collection (a dictionary, actually) of objects, one at a time, with buttons for "first, previous, next, and last". Everything works great, but if I were to continually click the next button to go through the items, I have to click it slowly (roughly once a second). If I click any faster, the click is ignored. For example, if I click four times over two seconds, it will only 'register' the first and third click and advance twice, instead of four times.
Below is example code for the 'next' button (and the other applicable pieces of code in the userform module):
Private dQIDs As Dictionary
Public Sub TransferQIDs(ByVal dIncomingQIDs As Dictionary)
Set dQIDs = dIncomingQIDs
End Sub
Private Sub bNext_Click()
Call LoadQID(CLng(lIndex.Caption) + 1)
End Sub
Private Sub LoadQID(lQID As Long)
Dim QID As cQID
Set QID = dQIDs(lQID)
lIndex.Caption = lQID
lItems.Caption = "Viewing new QID " & lQID & " of " & dQIDs.Count
Me.tQID = QID.lQID
Me.tTitle = QID.sTitle
Me.tVID = QID.sVendorID
Me.bOS = QID.bOSPatch
Me.bApp = Not QID.bOSPatch
Me.bPrev.Enabled = Not (lQID = 1)
Me.bFirst.Enabled = Not (lQID = 1)
Me.bNext.Enabled = Not (lQID = dQIDs.Count)
Me.bLast.Enabled = Not (lQID = dQIDs.Count)
End Sub
Any ideas?
Personally I would just disable to button while content is loaded.
Private Sub bNext_Click()
Dim b1 As Button
Set b1 = ActiveSheet.Buttons("LoadQID")
REM or Me.LoadQID
b1.Font.ColorIndex = 15
b1.Enabled = False
Application.Cursor = xlWait
Call LoadQID(CLng(lIndex.Caption) + 1)
b1.Enabled = True
b1.Font.ColorIndex = 1
Application.Cursor = xlDefault
End Sub
Reason why this happens is that accessing a single object takes quite a bit of time in Excel. This way if you can click it will be registered.
Alternatively you can toggle UI update with:
Application.ScreenUpdating = False
Application.ScreenUpdating = True
windows is checking for a doubleclick. so if you click fast it registers a doubleclick.
2 options. increase you doubleclick speed in the windows mouse settings
or make a doubleclick event
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
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'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).