logic to loop until web page element is not nothing - vba

*Using HTML Object library with vba
*(CAS is set as Browser instance (shdocvw))
Set HTMLDoc = CAS.document.frames("MainFrame").document 'pull the main frame
Do Until Not HTMLDoc Is Nothing
DoEvents
Loop
I dont think this is correct since, It will only set HTMLDoc one time, and if it is nothing, its going to keep looping itself over and over, checking for it to be something, but since it's only called once. A better way to go, imho, would be can check for an element and loop until the element exists, since the page can load, but my elements pulling from a DB take half a second longer or so. Im just not sure how to write the loop to keep setting the htmldoc, and then keep checking for an element within it to be not nothing. (The point is so even if my wait timer isnt waiting long enough, it should not proceed until the element exists)

If you wanted to wait for a specific element:
Dim el As Object
Do
Set el = Nothing
On Error Resume Next
Set el = CAS.document.frames("MainFrame").document.getElementById("idHere")
On Error GoTo 0
DoEvents
Loop While el Is Nothing
You probably want to build in a maximum wait time though, so you don't loop endlessly if for some reason the element never appears.

Related

Macro single step works when routine doesn't

I have been running this macro and it come up with an 424 Object Required Error but the macro works and I get the expected result when I run it with a single step button "F8".
Sub FileUpload()
Dim IEexp As InternetExplorer
Set IEexp = CreateObject("InternetExplorer.Application")
IEexp.Visible = False
IEexp.navigate "https://www.google.co.uk/?gws_rd=ssl#q=lenti+a+contatto+colorate"
Do While IEexp.ReadyState <> 4: DoEvents: Loop
Dim inputElement As HTMLDivElement
Set inputElement = IEexp.Document.getElementById("brs")
MsgBox inputElement.textContent
IEexp.Quit
Set IEexp = Nothing
End Sub
The error comes up on the Set inputElement = IEexp.Document.getElementById("brs") line.
You’re checking the ReadyState of the browser, but with some modern web pages the DOM isn’t actually updated with some objects until at least that point.
IE automation in VBA is quite primitive, and it sounds like in this scenario you’re trying to access a node in the DOM before it exists - despite your best efforts to wait until the browser is ready. In some cases this can literally be a matter of milliseconds out in timings.
Your quickest fix here is to simply add Application.Wait() in your loop to cause an actual time delay. A more elegant option might be to introduce a check in your loop and exit the loop when the desired DOM object actually exists. If you do this, there’s a danger of ending up in an infinite loop and so I would always recommend setting a maximum number of increments as a backup.

How to get the last child of an HTMLElement

I have written a macro in Excel that opens and parses a website and pulls the data from it. The trouble I'm having is once I'm done with all of the data on the current page I want to go to the next page. To do this I want to get the last child of the "result-stats" node. I found the lastChild function, and so came up with the following code:
'Checks to see if there is a next page
If html.getElementById("result-stats").LastChild.innerText = "Next" Then
html.getElementById("result-stats").LastChild.Click
End If
And here is the HTML that it is accessing:
<p id="result-stats">
949 results
<span class="optional"> (1.06 seconds)</span>
Modify search
Show more columns
Next
</p>
When I try to run this, I get an error. After a lot of searching I think I found the reason. According to what I read, getElementById returns an element and not a node. lastChild only works on nodes, which is why the function doesn't work here.
My question is this. Is there a clean and simple way to grab the last child of an element? Or is there a way to typecast an element to that of a node? I feel like I'm missing something obvious, but I've been at this way longer than I should have been. Any help anyone could provide would be greatly appreciated.
Thanks.
Here's a shell of how to do it. If my comments are not clear, ask away. I assumed knowledge of how to navigate to the page, wait for the browser, etc.
Sub ClickLink()
Dim IE As Object
Set IE = CreateObject("InternetExplorer.Application")
'load up page and all that stuff
'process data ...
'click link
Dim doc As Object
Set doc = IE.document
Dim aLinks As Object, sLink As Object
For Each sLink In doc.getElementsByTagName("a")
If sLink.innerText = "Next" Then 'may need to play with this, if `innerttext' doesn't work
sLink.Click
Exit For
End If
Next
End Sub

IE source code placeholder control for my VBA scraper

