3

I'm writing a Python code which navigates through webpages using the Chrome Webdriver and Splinter:

driver.switch_to.frame(driver.find_element_by_id("accountSummaryFrame"))

time.sleep(10)
login2 = driver.find_elements_by_tag_name("a")
actions.click(login2)
actions.perform()

At actions.perform(), an error occurs:

selenium.common.exceptions.StaleElementReferenceException: Message: 'stale element reference:    
element is not attached to the page document\n  (Session info: chrome=37.0.2062.124)\n  (Driver   
info: chromedriver=2.11.298604 (75ea2fdb5c87f133a8e1b8da16f6091fb7d5321e),platform=Windows NT 
6.2 x86_64)' 

I tried a time.sleep function but the stale error still happens. This is the only element on the page and I know the program has found it:

[<selenium.webdriver.remote.webelement.WebElement object at 0x033B3B10>]

I want to ask what I can do to resolve this problem. Thank you!

EDIT:

I changed the code to a find_element_by_class_name and only searched the element once but still got a stale element error?

time.sleep(5)
driver.switch_to.frame(driver.find_element_by_id("accountSummaryFrame"))
time.sleep(5)
actions.click(driver.find_element_by_class_name("platform_spot")).perform()

The whole code is the following:

import os
from splinter import Browser
from splinter import driver
from selenium import webdriver
import time
from selenium.webdriver.common.action_chains import ActionChains


url = "http://www.example.com"
driver = webdriver.Chrome(executable_path='C:\chromedriver_win32\chromedriver.exe')
driver.get(url)

actions = ActionChains(driver)
driver.switch_to.frame(driver.find_element_by_tag_name("iframe"))

form_username = driver.find_element_by_name('login[username]')
form_password = driver.find_element_by_name('login[password]')

form_username.send_keys("username")
form_password.send_keys("password")

time.sleep(5)
login_button = driver.find_element_by_id('mini_submit_login')
actions.click(login_button)
actions.perform()
driver.switch_to.default_content()


time.sleep(5)
driver.switch_to.frame(driver.find_element_by_id("accountSummaryFrame"))
time.sleep(5)
actions.click(driver.find_element_by_class_name("platform_spot")).perform()
antnik45
  • 41
  • 3
  • StaleElementReferenceException always means that you found the element once, and when you try to interact with it, it is no longer there. Usually this happens after the page transitioned. – SiKing Oct 15 '14 at 21:43
  • Well I've found that the element becomes stale after actions.click(login2), and at that point the page does not change but it displays the error on the last line. Do you think there might be something happening in the html code? – antnik45 Oct 15 '14 at 21:59
  • 1
    I have looked at the html and css code before and after and nothing has changed... it's really annoying! – antnik45 Oct 16 '14 at 00:24
  • Also the piece of html that is concerned is in an iframe – antnik45 Oct 16 '14 at 00:27
  • Is there any particular reason you're using an actions click? – Mark Rowlands Oct 16 '14 at 08:22
  • I declare the Chrome Webdriver like this: driver = webdriver.Chrome(executable_path='C:\chromedriver_win32\chromedriver.exe'). The only attribute I've found is actions and I use it because it is displayed as a button on the page which the user has to click. – antnik45 Oct 16 '14 at 15:26
  • @antnik45 is the page using React, Vue, or Angular or some other frontend template framework? In this case parts of the page may constantly be recreated using the same HTML, but the reference to elements in Selenium will get lost each rebuild. — If this is the case, it's best to ensure the submit button is not constantly being rebuilt in your app code. Alternatively, submit the form using the [Enter key](https://stackoverflow.com/a/42349472/451480) or by submitting the form using `driver.execute_script('document.forms[0].submit()')`. – Blaise Mar 21 '20 at 10:11

0 Answers0