相同的元素在迭代过程中变得不可点击

Same Element becomes unclickable during iteration

我正在使用 Selenium Python 为不同的用户执行相同的操作,流程如下,

1-网页显示用户id列表 2- 我搜索 1 个用户 ID(迭代) 3- 我点击用户 ID 旁边的复选框 4- 我点击完成

我的问题是复选框,它在迭代的第一行是可点击的,但在下一行就变得不可点击了。

复选框元素:

<input type="checkbox" class="ant-checkbox-input" value="" xpath="1">

我也试过 ActionChains 但我得到了错误:stale element reference: element is not attached to the page document

谢谢

输入复选框可能处于禁用状态,或者某些 javascript 在复选框变为可用之前尚未加载。

试试这个代码:

from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
WebDriverWait(driver, 15).until(EC.element_to_be_clickable((By.XPATH,"//tbody/tr[1]/td[1]/span[1]/label[1]/span[1]/input[1]"))).click()

另一个问题可能是 xpath 根据您登录的用户而变化。

from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
WebDriverWait(driver, 15).until(EC.element_to_be_clickable((By.XPATH,'//input [@type="checkbox"]'))).click()

以上代码仅在您有 1 个复选框时才有效。

此代码将尝试单击页面上显示的所有复选框,如果可行,我们可以缩小 XPath 的范围。

els=driver.find_elements_by_xpath('//input [@type="checkbox"]')
for el in els:
 try:
  el.click()
 except:
  pass