WebDriver Selenium API:ElementNotFoundErrorException 当 Element 明显存在时! [英] WebDriver Selenium API: ElementNotFoundErrorException when Element is clearly there !

查看:25
本文介绍了WebDriver Selenium API:ElementNotFoundErrorException 当 Element 明显存在时!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有时在关闭 Javascript 的情况下在 WebDriver 上运行测试时,WebDriver 在找到元素并尝试单击它时由于 ElementNotFound 错误而崩溃.

但是,该元素显然存在!

阅读本文后:http://code.google.com/p/selenium/wiki/FrequentlyAskedQuestions#Q:_My_XPath_finds_elements_in_one_browser,_but_not_in_others._Wh

我得出的结论是 webdriver 不能等到网页加载完成.如何使用 Webdriver Wait 类?有人可以提供一个例子吗?

解决方案

此示例 已发布在 Google 网上论坛上

一>.根据 Google 开发者的说法:

1 使用隐式等待.在这里司机会等到指定的超时直到找到元素.请务必阅读 javadoc警告.用法:

driver.get("http://www.google.com");driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);WebElement 元素 = driver.findElement(By.name("q"));driver.manage().timeouts().implicitlyWait(0, TimeUnit.SECONDS);//继续测试...

2 使用 org.openqa.selenium.support.ui.WebDriverWait 类.这会轮询直到预期条件为真,返回该条件的结果(如果它正在寻找一个元素).这比隐式灵活得多等待,因为您可以定义任何自定义行为.用法:

Function存在元素定位(最终定位器){return new Function() {公共 WebElement 应用(WebDriver 驱动程序){返回 driver.findElement(locator);}};}//...driver.get("http://www.google.com");WebDriverWait wait = new WebDriverWait(driver,/*seconds=*/3);WebElement element = wait.until(presenceOfElementLocated(By.name("q"));

sometimes when running tests on WebDriver with Javascript turned off, WebDriver crashes due to an ElementNotFound Error when it finds an element, and attempts to click it.

However, the element is clearly there !

After reading this : http://code.google.com/p/selenium/wiki/FrequentlyAskedQuestions#Q:_My_XPath_finds_elements_in_one_browser,_but_not_in_others._Wh

I came to the conclusion that webdriver must not be waiting until the web page has completed loaded. How do I use the Webdriver Wait class ? Can someone provide an example ?

解决方案

This example was posted on Google Groups. According to Google developers:

1 Use implicit waits. Here the driver will wait up until the designated timeout until the element is found. Be sure to read the javadoc for the caveats. Usage:

driver.get("http://www.google.com"); 
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS); 
WebElement element = driver.findElement(By.name("q")); 
driver.manage().timeouts().implicitlyWait(0, TimeUnit.SECONDS); 
// continue with test... 

2 Use the org.openqa.selenium.support.ui.WebDriverWait class. This will poll until the expected condition is true, returning that condition's result (if it's looking for an element). This is much more flexible than implicit waits, as you can define any custom behavior. Usage:

Function<WebDriver, WebElement> presenceOfElementLocated(final By locator) { 
  return new Function<WebDriver, WebElement>() { 
    public WebElement apply(WebDriver driver) { 
      return driver.findElement(locator); 
    }
  };
}

// ... 
driver.get("http://www.google.com"); 
WebDriverWait wait = new WebDriverWait(driver, /*seconds=*/3); 
WebElement element = wait.until(presenceOfElementLocated(By.name("q"));

这篇关于WebDriver Selenium API:ElementNotFoundErrorException 当 Element 明显存在时!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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