简体   繁体   English

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

[英]Selenium WebDriver C# not find element by FindElement(by.xpath)

I am trying to find the element <td>!!mmm region test !!mmm region test</td> in the table below. 我正在尝试在下表中找到元素<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>        
...

I have tried this 我已经试过了

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

and this 和这个

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

but in every case I get the error 但在每种情况下我都会得到错误

OpenQA.Selenium.NoSuchElementException: Unable to locate element: {"method":"xpath","selector":"//section[@id='UserListArea']/div/table/tbody/tr[5]/td"} OpenQA.Selenium.NoSuchElementException:无法找到元素:{“ method”:“ xpath”,“ selector”:“ // section [@ id ='UserListArea'] / div / table / tbody / tr [5] / td” }

You can try below Xpath:- 您可以在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

If still selenium show you NoSuchElementException then check that if there is any frame present. 如果仍然显示硒,则显示NoSuchElementException然后检查是否存在任何帧。 if frame is there then you need to switch to frame first 如果有框架,那么您需要先切换到框架

Please, next time edit your HTML more properly. 请下次再正确编辑您的HTML。 It is very uncomfortable to read 读起来很不舒服

//tr[@data-record-key="651"]/td

That works for me 这对我行得通

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM