2014-01-26 108 views
0

我想用Selenium在Java中創建ajax等待方法來獲取配置定義的許多結果。Selenium xpath語法不正確

這裏是我的方法:

/** 
    * Wait for WAIT seconds for AJAX data to be loaded. 
    * @throws Exception - if thread is interrupted 
    */ 
    public List<WebElement> waitAjaxLoad(By by) throws Exception{ 
     int ajaxResultsWait = Integer.parseInt(P.getProperty("ajax.results.wait")); 
     List<WebElement> resultsLoaded = null; 

     do{ 
      resultsLoaded = driver.findElements(by); 
     }while(resultsLoaded.size() < ajaxResultsWait); 
     return resultsLoaded; 
    } 

這裏是我的電話:

List<WebElement> videoElements = waitAjaxLoad(By.xpath("//div[@id='pageTv']/div[@id='pageTvScroller')]/div[@class='boxInner']/ul/li")); 

我得到這個異常:

org.openqa.selenium.InvalidSelectorException: The given selector //div[@id='pageTv']/div[@id='pageTvScroller')]/div[@class='boxInner']/ul/li is either invalid or does not result in a WebElement. The following error occurred: InvalidSelectorError: Unable to locate an element with the xpath expression //div[@id='pageTv']/div[@id='pageTvScroller')]/div[@class='boxInner']/ul/li because of the following error: [Exception... "The expression is not a legal expression." code: "12" nsresult: "0x805b0033 (SyntaxError)" location: ""] Command duration or timeout: 20 milliseconds

我應該wory約XPath表達式不是snytax正確,否則Selenium無法找到該WebElement?此異常曖昧我....

回答

0

它是一種簡單的打字錯誤,不必要的輪支架div[@id='pageTvScroller')]增加,因此更換

//div[@id='pageTv']/div[@id='pageTvScroller')]/div[@class='boxInner']/ul/li 

//div[@id='pageTv']/div[@id='pageTvScroller']/div[@class='boxInner']/ul/li 
+0

說實話......我看了5遍以上,仍然沒有吸引我的目光......)謝謝你...... – Zec

+0

很高興幫助:) – Amith