2

I have a page with a table and few rows where some rows have same texts as the other rows.

I want to click on a button "Delete" if text on the same row contains the text "real" in <td class="user_type">real .

<tr id="5gf5h5gh4rthhgh1" data-id="1418753">
    <td class="website">www.123.com</td>
    <td class="user_type">real</td>
    <td class="ip_address">123.123.1.1 ()</td>
    <td class="actions">
        <div class="list-action">   
            <button class="btn default check check green markAsChecked" type="button">
                <i class="fa fa-check green markAsChecked"></i>Delete</button></div></td>
</tr>

<tr id="5gf5h5gh4g5j1gh4" data-id="1418753">
    <td class="website">www.123.com</td>
    <td class="user_type">virtual</td>
    <td class="ip_address">88.123.2.2 ()</td>
    <td class="actions">
        <div class="list-action">   
            <button class="btn default check check green markAsChecked" type="button">
                <i class="fa fa-check green markAsChecked"></i>Delete</button></div></td>
</tr>
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
bbfl
  • 277
  • 1
  • 2
  • 16

2 Answers2

2

You can try following xpath:

//tr//td[@class="user_type" and text()="real"]//following-sibling::td//button[text()="Delete"]
frianH
  • 7,295
  • 6
  • 20
  • 45
1

To click on the element with text as Delete when the same row contains the text real you have to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following based Locator Strategies:

  • Using XPATH and following-sibling with class attribute:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//td[@class='user_type' and text()='real']//following-sibling::td[@class='actions']//button[contains(., 'Delete')]"))).click()
    
  • Using XPATH and following-sibling with index:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//td[@class='user_type' and text()='real']//following-sibling::td[2]//button[contains(., 'Delete')]"))).click()
    
  • Note : You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352