1

I can find the textA, but I need to locate the checkbox in that row. So my idea was to find the tr for that textA and then search for element checkbox under that tr. Can anyone help me with articulating a XPath query for this?

  <tbody class = "table">
        <tr class ="rows">
            <td class= "colmn">
                <div class = "textA">
            <td class= "colmn">
                <div class = "image">   
            <td class= "colmn">
                <div class = "drop down">
            <td class= "colmn">
                <div class = "checkbox">
        <tr class ="rows">
            <td class= "colmn">
                <div class = "textC">
            <td class= "colmn">
                <div class = "image">   
            <td class= "colmn">
                <div class = "drop down">
            <td class= "colmn">
                <div class = "checkbox">
kjhughes
  • 106,133
  • 27
  • 181
  • 240
  • Why don't you just use `debug view` in your browser and copy `xpath`? https://stackoverflow.com/questions/3030487/is-there-a-way-to-get-the-xpath-in-google-chrome – user1767754 Nov 28 '17 at 16:53

2 Answers2

1

You can try this :- //div[@class="textA"]/ancestor::tr//*[@class="checkbox"]

Ankur Singh
  • 1,239
  • 1
  • 8
  • 18
  • This worked! I had to change the div to td. I had actual text instead of class... so i just replaced that! Thank you! checkBox = selenium_driver.find_element(By.XPATH,'//td[text()="tango"]/ancestor::tr//*[@role="checkbox"]') – Riz Hossain Nov 28 '17 at 18:54
  • Question Ankur: why does this work: checkBox = selenium_driver.find_element(By.XPATH,'//*[text()="tango"]/ancestor::tr//*[@role="checkbox"]') and this does not. It clicks the first checkbox in the table: checkBox = selenium_driver.find_element(By.XPATH,'//*[text()="tango"]/ancestor::*//*[@role="checkbox"]') – Riz Hossain Nov 28 '17 at 19:06
  • checkBox = selenium_driver.find_element(By.XPATH,'//*[text()="tango"]/a‌​ncestor::tr//*[@role‌​="checkbox"]') - In this it will directly look after tr tag which contains role =checkbox. checkBox = selenium_driver.find_element(By.XPATH,'//*[text()="tango"]/a‌​ncestor::*//*[@role=‌​"checkbox"]') - in this xpath it will reach to ancestor and using * means you can have n number of parent element which may not work some time – Ankur Singh Nov 29 '17 at 07:14
1

This XPath,

//tr[td/div/@class='textA']/td/div[@class='checkbox']

will select the div with class='checkbox' within the tr that has a td/div with class='textA', as requested.

kjhughes
  • 106,133
  • 27
  • 181
  • 240