3

我正在编写一个 Python 代码,它使用 Chrome Webdriver 和 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()

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

0 回答 0