【问题标题】:Selenium Java (proper wait for new page to load)Selenium Java(正确等待新页面加载)
【发布时间】:2018-09-11 06:28:56
【问题描述】:

这不是一个问题,而是一个信息共享(我想很多人会使用它)

几天来试图做出一个最好的解决方案(没有恶作剧:-D)来等待新页面加载,点击后等。Ofc 没有过去的 Thread.sleep 或隐式等待

有些人建议等到加载新页面的最后一个元素,有些人建议使用有时不起作用的 JS 执行器(document.readyState 解决方案)(在我的情况下,它总是给出完整的响应)

找到另一个解决方案,检查当前页面上对元素的引用何时会抛出 StaleElementReferenceException。但是...在这种情况下,页面在此异常之后无法加载。

我做了什么?将两种解决方案结合在一起,这总是对我有用...一个确保文档未处于就绪状态(导致抛出 staleElementReferenceException),另一个立即检查直到页面完全加载并给出 readyState == complete

    while(true) {    
       try {
          //it's just an interaction (can be any) with element on current page
          anyElementFromCurrentPage.getText();
       } catch (StaleElementReferenceException e) {
            break;
       }
       waitForLoad(driver);
       return this;
    }

    void waitForLoad(WebDriver driver) {
       new WebDriverWait(driver, 30).until((ExpectedCondition<Boolean>) wd ->
                    ((JavascriptExecutor) wd).executeScript("return 
       document.readyState").equals("complete"));
    }

希望有人会使用这个防弹解决方案 :-)

【问题讨论】:

  • 是问题还是解决方案?

标签: selenium load wait proper


【解决方案1】:

所以我在 C# 中工作,但 Java 是类似的。这是我目前使用的,即使它被标记为“过时”。

WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(15));
wait.Until(ExpectedConditions.VisibilityOfAllElementsLocatedBy(By.Name("elementNameHere")));

【讨论】:

    猜你喜欢
    • 2018-03-20
    • 2017-03-27
    • 1970-01-01
    • 2017-11-10
    • 2011-08-17
    • 2018-06-14
    • 2012-07-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多