Selenium WebElement.Click()是否等到下一页加载完毕? [英] Does Selenium WebElement.Click() wait until the next page is loaded?

查看:536
本文介绍了Selenium WebElement.Click()是否等到下一页加载完毕?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我告诉Selenium等到看到元素-Selenium看到它 我告诉Selenium单击此元素,这是链接到新页面的按钮-Selenium单击它.

I tell Selenium to wait until it sees an element - Selenium sees it I tell Selenium to click on this element, it is a button to link to a new page - Selenium click on it.

问题在于,单击它之后,Selenium将等待直到下一页完全加载为止(该页面有时会加载一秒钟,或者等待很长时间,我认为该页面上的Zen Desk Live Chat存在问题) .

The problem is that after clicking it, Selenium will then wait until the next page is fully loaded (the page sometimes loads in a second, or waits for ages, I think it's a problem with Zen Desk Live Chat on that page).

完全加载后,它将引发错误并说看不到单击的元素(自然不能,因为它现在在新页面上)

When it is fully loaded it will then throw an error and say that the element it clicked on cannot be seen (naturally it can't because it is now on a new page)

我尝试使用

driver.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS);

但这无济于事. 我也尝试过

however this doesn't help. I have also tried

wait.until(ExpectedConditions.visibilityOfElementLocated(
By.xpath(element)));

但是这有同样的问题.

下面是我当前正在使用的代码.

Below is the code I am currently using.

try {
    wait.until(ExpectedConditions.visibilityOfElementLocated(
    By.xpath(element)));
    WebElement we = driver.findElement(By.xpath(element));
    we.click();
    System.out.println("Clicked: " + element);
}catch (Exception e){
    System.out.println(e.getMessage()); 
}   

我希望一旦元素被单击,Selenium就可以继续进行而不必关心下一页是否已加载.

I expect that once the element has been clicked on, that Selenium just carries on without caring if the next page has loaded up or not.

但是,当第二页加载时会发生什么,有时该页面会陷入等待widget-mediator.zopim.com"的状态,并且在WebDriverWait时间到期之前,Selenium不会经过click()行(60秒) 60秒过后,我会在控制台输出中看到此错误:

However what happens is when the 2nd page loads up, sometimes the page gets stuck "waiting for widget-mediator.zopim.com" and Selenium will not progress past the click() line until the the WebDriverWait time has expired (60 seconds) After the 60 seconds has expired I see this error in the Console Output:

[1561374309.111][SEVERE]: Timed out receiving message from renderer: 60.000
[1561374309.112][SEVERE]: Timed out receiving message from renderer: -0.002

这里还有其他事情吗?还是Click()是否等到页面加载完毕(如果该点击将其带到新页面)?如果这样做,有没有办法告诉它停止等待?我还有其他代码来检查页面是否已加载,但是我不需要Click()来完成此操作.

Is something else happening here? Or does Click() wait until the page has loaded, if that click takes it to a new page? If it does is there a way to tell it to stop waiting? I have other code to check if the page has loaded or not, but I don't need Click() to do this.

谢谢.

推荐答案

Selenium(或更正确地说是WebDriver)的单击行为由 W3C WebDriver规范.在该文档中,完整的算法定义为请求元素单击时发生的情况.通常,如果单击将导航到新页面,则驱动程序将根据页面加载策略等待该新页面被加载"(故意使用恐吓引号),直到页面加载超时.

Selenium’s (or more correctly, WebDriver’s) behavior on click is governed by the W3C WebDriver Specification. In that document, the full algorithm is defined for what happens when an element click is requested. In general, if the click will navigate to a new page, the driver will wait for that new page to be "loaded" (scare quotes intentional) according to the page load strategy until the page load timeout.

页面加载策略默认为正常",或者等待文档的readyState完成.如果在驱动程序实例化期间要求的功能中将其设置为"none",则驱动程序将完全不等待.选择该路由意味着您将需要处理正在加载的页面的所有同步.请注意,还有第三种页面加载策略渴望",但是在撰写本文时,并不是所有的WebDriver实现(最著名的是chromedriver)都支持它.

The page load strategy defaults to "normal", or waiting for the document’s readyState to be complete. If you set it to "none" in the capabilities requested during driver instantiation, the driver will not wait at all. Choosing that route would mean you would need to handle all synchronization for pages being loaded. Note there is a third page load strategy, "eager", but at the time of this writing, not all WebDriver implementations (most notably chromedriver) support it.

您可以在运行时使用Selenium代码调整页面加载超时.一种方法可能是在单击此特定元素的持续时间内将超时降低到一个相对较低的值,然后再将其恢复到其先前的值.此处的缺点是您必须在继续页面加载超时之前捕获超时异常.

You can adjust the page load timeout at runtime in your Selenium code. One approach might be to lower the timeout to a relatively low value for the duration of clicking on this particular element, then restoring it to its prior value afterward. The drawback here is that you will have to catch the timeout exception that is thrown when the page load times out before continuing.

这篇关于Selenium WebElement.Click()是否等到下一页加载完毕?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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