Selenium WebDriver C#找不到FindElement的元素(by.xpath)

时间:2015-10-08 08:25:54

标签: c# selenium xpath selenium-webdriver

我正在尝试在下表中找到元素<td>!!mmm region test !!mmm region test</td>

...
<div class="panel-body cim-padding-zero">
   <div class="row">
      <div class="col-md-12">
         <section id="UserListArea">
            <div class="jtable-main-container">
               ...
<table class="jtable">
    ...
    <tbody>
        <tr class="jtable-data-row jtable-row-even Pointer" data-record-key="88">
            <td></td>
        </tr>
        <tr class="jtable-data-row Pointer" data-record-key="301">
            <td></td>
        </tr>
        <tr class="jtable-data-row jtable-row-even Pointer" data-record-key="302">
            <td></td>
        </tr>
        <tr class="jtable-data-row Pointer" data-record-key="524">
            <td></td>
        </tr>
        <tr class="jtable-data-row jtable-row-even Pointer jtable-row-selected" data-record-key="651">
            <td>!!mmm region test !!mmm region test</td>
        </tr>
        ...
    </tbody>
</table>        
...

我试过这个

IWebElement UserList = driver.FindElement(By.XPath("//section[@id='UserListArea']/div/table/tbody/tr[5]/td"));

和这个

IWebElement UserList = driver.FindElement(By.XPath("//*[@id='UserListArea']/div/table/tbody/tr[5]/td"));

但在每种情况下我都会收到错误

  

OpenQA.Selenium.NoSuchElementException:无法找到元素:   {&#34;方法&#34;:&#34;的xpath&#34;&#34;选择器&#34;:&#34; //部[@id =&#39; UserListArea&#39;] /格/表/ tbody的/ TR [5] / TD&#34;}

2 个答案:

答案 0 :(得分:0)

您可以在Xpath下面尝试: -

//*[@id='UserListArea']/div/table/tbody/tr[5]/td[contains(.,'!!mmm region test !!mmm region test')]

OR

//*[@id='UserListArea']//tr[@data-record-key='651']/td

OR

//tr[@class='jtable-data-row jtable-row-even Pointer jtable-row-selected']/td

如果仍有硒向您显示NoSuchElementException,请检查是否存在任何框架。如果帧在那里,那么你需要先切换到帧

答案 1 :(得分:0)

请下次更正确地编辑HTML。阅读

非常不舒服
//tr[@data-record-key="651"]/td

这对我有用