0

I have a condition where tr row which generates dynamic value:

<tbody>
   <tr id="24686" tabindex="0">
     <td class="nowrap xh-highlight" style="padding: 3px 8px;">Available</td>
   </tr>
</tbody>

I have Xpath 1: (//tbody/tr/td[contains(text(),'Available')])[1] which returns

Available 

and Xpath 2: //tr[1]/@id which returns

ld_9050427
22707

The condition is that I want to generate one xpath which will return first number whose status is Available and then return its ID. Later on I want to use this same id to carry on later process?

I tried something like below but it didn't work

(//tbody/tr[/@id and/td[contains(text(),'Disponible')]])[1]
  • What is your exact desired output? Do you want to select table row by `@id` and the text value of child `td`? – Andersson Dec 05 '18 at 12:58
  • @Andersson Id is something that is generated dynamically and I have three status Available, In Progress, Not Available. I want to select first unit that has status Available and then fetch its dynamic ID which is present in tr row. – Pranjal Prakash Srivastava Dec 05 '18 at 12:59

2 Answers2

2

If you want to select tr that has id attribute (any) and table cell with text "Available" try

//tr[@id and td='Available']

to extract id value for further use you need get_attribute/getAttribute method

Andersson
  • 51,635
  • 17
  • 77
  • 129
0

To find the first number whose status is Available and then return its ID you can use the following solution:

  • xpath:

    "//tbody//tr//td[text()='Available']/.."
    

Note 1: The .. in the xpath refers to the ancestor node

Note 2: As you are looking for the first match with the implemented condition, you have use either:

  • Python:

    find_element_by_xpath()
    
  • Java:

    findElement()
    
  • C#:

    FindElement()
    

Note 3: Finally you have to use getAttribute("id") / get_attribute("id") to extract the value of the id attribute as follows:

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • I am looking for something like this (//tbody/tr[/@id and/td[contains(text(),'Availabel')]])[1] – Pranjal Prakash Srivastava Dec 05 '18 at 13:03
  • @PranjalPrakashSrivastava As you mentioned _...and then return its ID..._ so your end target is the `` from where you have to extract the ID e.g. **24686**. Then why would you at all trying to land within the ``? – undetected Selenium Dec 05 '18 at 13:07
  • is the child which has different status. There are three status Available, In Progress and Not Available. So I want to select first unit whose status is Available and then fetch its id so that I can use its ID for further use. – Pranjal Prakash Srivastava Dec 05 '18 at 13:09
  • That is what I spoke about in my ans. However you never mentioned the _language binding_ you are using. You have to use `find_element_by_xpath()` – undetected Selenium Dec 05 '18 at 13:12