I have the following code which opens an IE page, and fills in the fields with the value "caravan". However I only need the first field to be filled in with "caravan". I need the second one to be filled in with "2016" for example. I've had trouble with this task because I can't seem to uniquely identify each element within the input tag (to which all of the fields belong).
Here is my code:
Sub Quote()
Dim ie As Object
Set ie = CreateObject("InternetExplorer.Application")
ie.navigate ("https://insurance.qbe.com.au/portal/caravan/form/estimate")
ie.Visible = True
Do
DoEvents
Loop Until ie.readystate = 4
Application.Wait (Now + TimeValue("00:00:03"))
Do
DoEvents
Loop Until ie.readystate = 4
Set inputCollection = ie.document.getElementsByTagName("input")
For Each inputElement In inputCollection
inputElement.Value = "Caravan"
Next inputElement
Loop
End Sub
So it's taking each "inputElement" that is housed within the "input" tag, and where possible, it's making a corresponding field's display value be that of "caravan".
To illustrate why I'm having difficulty in uniquely identifying each field, here is the source of the first two fields (first one is for caravan type; second one is for caravan year-of-manufacture):
First one
Second one
So neither have an id. And both are within the "input" tag and both have the same classname. So I can't get-element-by-id or get-elements-by-classname. I've tried getting elements by classname in a wide range of ways and it simply does nothing (no error is produced and the web page isn't affected).
The only way I've managed to fill in a field is through using the code I have above. But, again, it's changing all the fields of course. I figure that the only thing I can really use to get my code to tell the two apart is the placeholder element of each one.
But how do I achieve this seeing as you cannot "get element by placeholder"
//
I've since tried to confirm that there's no way to use classname, with the following code modification:
Set inputCollection = ie.document.getElementsByTagName("input")
For Each inputElement In inputCollection
If ie.document.getElementsByClassName.Value = "ui-select-search ui-select-toggle ng-pristine ng-valid ng-touched" Then inputElement.Value = "Caravan"
Oh my! How exciting!! I finally found out how to do this after literally days of searching online. It always had to be something simple (but, alas, this isn't my area of expertise at all so it's always going to be really challenging for me). Anyway, this code works (and I expect I will need to put a fire-event line in soon):
Set inputCollection = ie.document.getElementsByTagName("input")
For Each inputElement In inputCollection
If inputElement.getAttribute("placeholder") = "Caravan type" Then
inputElement.Value = "Caravan"
Exit For
End If
Next inputElement
I was so unaware of "getAttribute" but it makes so much sense. If you don't have an id and some of the fields you are looking at have the same classname (as can often be the case), then you need to rely on other unique attributes and use this sort of code.
If you're wondering where I found out about this, I found this pretty cool Youtube channel, and here's the specific video that helped me:
https://www.youtube.com/user/alexcantu3/videos
Hope it helps someone else some day!

How to work with result collections from Selenium

Or how to work with collections (or arrayss) in VBA.
The issue is most probably myself, but I couldn't find an answer yet.
I am trying to go trough a some pages on a web-site with Selenium-vba to find some data.
As usual if there is more to display, the site shows a 'NEXT' button. The button has <a href ... > when the link is activated, else it's just plain text.
To test if there is another page I have found the way to use findElementsByLinkText, and either there is a link or the the collection is empty. So this can be tested by the size of the collection.
This works so far.
But when I try to use the collection (aside from a for each loop) for further action I can't get it to operate.
This is the code:
Dim driver As New SeleniumWrapper.WebDriver
Dim By As New By, Assert As New Assert, Verify As New Verify, Waiter As New Waiter
On Error GoTo ende1
driver.Start "chrome", "http://www.domain.tld/"
driver.setImplicitWait 5000
driver.get "//......."
Set mynext = driver.findElementsByLinkText("Next")
if mynext.Count >0 Then
mynext(1).Click 'THIS STATEMENT DOES NOT WORK
End If
So please help me to get around my understanding issue (which I am convinced it is)
How can I access an element from the collection.
My workaround so far is to execute
driver.findElementByLinkText("Next").Click
but this is unprofessional as it executes the query again.
The Next button is probably loaded asynchonously after the page is completed.
This implies that findElementsByLinkText("Next") returns no elements at the time it's called.
A way to handle this case is to silent the error, adjust the timeout and test the returned element:
Dim driver As New Selenium.ChromeDriver
driver.Get "https://www.google.co.uk/search?q=selenium"
Set ele = driver.FindElementByLinkText("Next", Raise:=False, timeout:=1000)
If Not ele Is Nothing Then
ele.Click
End If
driver.Quit
To get the latest version in date working with the above example:
https://github.com/florentbr/SeleniumBasic/releases/latest

UFT 12.0 unable to synchronize an object using .Exists property

from last few days i am trying to synchronize two objects that are dependent on each other for their execution.
EX: Object B should execute only after the completion of object A.
Now, in my scenario, the Object A is taking some time to execute, for this I need to synchronize the two objects.
The Completion of object will result in an image which will tell the user that the process is completed successfully or failed.
Below is my code:
sLoop = True
Do while sLoop
If (Browser("ABC").Page("PQR").Frame("XYZ").Image("BatchSuccess").Exist) then
msgbox "in if"
sLoop = false
Else
msgbox "in else"
sLoop = true
End If
Loop
Other Activities...
My IF condition is getting executed before it gets satisfied.
there is an image of ongoing process which the UFT is considering as the successful...
Note: there is also an image while executing the object. (observation, UFT is not able to distinguish between these 2 images)
Try to use WaitProperty method. Exist will return true when it finds Your object in app.
For example
objectA.WaitProperty(optionFromObjectSpy,someSpecificContent)
optionFromObjectSpy - innerhtml etc.
Just by looking at your code and your question, one thing pops right into my head is that you can use wait time inside your exist statement.
Like :
If B().P().Image().Exist("10") Then *'you can change the 10 second to anything you want*
'Code here
End If
Or Alternatively you could create a function that will check if readystate of the browser (assuming its IE) is 4 or not.
do while ie.readystate <> 4
Wait(1)
Loop
Call the function before doing anything with the Image object. Should work.
Try something like -
While not Browser("ABC").Page("PQR").Frame("XYZ").Image("BatchSuccess").Exist
Wait 1
Browser("ABC").Sync
Wend
'your code here after object A is synchronized
Browser("ABC").Page("PQR").Frame("XYZ").Image("BatchSuccess").Click