2012-04-21 46 views
0

我需要點擊通過以下XPath指向的鏈接:HTML /體/格[2]/UL /鋰[9]/A(使用firepath產生)相應的HTML snippes是如下: < A HREF = 「/ ABB /註銷」>註銷代碼適用於FirefoxDriver但不是在HtmlUnitDriver

我的代碼是這樣的:

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) 
+1

啓用javaScript因此解決了我的問題。但面對新的「FailingHttpStatusCodeException - 太多的重定向..」,爲狀態代碼302捕捉錯誤很重要,這是否有意義? FirefoxDriver再次沒有這樣的問題,但HtmlUnit正面臨着這個問題。我感到失落...請幫助.. – Tirtha 2012-04-23 12:42:48

回答

0

HtmlUnitDriver是假的瀏覽器,它不能處理JavaScript和其正常的瀏覽器具備的功能缺乏。 FirefoxDriver或ChromeDriver使用真實的瀏覽器。

0

// HTML /體/格[2]/UL /鋰[9]/A - 絕對的XPath。

不建議使用絕對XPath。使用相對XPath,以便它可以在所有瀏覽器中工作。

謝謝!

相關問題