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()