我需要点击以下xPath指向的链接:html/body/div[2]/ul/li[9]/a(使用firepath生成)相应的Html片段如下: <a href="/abb/注销">注销
我的代码是这样的:
HtmlUnitDriver driver= new HtmlUnitDriver(BrowserVersion.FIREFOX_3_6);
WebElement logoffElement = (new WebDriverWait(driver, 10))
.until(new ExpectedCondition<WebElement>(){
@Override
public WebElement apply(WebDriver d) {
return d.findElement(By.xpath("html/body/div[2]/ul/li[9]/a"));
}});
logoffElement.click();
上面的代码适用于 Firefoxdriver,但不适用于 HtmlUnitdriver。HtmlUnitdriver 给出以下错误:
Caused by: org.openqa.selenium.NoSuchElementException: Unable to locate a node using html/body/div[2]/ul/li[9]/a
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '2.21.0', revision: '16552', time: '2012-04-11 19:08:38'
System info: os.name: 'Windows XP', os.arch: 'x86', os.version: '5.1', java.version: '1.6.0_23'
Driver info: driver.version: HtmlUnitDriver
at org.openqa.selenium.htmlunit.HtmlUnitDriver.findElementByXPath(HtmlUnitDriver.java:802)
at org.openqa.selenium.By$ByXPath.findElement(By.java:344)
at org.openqa.selenium.htmlunit.HtmlUnitDriver$5.call(HtmlUnitDriver.java:1244)
at org.openqa.selenium.htmlunit.HtmlUnitDriver$5.call(HtmlUnitDriver.java:1)
at org.openqa.selenium.htmlunit.HtmlUnitDriver.implicitlyWaitFor(HtmlUnitDriver.java:984)
at org.openqa.selenium.htmlunit.HtmlUnitDriver.findElement(HtmlUnitDriver.java:1241)
at org.openqa.selenium.htmlunit.HtmlUnitDriver.findElement(HtmlUnitDriver.java:396)
at com.nike.automation.Task$1.apply(Task.java:70)
at com.nike.automation.Task$1.apply(Task.java:1)
at org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:201)