MePunekar
MePunekar

Reputation: 155

Traversing parent-child nodes with xpath using selenium webdriver

I have an html similar to following

<table>
<tr>
    <td class='1'>
        <div>
            <a class='abc'> element1 </a>
        </div>
    </td>
    <td class='2'></td>
    <td class='3'>
        <img id='generated id' class='xyz' />
    </td>
</tr>
</table>

I have element1 coming as a parameter to a function. When I get element1, I need to traverse to img tag and click on it.

I tried the xpath selector

WebElement e = driver.getelementbyXpath("//a[contains(@class,'abc')
and text()='element1')"]);

WebElement e2 = e1.findelementbyxpath("../..//img[contains(@id,'something') 
and contains(@class='xyz')]");

This does not work.

Upvotes: 1

Views: 4368

Answers (1)

Need4Steed
Need4Steed

Reputation: 2180

Your "element1" text node has space paddings at both sides. "//a[contains(@class,'abc') and text()=' element1 ')" would've worked ;)

Besides, you used too much contains, which I believe is quite inefficient comparing to [@class='abc'] [@id='blah..'].

Upvotes: 1

Related Questions