浏览器加载程序运行时如何在弹出窗口上与Webelement交互?

时间:2019-02-27 11:15:15

标签: selenium automation

在加载网站之前,我会有一个身份验证弹出窗口,而网页上的弹出窗口显示和浏览器上运行的加载程序显示,这意味着网页未完全加载。 根据硒,如果最初没有加载完整的网页,则硒不会与元素

交互

对此需要帮助。

2 个答案:

答案 0 :(得分:1)

对Java + Selenium使用以下方法

public boolean  isPageReady(WebDriver driver){
   boolean readyStateComplete = false;
    while (!readyStateComplete){
        JavascriptExecutor executor = (JavascriptExecutor) driver;
        readyStateComplete = executor.executeScript("return document.readyState") == "complete";
    }
    return  readyStateComplete;
}

对于C#+硒

private void WaitUntilDocumentIsReady(TimeSpan timeout){
    var javaScriptExecutor = WebDriver as IJavaScriptExecutor;
    var wait = new WebDriverWait(WebDriver, timeout);

    // Check if document is ready
    Func<IWebDriver, bool> readyCondition = webDriver => javaScriptExecutor
            .ExecuteScript("return (document.readyState == 'complete' && jQuery.active == 0)");
    wait.Until(readyCondition);
}

答案 1 :(得分:0)

您可以等到页面通过JavaScript完全加载。

private void WaitUntilLoaded()
{
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(30));
wait.Until((x) =>
{
    return ((IJavaScriptExecutor)this.driver)
    .ExecuteScript("return document.readyState").Equals("complete");
});
}

另一种选择是等待特定元素在页面上可见

WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(30));
wait.Until(ExpectedConditions.VisibilityOfAllElementsLocatedBy(
By.XPath("some Xpath")));

来源:https://www.automatetheplanet.com/advanced-webdriver-tips-tricks-part-1/