【问题标题】:Make Selenium Webdriver Stop Loading the page if the desired element is already loaded?如果所需的元素已经加载,让 Selenium Webdriver 停止加载页面?
【发布时间】:2014-01-19 07:42:44
【问题描述】:

我正在创建一个测试并遇到一些问题。这是场景。我使用 Selenium Web 驱动程序在 Page1 上填写表单并通过单击按钮提交表单。 Page2 开始加载...但问题是,Page2 使用 Google Analytics 代码,有时页面需要很长时间才能停止加载。

即使预期的元素已经存在,Selenium Web 驱动程序在整个网页完全加载之前不会继续。

如果预期的元素已经存在,我如何让 Selenium 继续执行下一个任务或停止加载外部 javascript/css?

我尝试调整以下设置,但没有成功。

driver.manage().timeouts().pageLoadTimeout(60, TimeUnit.SECONDS);
driver.manage().timeouts().setScriptTimeout(30, TimeUnit.SECONDS);
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

临时解决方案:滚动下方以获得答案!

【问题讨论】:

标签: java selenium selenium-webdriver


【解决方案1】:

给下面的方法一个镜头。

driver.findElement(By.tagName("body")).sendKeys("Keys.ESCAPE");

((JavascriptExecutor) driver).executeScript("return window.stop");

或者,您也可以使用 WebDriverBackedSelenium,如下面的 Vincent Bouvier 的 sn-p 所示。

//When creating a new browser:
WebDriver driver = _initBrowser(); //Just returns firefox WebDriver
WebDriverBackedSelenium backedSelenuium = 
            new WebDriverBackedSelenium(driver,"about:blank");    

//This code has to be put where a TimeOut is detected
//I use ExecutorService and Future<?> Object

void onTimeOut()
{
    backedSelenuium.runScript("window.stop();");
}

来源:https://sqa.stackexchange.com/a/6355

来源:https://stackoverflow.com/a/13749867/330325

【讨论】:

  • 我试了一下,但同样的问题仍然存在。无论我是点击 JavascriptExecutor 还是 WebDriver 的按钮,Page2 都会开始加载。但是由于 Page2 没有完全加载,它要么抛出 pagetimeout 错误,要么不移动到下一条语句(即检查 Page2 中是否存在预期的 elm)。 Page2 有元素 visitble,只是 selenium 坚持只有在页面完全加载后才能继续。
  • 仅供参考,我还尝试尝试/捕获 pagetimeout 异常,看看我是否仍然可以执行 selemium 命令,但它也不执行命令:(
【解决方案2】:

所以,我向 Selenium 报告了这些问题。临时解决方法是……弄乱 Firefox 的超时设置。基本上默认情况下,Firefox 在超时之前等待每个连接大约 250 秒。您可以查看 about:config 了解详细信息。基本上我把它调低了,这样 Firefox 就不会等待太久,Selenium 可以继续,就好像页面已经完成加载一样:P。

其他浏览器可能存在类似的配置。我仍然认为 Selenium 应该让我们处理 pagetimeout 异常。请务必在此处为错误添加星标:http://code.google.com/p/selenium/issues/detail?id=6867&sort=-id&colspec=ID%20Stars%20Type%20Status%20Priority%20Milestone%20Owner%20Summary,以便 selenium 修复这些问题。

FirefoxBinary firefox = new FirefoxBinary(new File("/path/to/firefox.exe"));
FirefoxProfile customProfile = new FirefoxProfile();
customProfile.setAcceptUntrustedCertificates(true);
customProfile.setPreference("network.http.connection-timeout", 10);
customProfile.setPreference("network.http.connection-retry-timeout", 10);

driver = new FirefoxDriver(firefox, customProfile);
driver.manage().deleteAllCookies();

【讨论】:

    【解决方案3】:

    一旦您检查了元素并且知道它存在,您可以导航到/加载不同的页面(如果下一个任务在不同的页面上)或者如果任务在同一页面上(如你反正不需要尚未加载的元素),你可以像往常一样继续 - selenium 将识别已经加载的元素。当我使用功能丰富的页面时,这对我有用。

    【讨论】:

      【解决方案4】:

      不要使用 webdriver click() 提交表单,而是使用 jsexecutor 并单击。 Jsexecutor 不等待页面加载,您可以使用其他操作。

      【讨论】:

      • 我尝试使用((JavascriptExecutor)getDriver()).executeScript("document.getElementById('btn-id').click();"); 但同样的问题。 Page2 正在加载,Page2 已经加载了项目,但由于页面没有完全加载完成,Selenium 没有执行下一个命令getDriver().findElement(By.id("elm-waiting-for")).isDisplayed()
      【解决方案5】:

      根据上面的场景解释,我觉得最好在第一页使用下面的等待命令。

      WebDriverWait wait = new WebDriverWait(driver, 10);
      WebElement element = wait.until(ExpectedConditions.presenceOfElementLocated(By.id(>someid>)));
      

      一旦在第一页中找到所需的元素,接下来您就可以进入第二页。

      【讨论】:

        【解决方案6】:

        根据上面的场景解释,我觉得最好在第一页使用下面的等待命令。

        WebDriverWait wait = new WebDriverWait(driver, 10); WebElement element = 
                wait.until(ExpectedConditions.presenceOfElementLocated(By.id(>someid>)));
        

        一旦在第一页中找到所需的元素,您就可以进入第二页。

        【讨论】:

          【解决方案7】:

          使用显式/webdriver 等待----

             WebDriverWait wt=new WebDriverWait(driver, 20);
             wt.until(ExpectedConditions.elementToBeClickable(By.name("abc")));
          

          【讨论】:

          • 此解决方案与所提出的问题无关。
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-11-14
          • 1970-01-01
          • 1970-01-01
          • 2011-12-11
          • 1970-01-01
          • 2015-10-31
          相关资源
          最近更新 更多