Selenium:带有 WebDriver 的 Firefox:页面加载问题

发布于 2024-12-02 19:03:23 字数 635 浏览 0 评论 0原文

SeleniumServer 版本:2.5.0,Firefox 版本:4.0.1

我遇到的情况是通过 Ajax 调用呈现“新问题”超链接。页面加载完成后,我需要单击此超链接才能继续。我正在使用类似下面的内容来等待链接出现,然后单击它。

while (!(driver.findElement(By.xpath("//a[text()='New Question']")).isEnabled())) {
            Thread.sleep(1000);
}
driver.findElement(By.xpath("//a[text()='New Question']")).click();

这在 IE 中就像一个魅力。但在 Firefox 中,该链接未被单击。

这告诉我,Firefox 告诉 Selenium 页面在未完全加载时已加载,而 IE 正在做正确的事情。

为了检查点击是否确实发生,我在超链接的 onclick 中添加了 javascript: alert("Hello From Chandra");。弹出窗口出现在 IE 上,但在 Firefox 上没有。

问题:我做错了什么/不充分吗?有解决方法吗?

谢谢。 PS:如果您需要更多信息,请告诉我。

SeleniumServer version: 2.5.0, Firefox version: 4.0.1

I have a situation where a 'New Question' hyperlink is rendered through an Ajax call. Once the page load is complete, I need to click on this hyperlink to proceed. I'm using something like below to wait until the link is present and then click on it.

while (!(driver.findElement(By.xpath("//a[text()='New Question']")).isEnabled())) {
            Thread.sleep(1000);
}
driver.findElement(By.xpath("//a[text()='New Question']")).click();

This works like a charm in IE. But in Firefox, the link is not clicked.

What this tells me is that Firefox is telling Selenium that the page is loaded when it is not loaded fully, where as IE is doing the right thing.

To check whether the clicking is actually happening, I put in a javascript: alert("Hello From Chandra"); to the hyperlink's onclick. The pop-up showed up on IE, but not on Firefox.

Question: Am I doing something wrong/inadequate? Is there a workaround?

Thanks.
PS: Please let me know if you need more info.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

岛徒 2024-12-09 19:03:23

您不需要 while 循环,您可以在初始化驱动程序时使用隐式等待。这将使驱动程序轮询元素是否存在 90 秒,然后抛出“未找到元素”异常。

 driver.manage().timeouts().implicitlyWait(90, TimeUnit.SECONDS);
 driver.findElement(By.xpath("//a[text()='New Question']")).click();

You don't need the while loop, you could use implicitlyWait when you initialize the driver. This will make the driver poll for the presence of an element for 90 seconds before throwing out element not found exception.

 driver.manage().timeouts().implicitlyWait(90, TimeUnit.SECONDS);
 driver.findElement(By.xpath("//a[text()='New Question']")).click();
诗酒趁年少 2024-12-09 19:03:23

感谢 Nilesh 和 prestomanifesto,但在放入 while 循环之前我尝试过“WebDriverWait”和“implicitlyWait”(效果不佳),但它不起作用。我做了更多的挖掘,“似乎”找到了如下解决方案。页面加载完成后,有一个“搜索”文本字段将获得焦点。我使用“activeElement”来检查焦点是否“真正”设置。

 while (!driver.findElement(By.id("searchValue")).isEnabled()) {
            WebElement currActiveElement = driver.switchTo().activeElement();
            if (currActiveElement.getAttribute("id").equals("searchValue")) {
                break;
            }
            Thread.sleep(1000);
        }

Thanks Nilesh and prestomanifesto, but I had tried 'WebDriverWait' and 'implicitlyWait' before putting in the while loop (which did not work as well) and it did not work. I did a bit more digging and 'seem' to have found a solution as follows. There is a 'search' text field that gets focus once the page has completed loading. I used the 'activeElement' to check if the focus is 'truly' set.

 while (!driver.findElement(By.id("searchValue")).isEnabled()) {
            WebElement currActiveElement = driver.switchTo().activeElement();
            if (currActiveElement.getAttribute("id").equals("searchValue")) {
                break;
            }
            Thread.sleep(1000);
        }
烟─花易冷 2024-12-09 19:03:23

您仍然可以通过等待预期元素的存在(不等待可见性,isEnabled)5-8秒,然后发送 window.stop() JS 脚本(以停止加载更多元素)来加速脚本执行,而无需等待整个页面在 5-8 秒后加载或捕获页面加载超时异常,然后调用 window.stop()

因为如果页面没有采用延迟加载技术(仅加载可见元素并仅在滚动后加载其余元素),它会在之前加载每个元素返回window.ready 状态,因此如果任何元素需要更长的时间来渲染,它会变慢。

You can still speedup your script execution by waiting for presence (not waiting for visibility, isEnabled) of expected element for say 5-8 sec and then sending window.stop() JS Script (to stop loading further elements ) without waiting for entire page to load or catching the timeout exception for page load after 5-8 seconds then calling window.stop()

Because if the page not adopted lazy loading technique (loading only visible element and loading rest of element only after scroll) it loads each element before returning window.ready state so it will be slower if any of the element takes longer time to render.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文