Python Splinter - 消息:陈旧元素引用。但等待并不能解决问题

时间:2014-10-15 20:35:35

标签: python selenium selenium-webdriver webdriver splinter

我正在编写一个使用Chrome Webdriver和Splinter浏览网页的Python代码:

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

在actions.perform()处,发生错误:

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

我尝试了time.sleep函数,但仍然发生了陈旧的错误。这是页面上唯一的元素,我知道程序已找到它:

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

我想问一下我可以做些什么来解决这个问题。谢谢!

修改

我将代码更改为find_element_by_class_name,只检索了一次元素,但仍然出现过时的元素错误?

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

整个代码如下:

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

0 个答案:

没有答案