51Testing软件测试论坛

标题: webdriver 不能抛出NoSuchElementException异常 [打印本页]

作者: dpdpdp    时间: 2012-12-21 12:02
标题: webdriver 不能抛出NoSuchElementException异常
findElement(By) 在firefox下对象不存在时,不能抛出NoSuchElementException异常,chrome 和ie 下都正常,有人遇到过么
  1. package test;
  2. import java.util.List;
  3. import java.util.concurrent.TimeUnit;

  4. import org.openqa.selenium.By;
  5. import org.openqa.selenium.NoSuchElementException;
  6. import org.openqa.selenium.WebDriver;
  7. import org.openqa.selenium.WebElement;
  8. import org.openqa.selenium.firefox.FirefoxDriver;
  9. import org.openqa.selenium.chrome.ChromeDriver;
  10. import org.openqa.selenium.ie.InternetExplorerDriver;

  11. public class google {
  12.     public static void main(String[] args) throws Exception {
  13.                    System.setProperty("webdriver.firefox.bin","D:/Program Files/Mozilla Firefox/firefox.exe");
  14.                    System.setProperty("webdriver.chrome.driver","D:/Python27/Scripts/chromedriver.exe");
  15.                    WebDriver driver = new InternetExplorerDriver();
  16.         driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
  17.         driver.get("http://www.baidu.com/");
  18. boolean resultsDiv = driver.findElement(By.id("kw1")).isDisplayed();
  19.         System.out.print(resultsDiv);
  20.         WebElement query = driver.findElement(By.id("kw"));
  21.         query.sendKeys("111");
  22.         driver.quit();
  23.      }

  24. }
复制代码

  1. WebDriver driver = new InternetExplorerDriver();
复制代码

使用ie chrome时
  1. boolean resultsDiv = driver.findElement(By.id("kw1")).isDisplayed();
复制代码

都能得到NoSuchElementException异常
但使用firefox时,长时间没有反应
作者: 六月天    时间: 2012-12-21 12:35
帮你试了一下,有NoSuchElementException异常,没问题
作者: dpdpdp    时间: 2012-12-21 12:56
谢谢楼上,我换台机器试试,难道是firefox的问题?
作者: dpdpdp    时间: 2012-12-24 09:48
selenium2.25,升级到2.27可以工作正常了
作者: shijin880921    时间: 2012-12-26 10:10
我最近也在研究webdriver,创建了这个群238488255,里面还是有几个高手的,大伙以后一起学习吧。
作者: 遇上鱼子    时间: 2013-1-23 17:42
其实这里有问题:5, TimeUnit.SECONDS 等待超时时间太短了吧!




欢迎光临 51Testing软件测试论坛 (http://bbs.51testing.com/) Powered by Discuz! X3.2