Selenium webdriver 选择一个单选按钮

Selenium webdriver 选择一个单选按钮,selenium-webdriver,xpath,Selenium Webdriver,Xpath,我正在编写python selenium代码,尝试单击单选按钮。以下是html代码: <tbody> <tr class="row-event"> <td class="selection-cell"><input type="radio" class=""></td> <td>812589</td>

我正在编写python selenium代码,尝试单击单选按钮。以下是html代码:

<tbody>
   <tr class="row-event">
      <td class="selection-cell"><input type="radio" class=""></td>
      <td>812589</td>
      <td>john</td>
   </tr>
   <tr class="row-event">
      <td class="selection-cell"><input type="radio" class=""></td>
      <td>909720</td>
      <td>bob</td>
   </tr>
</tbody>

您可以使用以下XPath:

//td[.='812589']/preceding-sibling::td[1]/input
我们寻找一个
input
元素,它是包含
812589
td
元素的第一个
td
前兄弟的子元素

进口:

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
代码:

WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//td[.='812589']/preceding-sibling::td[1]/input"))).click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//td[.='812589']/preceding-sibling::td[1]/input"))).click()