首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将Selenium测试从Firefox驱动移植到HTMLUnit驱动

将Selenium测试从Firefox驱动移植到HTMLUnit驱动
EN

Stack Overflow用户
提问于 2016-01-12 18:27:08
回答 1查看 793关注 0票数 0

我正在尝试将selenium测试从Firefox浏览器转换为HTMLUnit驱动程序。但是,当我尝试运行HTMLUnit测试时,它给出了XPATH错误。Firefox浏览器测试运行得非常好。

我的应用程序测试套件广泛使用XPATH。因此,我有意尝试使用XPATH。

我已经尝试过使用

代码语言:javascript
复制
new WebDriverWait(driver, 10)).until(ExpectedConditions.presenceOfElementLocated

但是我还是得到了同样的错误。

这是错误:

代码语言:javascript
复制
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浏览器测试:

代码语言:javascript
复制
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测试:

代码语言:javascript
复制
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。

EN

回答 1

Stack Overflow用户

发布于 2016-01-13 16:51:51

尝试应用一些时间,等待页面加载,然后尝试定位您的元素。这只是为了确保您的代码不会在页面仍在加载时执行。

尝试下面这样的方法,

代码语言:javascript
复制
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,并且缺乏普通浏览器所具有的功能。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34741124

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档