Diego Ruiz
Diego Ruiz

Reputation: 321

Find superior element in html with Selenium

I have the following html code:

<tr id="id that I want to obtain via title">
   <td class="icon">
       <span class="greenArrowIcon pid-1-arrowSmall"></span>
   </td>
   <td class="bold left noWrap elp">
       <a href="..." title="title that I have obtained">
           TITLE
       </a>
       <MORE CODE>
   </td>
</tr>

and I know that the tag title title="title that I have obtained" are always the same, but the id id="id that I want to obtain viva title" could change, is strange that changes, but could. So, my question is: How can I find the id via the title ? I think the problem is that the title tag is inside (an inferior jerarchy) from the id that I want to solve it.

I am using Selenium, and this is the code to solve the title and get the web element:

driver.find_element_by_css_selector("[title^='title that I have obtained']")

Is it possible do this?

Upvotes: 1

Views: 82

Answers (2)

Nandan A
Nandan A

Reputation: 2922

Already @Prophet answered in a good way but I want to show you other ways to find the same element.

Using parent function:

 //a[@title='title that I have obtained']//parent::td//..
 //a[@title='title that I have obtained']//..//parent::tr

Using ancestor function:

//a[@title='title that I have obtained']//ancestor::tr

Upvotes: 0

Prophet
Prophet

Reputation: 33361

To find the desired id attribute value you can use the following XPath locator:

tr = driver.find_element_by_xpath("//tr[.//a[@title='title that I have obtained']]")
id = tr.get_attribute("id")

Upvotes: 1

Related Questions