如何使selenium等待页面完全加载到Excel VBA的包装?

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

简答

Selenium(也是ExcelVBA包装)为你做这个。 它等待DOM readyState信号表示已完成。

在Excel-Wrapper的情况下,这将在调用driver.startdriver.start

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

在那里你将需要实现隐式和显式的等待来检查你正在尝试与之交互的元素是否已经存在或者甚至被显示。

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

如果你想详细说明你的实际问题,我们可以给你更详细的build议。

我也有这个问题。 我已经尝试了隐式和显式的等待,但是还没有能够超时超过30秒。 我使用两种解决方法。

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

第二个是更精致一点。 如果我知道我的页面通常需要50秒的加载时间,我会放置一个45秒的等待时间,然后让脚本validation某个元素已经加载,然后继续:

 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秒的超时时间,所以您仍然必须首先使用睡眠function来缓慢加载页面。

最新版本在执行所需操作之前隐含地等待目标元素存在。 例如,driver.FindElementById(“…”)。默认情况下,在抛出错误之前,会在3秒内尝试find元素。

这种隐含的等待可以在全局上定义:

 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 

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

您好使用此参考等待页面加载在selenium

我开发了这个代码:

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

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