MackyMac
MackyMac

Reputation: 61

How to make selenium wait for the page to fully load in Selenium - wrapper for Excel VBA?

What code or function for Selenium - wrapper for Excel VBA to be used so that the program will wait for the page to fully load then execute other commands?

Upvotes: 2

Views: 34601

Answers (5)

pranjalk
pranjalk

Reputation: 21

While Florent B's answer is as usual correct what I'd like to add further to that is that there is another piece of code that you might want to add to directly address the problem of increasing the PageLoad in addition to setting the Server Timeouts of the selenium browser:

'At the beginning of the code
Application.DisplayAlerts = False

driver.Timeouts.PageLoad = 500000 'this is the piece of code that worked for me
driver.Timeouts.Server = 500000

'At the end of the code
Application.DisplayAlerts = True

The pageload code makes sure that the browser continues to wait until the page is fully loaded with all the data that needs to get loaded in it.

Further, if you would need to increase the time to as much as 500 seconds as highlighted in the code you may also want to disable alerts in Excel as highlighted in code above

Upvotes: 2

Christian Torrez
Christian Torrez

Reputation: 81

Hi using this Reference wait-for-page-load-in-selenium

I developed this code:

While Selenium.ExecuteScript("return document.readyState") <> "complete"
    Selenium.Wait (5000)
Wend

It looks that the best way is pseudocode: Wait until a specific FindElementbyId appears. The solution presented is a general approach.

Upvotes: 2

Florent B.
Florent B.

Reputation: 42538

The latest version waits implicitly that the targeted element is present before executing the required action. For instance, driver.FindElementById("...").Click will by default try to find the element during 3 seconds before throwing an error.

This implicit waiting can be defined globally:

driver.Timeouts.ImplicitWait = 5000 ' 5 seconds

As well as individually :

driver.FindElementById("id", timeout:=10000).Click  ' implicitly wait of 10sec
driver.FindElementById("id", timeout:=0).Click  ' no implicit waiting

To set the timeout for the server that runs the browser:

driver.Timeouts.Server = 120000 ' 2 mins

To get the latest version in date working with the above example: https://github.com/florentbr/SeleniumBasic/releases/latest

Upvotes: 6

jlookup
jlookup

Reputation: 66

I have this problem as well. I have tried both implicit and explicit waits, but have not been able to get the timeout to hold off longer than 30 seconds. I use two workarounds.

The first is klunky but simple: after my .get or .click event that initiates the page load I pause the code using a messagebox: msgBox "Wait for page to load then click OK."

The second is a bit more refined. If I know my page usually takes 50 seconds to load I will put in a set wait period of say, 45 seconds, then have the script verify that a certain element has loaded before continuing:

Public Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As LongPtr)

sub LongLoad()
Dim driver As New SeleniumWrapper.WebDriver
Dim By As New By, Assert As New Assert, Verify As New Verify, Waiter As New Waiter
dim YesNo as boolean

driver.Start "chrome", "https://super.secret.com/"
driver.setImplicitWait 5000
driver.setTimeout 120000

driver.Get "/heroLogin"
driver.findElementByName("USER").Clear
driver.findElementByName("USER").SendKeys "user"
driver.findElementByName("PASSWORD").Clear
driver.findElementByName("PASSWORD").SendKeys "pass"
driver.findElementById("Button").Click

'pause for 45 seconds (see if you can hold your breath)
Sleep 45000

'poll for the presence of an element. In my case a certain string of    
'text is good enough. 
driver.verifyTextPresent ("Enterprise Reporting")

'now you're ready to go.

.verifyTextPresent also has a timeout of 30 seconds, so you still have to use the sleep function first for slow page loads.

Upvotes: 0

drkthng
drkthng

Reputation: 6939

Short Answer

Selenium (also the ExcelVBA wrapper) does this for you. It waits for the DOM readyState to signal that it is complete.

In case of the Excel-Wrapper this will be done when calling driver.start

BUT

This won't help you if you have AJAX-calls or Elements that load via JavaScript and so forth.

There you will need to implement implicit and explicit waits to check if the element you are trying to interact with is already existent or even displayed.

It makes no sense to wait for ALL elements to be visible if you want to interact with a certain element. In this case you should just wait for this element to be displayed and then interact with this element.

If you would elaborate more on your actual problem, we could give you much more detailed advice.

Upvotes: 0

Related Questions