0

I'm trying to use Selenium with Python to submit a form on a webpage. The form has an input field and a submit button that I'm trying to locate with find_element() and By.NAME and By.XPATH.

I can successfully enter text into the input field and click the submit button. However, after submitting the form and being redirected to a new page, I keep getting the following error:

selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: element is not attached to the page document

From my understanding, this error occurs when the element that I'm trying to interact with no longer exists on the page. However, I'm confused as to why this is happening since I've already successfully clicked the submit button and the form was submitted.

Here's the relevant code:

import time
from selenium import webdriver
from selenium.webdriver.common.by import By

# open browser and go to webpage
driver = webdriver.Chrome()
driver.get('https://twitter.com/account/begin_password_reset')

# read usernames from txt file
with open('usernames.txt', 'r') as file:
    usernames = file.readlines()

# find input field and submit button
input_field = driver.find_element(By.NAME,'account_identifier')
submit_button = driver.find_element(By.XPATH,"//input[@type='submit' and @value='Search']")

# iterate over usernames and submit form
for username in usernames:
    # input username and submit form
    input_field.clear()
    input_field.send_keys(username)
    submit_button.click()

    # wait for form to submit
    time.sleep(10)

    # check if radio button exists and get label value
    try:
        label = driver.find_element_by_css_selector('input[name="method"][checked] + label')
        text = label.get_attribute('textContent')
        print(text)
    except:
        pass

# close browser
driver.quit()

Can anyone explain why I'm still getting this error even though the submit button was successfully clicked and the form was submitted? And is there a way to fix this error?

Thank you in advance for your help!

Edit (for the complete error log):

Traceback (most recent call last):
  File "C:\Users\admin\AppData\Local\Programs\Python\Python311\tryreset.py", line 22, in <module>
    submit_button.click()
  File "C:\Users\admin\AppData\Local\Programs\Python\Python311\Lib\site-packages\selenium\webdriver\remote\webelement.py", line 93, in click
    self._execute(Command.CLICK_ELEMENT)
  File "C:\Users\admin\AppData\Local\Programs\Python\Python311\Lib\site-packages\selenium\webdriver\remote\webelement.py", line 410, in _execute
    return self._parent.execute(command, params)
  File "C:\Users\admin\AppData\Local\Programs\Python\Python311\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 444, in execute
    self.error_handler.check_response(response)
  File "C:\Users\admin\AppData\Local\Programs\Python\Python311\Lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 249, 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=107.0.5304.107)
Stacktrace:
Backtrace:
    Ordinal0 [0x0082ACD3+2075859]
    Ordinal0 [0x007BEE61+1633889]
    Ordinal0 [0x006BB7BD+571325]
    Ordinal0 [0x006BE374+582516]
    Ordinal0 [0x006BE225+582181]
    Ordinal0 [0x006BE4C0+582848]
    Ordinal0 [0x006EC654+771668]
    Ordinal0 [0x006E1AED+727789]
    Ordinal0 [0x0070731C+881436]
    Ordinal0 [0x006E15BF+726463]
    Ordinal0 [0x00707534+881972]
    Ordinal0 [0x0071B56A+963946]
    Ordinal0 [0x00707136+880950]
    Ordinal0 [0x006DFEFD+720637]
    Ordinal0 [0x006E0F3F+724799]
    GetHandleVerifier [0x00ADEED2+2769538]
    GetHandleVerifier [0x00AD0D95+2711877]
    GetHandleVerifier [0x008BA03A+521194]
    GetHandleVerifier [0x008B8DA0+516432]
    Ordinal0 [0x007C682C+1665068]
    Ordinal0 [0x007CB128+1683752]
    Ordinal0 [0x007CB215+1683989]
    Ordinal0 [0x007D6484+1729668]
    BaseThreadInitThunk [0x771F00F9+25]
    RtlGetAppContainerNamedObjectPath [0x77337BBE+286]
    RtlGetAppContainerNamedObjectPath [0x77337B8E+238]
    ```

1 Answer 1

0

The problem is that you are fetching and storing input_field and submit_button before/outside of the loop. Inside the loop, I'm sure that when you click the submit button, the page changes/refreshes causing the stale exception because you've lost the references to the two elements. Just fetch these two elements INSIDE the loop and that should fix the problem. Once the page is reloaded, these elements are fetched fresh off the current page.

I also removed the sleep in the middle of the loop. Sleeps are a bad practice because they aren't "smart". They wait however long you tell them to wait and that may be enough or too much depending on how fast the site is. Instead use a WebDriverWait to wait for some condition and it will only wait as long as it needs to, speeding up your script and (generally) eliminating intermittent issues.

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait

# open browser and go to webpage
driver = webdriver.Chrome()
driver.get('https://twitter.com/account/begin_password_reset')

# read usernames from txt file
with open('usernames.txt', 'r') as file:
    usernames = file.readlines()

# iterate over usernames and submit form
for username in usernames:
    # input username and submit form
    input_field = driver.find_element(By.NAME,'account_identifier')
    input_field.clear()
    input_field.send_keys(username)
    submit_button = driver.find_element(By.XPATH,"//input[@type='submit' and @value='Search']")
    submit_button.click()

    # wait for form to submit
    wait = WebDriverWait(driver, 10)
    wait.until(EC.staleness_of(submit_button))

    # check if radio button exists and get label value
    try:
        label = wait.until(EC.visibility_of_element_located(By.CSS_SELECTOR, 'input[name="method"][checked] + label'))
        text = label.get_attribute('textContent')
        print(text)
    except:
        pass

# close browser
driver.quit()
Sign up to request clarification or add additional context in comments.

2 Comments

Hi, thank's for your answer, but this still give me the same error
Well, I can't help you any more than this because I don't have your usernames.txt file, etc. so this was a best guess.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.