硒等到文件准备好 [英] Selenium wait until document is ready

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

问题描述

任何人都可以让我如何让硒等到页面完全加载时吗?我想要通用的东西,我知道我可以配置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.

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

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