[英]Click button in a table on a specific row in Python Selenium
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 .如果同一行上的文本在<td class="user_type">
real 中包含文本“ 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>
您可以尝试以下 xpath:
//tr//td[@class="user_type" and text()="real"]//following-sibling::td//button[text()="Delete"]
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 xpath based Locator Strategies :要在同一行包含文本real时单击带有文本作为Delete的元素,您必须为element_to_be_clickable()
引入WebDriverWait并且您可以使用以下任一基于xpath的Locator Strategies :
Using XPATH
and following-sibling with class attribute:使用XPATH
和具有类属性的跟随兄弟:
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:使用XPATH
和带有索引的跟随兄弟:
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.