【问题标题】:time out when clicking an element using selenium webdriver使用 selenium webdriver 单击元素时超时
【发布时间】:2014-08-18 10:19:47
【问题描述】:

我在尝试单击链接文本时遇到问题。问题是页面加载时间过长并导致异常:“命令持续时间或超时:60.01 秒”。我也设置了:

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

总是在 60.01 秒后超时,看起来代码无法覆盖页面加载的默认超时时间。我正在使用 chrome 驱动程序。这是我的代码:

    driver.get(baseUrl + "?k=" + test.Key);

    // verify the MoCoin icon exists
    assertTrue("The 'MoCoin' icon does not exist",
            driver.findElements(By.id("tokens-navbar")).size() != 0);

    // verify the 'pronews' menu exists
    assertTrue("The the 'pronews' menu does not exist", driver
            .findElements(By.linkText("PRONEWS")).size() != 0);

    // click on the 'pronews' menu
    driver.findElement(By.linkText("PRONEWS")).click(); // exception here, the element was clicked and the new page was loading

有没有办法覆盖页面加载的时间?或者任何停止页面加载的技巧,以便我可以验证标题和其他元素?任何帮助都会很棒。

【问题讨论】:

  • 我也有同样的问题。如果你找到了,我很想知道答案。
  • 我不记得我为克服这个问题做了什么。我使用Appium 进行自动化测试,我只是将此应用程序的超时设置为 60 多秒。该问题不属于网络驱动程序。希望对你有帮助。

标签: selenium webdriver


【解决方案1】:

请尝试以下代码等待 7 秒,轮询时间为 50 毫秒 - 根据您的方便更改时间 -

new WebDriverWait(driver,7,50).until(ExpectedConditions.textToBePresentInElementLocated(By.linkText("PRONEWS"), "PRONEWS"));
driver.findElement(By.linkText("PRONEWS")).click();

【讨论】:

【解决方案2】:

尝试使用显式等待而不是隐式等待: 显式等待:

WebDriverWait.until(condition-that-finds-the-element) : 你也可以在这里忽略异常。

隐式等待:

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

更多信息在这里:http://www.bizalgo.com/2012/01/14/timing-races-selenium-2-implicit-waits-explicit-waits/

也看看流利的等待:http://selenium.googlecode.com/svn/trunk/docs/api/java/org/openqa/selenium/support/ui/FluentWait.html

【讨论】:

  • 元素被点击,新页面正在加载。 60 秒后,它导致超时异常。这与驱动程序找不到元素无关。
  • 哦,抱歉误解了这个问题。尝试在隐式等待之前设置 driver.manage().timeouts().pageLoadTimeout(180, TimeUnit.SECONDS) 。看看这是否有效。
  • 感谢您的支持,但在 60 秒后仍然超时。
猜你喜欢
  • 1970-01-01
  • 2012-09-05
  • 1970-01-01
  • 1970-01-01
  • 2018-06-11
  • 2013-10-04
  • 1970-01-01
  • 2013-01-13
  • 2012-08-21
相关资源
最近更新 更多