0

我正在尝试将我的 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。我只想将一个简单的测试从 Firefox 驱动程序移植到 HTMLUnit。

4

1 回答 1

0

尝试稍微等待页面加载,然后尝试找到您的元素。这只是为了确保您的代码在页面仍在加载时没有执行。

试试下面的东西,

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,并且缺乏普通浏览器所具有的功能。

于 2016-01-13T08:51:51.157 回答