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]
```