Not able to click Checkbox Element using Python Selenium integration

505 views
Skip to first unread message

Vipul Garg

unread,
Mar 11, 2020, 1:11:08 AM3/11/20
to Selenium Users


I am trying to iterate over pages having a Campaigns Programs List and then I have to select checkbox defined in an image below in order to get the run length of a loop on-page.

But when I am trying to navigate over pages via the next button it will stop clicking on checkboxes after one or 2 pages and throws me below error.

I have tried with xpath, css selector and id method But all get failed.




Line of Code Used-

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




Error Stack Traces:-

 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)

CheckBoxClick.png



Disclaimer:- This email and any files transmitted with it are confidential and intended to be received only by individual or entity to whom they are addressed. If you are not the intended recipient, you are hereby cautioned that any dissemination, disclosure, copying, distribution or taking any action in reliance on the contents of this message or any accompanying attachment is strictly prohibited and is unlawful, Please notify the sender immediately if you have received this email in error then delete this email from your system.

Josh Abrahamsen

unread,
Mar 11, 2020, 11:45:22 PM3/11/20
to Selenium Users
Vipul,

Does the page have any busy loading indicator you could wait for visibility of and then invisibility of? Basically what is happening here is the page takes longer to load the next checkbox then Selenium is waiting for. By the time the next button is clicked it's already seeing the same checkbox and then attempting to click it while the next one has loaded.

The reason for this is because the element you are referencing is removed from the DOM.

I've gotten around this by waiting for the busy loading indicator to be visible, then invisible, and then clicking on the element.

Other fixes I'm seeing would be something like this to reference the element again upon getting the exception:
try {
    checkboxClicked = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "#workflow_table > thead > tr > th.first.checkbox-column.checkbox > input")));
    checkboxClicked.click();
} catch(org.openqa.selenium.StaleElementReferenceException ex) { checkboxClicked = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "#workflow_table > thead > tr > th.first.checkbox-column.checkbox > input")));
   checkboxClicked.click();
}
Reply all
Reply to author
Forward
0 new messages