简体   繁体   中英

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.

...
<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"}

You can try below 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. if frame is there then you need to switch to frame first

Please, next time edit your HTML more properly. It is very uncomfortable to read

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

That works for me

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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