如何在 Web 驱动程序中检查页面是否已完全加载? [英] How I can check whether a page is loaded completely or not in web driver?

查看:25
本文介绍了如何在 Web 驱动程序中检查页面是否已完全加载?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一些 Java Webdriver 代码来自动化我的应用程序.如何正确检查页面是否已加载?该应用程序也有一些 Ajax 调用.

I am writing some Java Webdriver code to automate my application. How can I correctly check whether the page has been loaded or not? The application has some Ajax calls, too.

我已声明隐式等待 WebDriver.

I have declared an implicit wait for WebDriver.

推荐答案

Selenium 为您做到了.或者至少它尽力而为.有时它不够用,你必须帮助它一点点.通常的解决方案是 Implicit Wait 解决了大部分问题.

Selenium does it for you. Or at least it tries its best. Sometimes it falls short, and you must help it a little bit. The usual solution is Implicit Wait which solves most of the problems.

如果您真的知道自己在做什么以及为什么这样做,您可以尝试编写一个通用方法来检查页面是否完全加载.但是,它不能适用于所有网络和每种情况.

If you really know what you're doing, and why you're doing it, you could try to write a generic method which would check whether the page is completely loaded. However, it can't be done for every web and for every situation.

相关问题:Selenium WebDriver: 等待加载带有 JavaScript(JS) 的复杂页面,在此处查看我的答案.

Related question: Selenium WebDriver : Wait for complex page with JavaScript(JS) to load, see my answer there.

较短的版本:你永远无法确定.

Shorter version: You'll never be sure.

正常"加载很容易 - document.readyState.当然,这是由 Selenium 实现的.有问题的是异步请求,AJAX,因为你永远无法判断它是否已经完成.今天的大多数网页都有永久运行的脚本,并且一直在轮询服务器.

The "normal" load is easy - document.readyState. This one is implemented by Selenium, of course. The problematic thing are asynchronous requests, AJAX, because you can never tell whether it's done for good or not. Most of today's webpages have scripts that run forever and poll the server all the time.

您可以做的各种事情都在上面的链接下.或者,像 95% 的其他人一样,使用 Implicit Wait 隐式和 显式等待 + ExpectedConditions 在需要的地方.

The various things you could do are under the link above. Or, like 95% of other people, use Implicit Wait implicity and Explicit Wait + ExpectedConditions where needed.

例如单击后,页面上的某些元素应该会变得可见,您需要等待它:

E.g. after a click, some element on the page should become visible and you need to wait for it:

WebDriverWait wait = new WebDriverWait(driver, 10);  // you can reuse this one

WebElement elem = driver.findElement(By.id("myInvisibleElement"));
elem.click();
wait.until(ExpectedConditions.visibilityOf(elem));

这篇关于如何在 Web 驱动程序中检查页面是否已完全加载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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