Selenium 等待文档准备就绪 [英] Selenium wait until document is ready

查看:27
本文介绍了Selenium 等待文档准备就绪的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能告诉我如何让 selenium 等到页面完全加载的时候?我想要一些通用的东西,我知道我可以配置 WebDriverWait 并调用诸如find"之类的东西来让它等待,但我没有走那么远.我只需要测试页面是否成功加载,然后转到下一页进行测试.

Can anyone let me how can I make selenium wait until the time the page loads completely? I want something generic, I know I can configure WebDriverWait and call something like 'find' to make it wait but I don't go that far. I just need to test that the page loads successfully and move on to next page to test.

我在 .net 中找到了一些东西,但不能让它在 java 中工作......

I found something in .net but couldn't make it work in java ...

IWait<IWebDriver> wait = new OpenQA.Selenium.Support.UI.WebDriverWait(driver, TimeSpan.FromSeconds(30.00));
wait.Until(driver1 => ((IJavaScriptExecutor)driver).ExecuteScript("return document.readyState").Equals("complete"));

有人有什么想法吗?

推荐答案

试试这个代码:

  driver.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS);

上面的代码将等待页面加载最多 10 秒.如果页面加载超过时间会抛出TimeoutException.您捕获异常并满足您的需求.我不确定它是否在抛出异常后退出页面加载.我还没有尝试这个代码.想试试.

The above code will wait up to 10 seconds for page loading. If the page loading exceeds the time it will throw the TimeoutException. You catch the exception and do your needs. I am not sure whether it quits the page loading after the exception thrown. i didn't try this code yet. Want to just try it.

这是一个隐式等待.如果您设置一次,它将具有直到 Web Driver 实例销毁为止的范围.

This is an implicit wait. If you set this once it will have the scope until the Web Driver instance destroy.

请参阅文档以了解WebDriver.Timeouts 了解更多信息.

See the documentation for WebDriver.Timeouts for more info.

这篇关于Selenium 等待文档准备就绪的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