iamthebull
iamthebull

Reputation: 109

selenium - python - fetch the table row containing element

I am fetching elements in the table:

<tbody id="actiontable-tbody">
<tr>
    <td class="letterbreak first">default</td>       <!-- first fetch this element -->
    <td class="letterbreak">Internal</td>
    <td class="action-btn-cell" id="id9c">
</tr>
<tr>
    <td class="letterbreak first odd">opcua-module</td>
    <td class="letterbreak odd">Internal</td>
    <td class="action-btn-cell" id="id9d">
    </td>
</tr>
</tbody>

I first fetch an element in one of the rows:

element = driver.find_element(By.XPATH,"//*[text()='default']")

Now I want to fetch the row containing that element. I tried:

row = element.parent

But that is returning a webDriver object. How do I fetch the row with respect to the element in that row?

Edit: The ultimate goal is to then fetch the "action-btn-cell" element in that same row. So I was trying to go up a node and fetch the row node to then fetch the "action-btn-cell". Once I fetch the first element can I then fetch it's siblings? The fetch needs to be done starting with the first fetched element, either by going up to the row node or fetching the siblings, not by looping through all of the rows (it's a limitation by how the algorithm is implemented).

Upvotes: 0

Views: 2530

Answers (2)

KunduK
KunduK

Reputation: 33384

To identify the element <td class="action-btn-cell" id="id9c"> use any of the follwoing xpath.

option 1: use following-sibling

//*[text()='default']/following-sibling::td[@class='action-btn-cell']

OR

//*[text()='default']/following-sibling::td[2]

option 2: identify parent and then following child.

//tr[.//*[text()='default']]/td[@class='action-btn-cell']

OR

//tr[.//*[text()='default']]/td[2]

Example:

driver.find_element_by_xpath("//tr[.//*[text()='default']]/td[@class='action-btn-cell']")

Upvotes: 2

undetected Selenium
undetected Selenium

Reputation: 193308

To extract and print the texts belonging to the same <tr> that contains the text default using Selenium and you can use either of the following Locator Strategies:

  • Using xpath and text attribute:

    print([my_elem.text for my_elem in driver.find_elements_by_xpath("//td[text()='default']/..")])
    
  • Using xpath, parent and text attribute:

    print([my_elem.text for my_elem in driver.find_elements_by_xpath("//td[text()='default']//parent::tr[1]")])
    
  • Using xpath, ancestor and text attribute:

    print([my_elem.text for my_elem in driver.find_elements_by_xpath("//td[text()='default']//ancestor::tr[1]")])
    

Ideally you need to induce WebDriverWait for visibility_of_all_elements_located() and you can use either of the following Locator Strategies:

  • Using xpath and text attribute:

    print([my_elem.text for my_elem in WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "//td[text()='default']//parent::tr[1]")))])
    
  • Using xpath, parent and text attribute:

    print([my_elem.text for my_elem in WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "//td[text()='default']//parent::tr[1]")))])
    
  • Using xpath, ancestor and text attribute:

    print([my_elem.text for my_elem in WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "//td[text()='default']//ancestor::tr[1]")))])
    

Upvotes: 0

Related Questions