我正在尝试将selenium测试从Firefox浏览器转换为HTMLUnit驱动程序。但是,当我尝试运行HTMLUnit测试时,它给出了XPATH错误。Firefox浏览器测试运行得非常好。
我的应用程序测试套件广泛使用XPATH。因此,我有意尝试使用XPATH。
我已经尝试过使用
new WebDriverWait(driver, 10)).until(ExpectedConditions.presenceOfElementLocated但是我还是得到了同样的错误。
这是错误:
Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate a node using .//*[@id='tsf']/div[2]/div[3]/center/input[1]
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '2.48.2', revision: '41bccdd', time: '2015-10-09 19:55:52'
System info: host: 'WL309476', ip: '10.83.16.25', os.name: 'Windows 7', os.arch: 'x86', os.version: '6.1', java.version: '1.8.0_66'
Driver info: driver.version: HtmlUnitDriver
at org.openqa.selenium.htmlunit.HtmlUnitDriver.findElementByXPath(HtmlUnitDriver.java:1161)
at org.openqa.selenium.By$ByXPath.findElement(By.java:361)
at org.openqa.selenium.htmlunit.HtmlUnitDriver$5.call(HtmlUnitDriver.java:1715)
at org.openqa.selenium.htmlunit.HtmlUnitDriver$5.call(HtmlUnitDriver.java:1)
at org.openqa.selenium.htmlunit.HtmlUnitDriver.implicitlyWaitFor(HtmlUnitDriver.java:1363)
at org.openqa.selenium.htmlunit.HtmlUnitDriver.findElement(HtmlUnitDriver.java:1711)
at org.openqa.selenium.htmlunit.HtmlUnitDriver.findElement(HtmlUnitDriver.java:606)
at seleniumtest.Test_Google.main(Test_Google.java:17)这是我的Firefox浏览器测试:
WebDriver driver = new FirefoxDriver();
driver.get("https://www.google.co.in/");
WebElement e =driver.findElement(By.xpath(".//*[@id='tsf']/div[2]/div[3]/center/input[1]"));
System.out.println("The current element is " + e.getAttribute("value"));这是我的HtmlUnit测试:
WebDriver driver = new HtmlUnitDriver(true);
driver.get("https://www.google.co.in/");
WebElement e =driver.findElement(By.xpath(".//*[@id='tsf']/div[2]/div[3]/center/input[1]"));
System.out.println("The current element is " + e.getAttribute("value"));我不认为它是重复的,因为在我的例子中没有涉及到javascript。我只想把一个简单的测试从火狐驱动移植到HTMLUnit。
发布于 2016-01-13 16:51:51
尝试应用一些时间,等待页面加载,然后尝试定位您的元素。这只是为了确保您的代码不会在页面仍在加载时执行。
尝试下面这样的方法,
WebDriver driver = new HtmlUnitDriver(true);
driver.get("https://www.google.co.in/");
Thread.sleep(5000);
WebElement e =driver.findElement(By.xpath(".//* [@id='tsf']/div[2]/div[3]/center/input[1]"));
System.out.println("The current element is " + e.getAttribute("value"));在执行下一行之前,Thread.sleep();将等待指定的时间。
检查一下,看看是否找到了您的元素。
和
还要检查您的网页或元素是否依赖于JavaScript。HTMLUnit驱动程序不能处理JavaScript,并且缺乏普通浏览器所具有的功能。
https://stackoverflow.com/questions/34741124
复制相似问题