如何在通过 JS 事件加载新页面时让 Selenium WebDriver 等待页面加载 [英] How to make Selenium WebDriver wait for page to load when new page is loaded via JS event

查看:36
本文介绍了如何在通过 JS 事件加载新页面时让 Selenium WebDriver 等待页面加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在致力于自动化一个站点,该站点具有许多通过 JS 事件加载新页面的链接.基本上,有些元素是可点击的,点击一个元素会导致一些 JavaScript 运行,这会导致提交表单并路由到新页面.

I'm working on automating a site which has a number of links which load new pages via a JS event. Basically, there are elements which are clickable, clicking on one causes some JavaScript to run and this leads to a form being submitted and routing to a new page.

现在,如果这些只是标准的 HTML 链接,那就没有问题了,因为 Selenium 足够聪明,可以告诉有一个新页面即将到来并等待执行操作.但尽管如此,Selenium 无法判断此实例中的点击导致加载新页面,因此它不会等待并继续运行.因此,它不会等待新页面,而是尝试查找不存在的元素,而我的测试都失败了.无赖.

Now if these were just standard HTML links there would be no problem as Selenium is smart enough to tell that there's a new page coming and to wait to do things. But as good as it is, Selenium can't tell that the clicks in this instance are leading to new pages to load so it doesn't wait and just keeps going. As such it doesn't wait for the new page, tries to find elements which aren't there and my tests all fail. Bummer.

作为临时解决方案,我只是将程序暂停三秒钟,如下所示:

As a temporary solution I'm just pausing the program for three seconds like so:

oldPageDriver.clickThatButton();

try {
  Thread.sleep(3000);
} catch(InterruptedException ex) {
  Thread.currentThread().interrupt();
}

newPageDriver = new NewPageDriver(driver);
newPageDriver.doStuffOnNewPage();

这很管用.我不喜欢它,因为它是hacky",只是中断程序而不是做一些更聪明的事情.因为延迟是硬编码的三秒,如果链接正常工作但速度很慢,我会失败.我已经考虑过类似隐式等待的事情,但它完成了同样的事情,经过大量的查找,我没有在 Java 中找到可靠、可行的答案.

And this works, sort of. I don't like it because it's "hacky," and just interrupting the program instead of doing something smarter. Because the delay is hard coded at three seconds I get failures if the link is working but just slow. I've considered something like an implicit wait but that accomplishes the same thing and I've not found a solid, workable answer in Java anywhere after a considerable amount of looking.

那么,任何人都可以提出解决这个问题的方法吗?具体来说,如何让 Selenium 知道需要一个新页面并等待它可用?

So, can anyone suggest a way around this? Specifically, how to make Selenium know that a new page is expected and to wait until it's available?

推荐答案

显式等待是您所需要的;

Explicit waits are what you need;

http://docs.seleniumhq.org/docs/04_webdriver_advanced.jsp

您可以直接将其添加到您的测试中,或者您可能希望将其干燥,特别是如果存在常见的等待期望(例如旋转图标消失).

You can directly add this to your test or you may want to DRY it up, especially if there is a common wait expectation such as the disappearance of a spinning icon.

您可以扩展 click 方法以在单击后始终等待,或者如果跟随页面对象,则将 wait_until_loaded 方法添加到基页面类.还有许多其他有效的方法,但取决于 AUT 的实现方式

You could extend the click method to always wait after clicking or if following page objects, add a wait_until_loaded method to a base page class. There many other valid approaches but dependent on how the AUT is implemented

这篇关于如何在通过 JS 事件加载新页面时让 Selenium WebDriver 等待页面加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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