使用Xpath(selenium web驱动程序)的无效选择器异常

时间:2013-11-21 13:43:00

标签: xpath selenium

我正在使用Xpathchecker FF插件获取XPath表达式,但是当我运行此代码时:

private void srch() throws MalformedURLException, IOException 

{

  driver.get(TestURL);
  WebElement input1 = driver.findElement(By.id("login_form_user_input"));  
  input1.sendKeys("guest");            
  WebElement input2 = driver.findElement(By.id("login_form_password_input"));
  input2.sendKeys("guest");
  WebElement btn = driver.findElement(By.id("login_form_signin_button"));
  btn.click();
  WebElement w1 = driver.findElement(By.id("header_search_text_field"));
  w1.sendKeys("tsk");
  //WebElement resulttable = driver.findElement(By.id("search_results_table"));
 //System.out.println(resulttable.getSize());
    List <WebElement> rows =  driver.findElements(By.xpath("id('search_results_table')/x:tbody/x:tr"));
         System.out.println(rows.size());


}

我收到以下错误:

The given selector id('search_results_table')/x:tbody/x:tr is either invalid or does not result in a WebElement. The following error occurred:
InvalidSelectorError: Unable to locate an element with the xpath expression id('search_results_table')/x:tbody/x:tr because of the following error:
[Exception... "An attempt was made to create or change an object in a way which is incorrect with regard to namespaces"  code: "14" nsresult: "0x8053000e (NamespaceError)"  location: "file:///C:/Users/orsyp/AppData/Local/Temp/anonymous8101865731711582627webdriver-profile/extensions/fxdriver@googlecode.com/components/driver_component.js Line: 5916"]
Command duration or timeout: 109 milliseconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/invalid_selector_exception.html
Build info: version: '2.35.0', revision: 'c916b9d', time: '2013-08-12 15:42:01'
System info: os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.7.0_40'
Session ID: 9cbc470a-c211-46ce-8bb1-ccdb9c66ff1e

这是我的html代码,因为你可以看到这是一个动态表:

<table id="search_results_table" class="table table-striped table-hover table-condensed  table-bordered">
   <tbody>
      <tr class="ng-scope" ng-repeat="object in objects">
          <td>
             <a href="#/en/object?company=WAR001&area=X&mu=BIST_CE691&name=TSK(BEN701JU50)(000)">
            <p style="font-size:11px">
         </td>
     </tr>
     <tr class="ng-scope" ng-repeat="object in objects">
     <tr class="ng-scope" ng-repeat="object in objects">
     <tr class="ng-scope" ng-repeat="object in objects">
     <tr class="ng-scope" ng-repeat="object in objects">
     <tr class="ng-scope" ng-repeat="object in objects">
     <tr class="ng-scope" ng-repeat="object in objects">
     <tr class="ng-scope" ng-repeat="object in objects">
     </tbody>

1 个答案:

答案 0 :(得分:2)

假设您想要tbody中的所有行,则xpath应为:

//table[@id='search_results_table']/tbody/tr