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]
2
  • What is your exact desired output? Do you want to select table row by @id and the text value of child td? Commented Dec 5, 2018 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. Commented Dec 5, 2018 at 12:59

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

Comments

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:

4 Comments

I am looking for something like this (//tbody/tr[/@id and/td[contains(text(),'Availabel')]])[1]
@PranjalPrakashSrivastava As you mentioned ...and then return its ID... so your end target is the <tr> from where you have to extract the ID e.g. 24686. Then why would you at all trying to land within the <td>?
<td> 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.
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()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.