如何使selenium等待页面在Selenium中完全加载 - Excel VBA的包装器?

时间:2015-09-02 08:43:59

标签: excel excel-vba selenium selenium-webdriver vba

Selenium使用什么代码或函数 - 要使用Excel VBA包装程序,以便程序等待页面完全加载然后执行其他命令?

5 个答案:

答案 0 :(得分:1)

最新版本在执行所需操作之前隐式等待目标元素存在。例如,driver.FindElementById(“...”)。默认情况下单击将尝试在发出错误之前的3秒内找到该元素。

这种隐式等待可以全局定义:

driver.Timeouts.ImplicitWait = 5000 ' 5 seconds

以及个别:

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

设置运行浏览器的服务器的超时时间:

driver.Timeouts.Server = 120000 ' 2 mins

要使用上面的示例获取最新版本的日期:https://github.com/florentbr/SeleniumBasic/releases/latest

答案 1 :(得分:1)

尽管Florent B的答案与往常一样正确,但我想进一步补充的一点是,除了设置服务器之外,您可能还想添加另一段代码来直接解决增加PageLoad的问题。硒浏览器超时:

'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

页面加载代码可确保浏览器继续等待,直到页面完全加载了需要加载的所有数据为止。

此外,如果您需要将时间增加到代码中突出显示的时间最多500秒,您可能还想禁用Excel中的警报,如上面的代码突出显示

答案 2 :(得分:0)

简答

Selenium(也是ExcelVBA包装器)为您完成此操作。它等待DOM readyState表示它已完成。

如果是Excel-Wrapper,这将在调用driver.start

时完成

BUT

如果你有通过JavaScript加载的AJAX调用或元素,这对你没有帮助。

您需要实施隐式和显式等待,以检查您尝试与之交互的元素是否已存在或甚至已显示。

如果要与某个元素进行交互,等待所有元素可见是没有意义的。在这种情况下,您应该等待显示此元素,然后与此元素进行交互。

如果您想详细说明您的实际问题,我们可以为您提供更详细的建议。

答案 3 :(得分:0)

我也有这个问题。我已经尝试了隐式和显式等待,但是无法让超时延迟超过30秒。我使用了两种解决方法。

第一个是笨重但很简单:在我的.get.click事件启动页面加载后,我使用消息框暂停代码:msgBox "Wait for page to load then click OK."

第二个更精致。如果我知道我的页面通常需要50秒才能加载,我将设置等待时间为45秒,然后让脚本在继续之前验证某个元素是否已加载:

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也有30秒的超时,因此您仍然必须首先使用睡眠功能来缓慢加载页面。

答案 4 :(得分:0)

您好使用此参考wait-for-page-load-in-selenium

我开发了这段代码:

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

看起来最好的方式是伪代码:Wait until a specific FindElementbyId appears。提出的解决方案是一种通用的方法。