Aesir
Aesir

Reputation: 2473

Unable to find html element using selenium

I am using selenium to test my webapp with chromedriver however selenium doesn't seem able to find the element which I want.

The html I am querying is:

<table>
   <thead><thead>
   <tbody>
     <tr>
       <td></td>
       <td>
          <span class="class1">
            <span>Test text</span>
          </span>
          <ul>
            <li class="class2"><a href="#">Edit</a></li>
            <li class="class2"><a href="#">Remove</a></li>
         </ul>
       </td>
     </tr>
   </tbody>
</table>

I am trying to select the web element:

<a href="#">Edit</a>

I have tried the following using CSS selectors:

table > tbody > tr:first-child > td:nth-child(2) > ul > li:first-child > a

and the same sort of query using XPath:

//table/tbody/tr/td/following-sibling::td[1]/ul/li/a

When I debug this and use a quickwatch to dynamically modify the query both queries work up until I try and select the ul; for example:

table > tbody > tr:first-child > td:nth-child(2)

AND

//table/tbody/tr/td/following-sibling::td[1]

Both correctly select the contents of the correct td containing the ul. When I try to make either of them get the ul, each of the selections becomes null.

So I have two problems, why am I not able to select the ul element using either CSS selectors or XPath and is there another technique I can use to select the first tag in the list with text "Edit".

Cheers

Upvotes: 1

Views: 3196

Answers (4)

Misha Akovantsev
Misha Akovantsev

Reputation: 1825

'//table//li/a[text()="Edit"]'

Here is the ipython session example:

In [1]: from lxml import etree

In [2]: html = """
   ...: <table>
   ...:    <thead><thead>
   ...:    <tbody>
   ...:      <tr>
   ...:        <td></td>
   ...:        <td>
   ...:           <span class="class1">
   ...:             <span>Test text</span>
   ...:           </span>
   ...:           <ul>
   ...:             <li class="class2"><a href="#">Edit</a></li>
   ...:             <li class="class2"><a href="#">Remove</a></li>
   ...:          </ul>
   ...:        </td>
   ...:      </tr>
   ...:    </tbody>
   ...: </table>
   ...: """

In [3]: t = etree.fromstring(html, parser=etree.HTMLParser())

In [4]: t.xpath('//table//li/a[text()="Edit"]')
Out[4]: [<Element a at 101c83b50>]

In [5]: a = t.xpath('//table//li/a[text()="Edit"]')[0]

In [6]: a.text
Out[6]: 'Edit'

In [7]: a.attrib
Out[7]: {'href': '#'}

Upvotes: 0

ezkl
ezkl

Reputation: 3851

If you're more comfortable with CSS selectors, check out: table tbody tr:first-child ul li:contains('Edit') a

Upvotes: 0

faramka
faramka

Reputation: 2234

Try

//table/tbody/tr/td[2]/ul/li[1]/a

or

//a[text()='Edit']

(if there will not be more links with this text on current page)

Upvotes: 2

BoltClock
BoltClock

Reputation: 723598

Not really sure why your selector would break after you add ul, but try this CSS selector as an alternative:

table > tbody > tr:first-child > td:nth-child(2) li.class2:first-child > a

Or this equivalent XPath expression:

//table/tbody/tr/td[2]//li[@class="class2"][1]/a

Upvotes: 1

Related Questions