无法使用Python Selenium集成单击复选框元素

2024-04-23 06:28:12 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图在有程序列表的页面上进行迭代,然后我必须选择下图中定义的复选框,以获得页面上循环的运行长度

但当我试图通过“下一步”按钮浏览页面时,它将停止单击复选框,并将我抛出错误下方

我尝试过xpath、css选择器和id方法,但都失败了

DOM element SS along with checkbox highlighted

使用的代码行-

CheckBoxclick = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "#workflow_table > thead > tr > th.first.checkbox-column.checkbox > input"))).click()

错误:-

   File "D:/Vipul Garg Backup/XXXX/TestingPardotExportProgramsData.py", line 74, in <module>
    ProgramsPathlist = getDataForPrograms(RecordsonOnePage,totalnofpages)
  File "D:/Vipul Garg Backup/XXXX/TestingPardotExportProgramsData.py", line 63, in getDataForPrograms
    RecordsonOnePage = getrecordsoNpage()
  File "D:/Vipul Garg Backup/XXXX/TestingPardotExportProgramsData.py", line 42, in getrecordsoNpage
    CheckBoxclick = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "#workflow_table > thead > tr > th.first.checkbox-column.checkbox > input"))).click()
  File "C:\Users\Vipul.Garg\AppData\Local\Programs\Python\Python37\lib\site-packages\selenium\webdriver\remote\webelement.py", line 80, in click
    self._execute(Command.CLICK_ELEMENT)
  File "C:\Users\Vipul.Garg\AppData\Local\Programs\Python\Python37\lib\site-packages\selenium\webdriver\remote\webelement.py", line 633, in _execute
    return self._parent.execute(command, params)
  File "C:\Users\Vipul.Garg\AppData\Local\Programs\Python\Python37\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "C:\Users\Vipul.Garg\AppData\Local\Programs\Python\Python37\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: element is not attached to the page document
  (Session info: chrome=80.0.3987.122)

但上面的代码行正在中间工作,并抛出如上所述的错误。
关于这行代码替换的任何建议,以及我为什么会遇到如此不一致的情况


Tags: inpylocalseleniumlineelementusersappdata
1条回答
网友
1楼 · 发布于 2024-04-23 06:28:12

您正在使用表的ID,并且复选框位于该表内,所以您需要传递复选框的定位器

由于没有为复选框定义id,因此使用xpath访问定位符,如-//input[@type='checkbox']

相关问题 更多 >