Ben Ben - 1 year ago 104
Python Question

Selenium IDE / Xpath - How do I select an element inside a table cell when another given element is NOT present?

This is my first big test automation project and first ever SO post, so please excuse me.

I am using Selenium IDE to test the application. I have a table row with a couple of different tds, like so:

<!--Table cell I DO NOT want to select-->
<td class="inline-edit">
<a class="edit" href="#" onclick="someFunction();">
<i class="fa fa-pencil"></i>

<a href="Some_link_i_need_to_not_select">TS1</a>

<sup class="r_f3">P</sup>

<!--Table cell I want to select-->
<td class="inline-edit">
<a class="edit" href="#" onclick="someFunction();">

What I am trying to do is select the link with the class "edit", but only if the td that it's in does NOT also contain another link or the date text. My thinking was to write an xpath, but I have been unable to write one with the conditions I need that actually locates the element (very possibly due to lack of knowledge).

Once I export the test suite as Python code, I should be able to write some logic to look for the element, but for now I need to get it working in Selenium IDE. What would be a reliable way to select the first link only if the second link (with the TS1 text) is not present?

Thanks in advance, and please let me know if I should edit my question in any particular way. As I mentioned, this is my first one.

Answer Source

The following XPath expression

//tr/td[count(.//a) = 1]//a[@class='edit']

will select all a elements of class edit in all td elements containing only a single a element. When applied to your sample document, the result is:

<a class="edit" href="#" onclick="someFunction();"/>
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download