Home > Back-end >  Find superior element in html with Selenium
Find superior element in html with Selenium

Time:10-11

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?

CodePudding user response:

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")
  • Related