0

I am using Selenium to help me collect data from a web page. This page has two dropdowns, which change a list of links to PDFs which I then download. My code runs fine in a notebook but does not work properly in a .py file called from the cmd. It sends the following error:

StaleElementReferenceException: Message: stale element reference: element is not attached to the page document

Below, I leave a simplified version of my code:

driver.get(url)

try:
    leg_selector_elem = WebDriverWait(driver, 3).until(
        EC.presence_of_element_located((By.NAME, selector_name,))
    )
finally:
    leg_select = Select(leg_selector_elem)
    for option in leg_select.options:
        leg = option.text.split()[0]
        if leg == args.leg:
            leg_select.select_by_visible_text(option.text)
            driver.implicitly_wait(3)
            break

try:
    session_selector_elem = WebDriverWait(driver, 3).until(
        EC.presence_of_element_located((By.NAME, session_selector_name,))
    )
finally:
    session_select = Select(session_selector_elem)
    for option in session_select.options:
        selected_session = option.text.split()[0]
        selected_session = selected_session.split(".")[0]
        if selected_session == args.session:
            session_select.select_by_visible_text(option.text)
            driver.implicitly_wait(3)
            break

download_session_pdfs(driver, args.leg, selected_session, save_path)

driver.implicitly_wait(3) does not work.

Thanks for any help you can provide.

2
  • The problem is that in notebook there is time to process between cell execution, but in cmd there isn't. If I use time.sleep() it works, but what should I do differently to properly wait for the DOM elements to be collected? Jun 2 at 14:27
  • Can you point where exactly the error happens? Most probably you gonna need to use Explicit Waits rather than implicit ones. Jun 3 at 12:15

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.