【问题标题】:Stale element reference: element is not attached to the page document error after loading a new page [python][selenium]陈旧的元素参考:加载新页面后元素未附加到页面文档错误[python] [selenium]
【发布时间】:2016-10-21 18:22:42
【问题描述】:

这些天我一直在使用 python 和 selenium 的 webdriver 处理个人项目,但是每次脚本加载新页面然后尝试访问时,我都会收到“过时的元素引用:元素未附加到页面文档”错误一个使用“find_element”的元素。我在这里阅读了很多关于这个问题的帖子,但似乎没有任何效果。所以请给我困惑的大脑一些启示。

代码如下:

from selenium import webdriver 
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains
from bs4 import BeautifulSoup

#Ask User for keyword 
keyword = input("Insert Product Name:")
#Open chrome browser
driver = webdriver.Chrome(executable_path=r"C:\chromedriver.exe") 
#Open e-bay.com
driver.get("http://www.e-bay.com")
#Find the search text field element 
search_element = driver.find_element_by_id("gh-ac")
#Clear the search text field of any data
search_element.clear()
#Type the user given keyword to the text field
search_element.send_keys(keyword)
#Hit enter key
search_element.send_keys(Keys.RETURN)
#Set number of listings on the same page (25,50,100,200)
#Find the dropdown menu element for listings per page(Wait till it shows up)
dropdown_menu_lpp = WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "#cbBtmElem > div > ul.sel > li > a.btn.btn-s.small.btn-ter.dropdown-toggle")))
#Mouse over the dropdown menu
actions = ActionChains(driver)
actions.move_to_element(dropdown_menu_lpp)
actions.perform()
#Find the desired setting and click it.I choose 200 listings per page here because is the most convennient for this application.
listings_per_page = WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "#cbBtmElem > div > ul.lyr.txtRt.dropdown-menu.dropdown-menu-sm.menu3 > li:nth-child(3)")))
actions.move_to_element(listings_per_page)
actions.click()
actions.perform()
#Find Listings link(Wait til the links show up first)
listings_links = []
listing_link_elements = WebDriverWait(driver, 10).until(EC.presence_of_all_elements_located((By.CLASS_NAME,"vip")))
#Put the links in a list data-structure 
for link in  listing_link_elements:
            listings_links.append(link.get_attribute('href'))
print(listings_links)    
driver.quit()

该脚本应该打开 e-bay.com ,搜索关键字,将显示的列表数量更改为 200 ,获取(200)列表的链接并将它们放在列表数据结构中。

提前致谢。

PS:我使用的是 python 3.5。

【问题讨论】:

    标签: python-3.x selenium-webdriver


    【解决方案1】:

    我不确定您为什么遇到过时元素问题。我没有 python,但我用 Java 编写了下面的代码,它工作正常。您会注意到我删除了所有等待...您可能需要它们,但它成功打印了所有 206 个(200 个常规 + 6 个赞助)链接,因此我将它们排除在外。

    driver.get("http://www.ebay.com/sch/i.html?_ipg=200&_nkw=" + keyword.replace(" ", "+"));
    List<WebElement> products = driver.findElements(By.cssSelector("a.vip"));
    System.out.println(products.size());
    List<String> links = new ArrayList<String>();
    for (WebElement product : products)
    {
        links.add(product.getAttribute("href"));
    }
    System.out.println(links);
    

    我采取了与您不同的方法。我发现可以编辑 URL 以显示您想要的每页 200 个,您还可以将搜索关键字直接插入 URL。这样可以节省一些时间和代码,除非您尝试执行用户场景(我猜您不是),否则您可以使用此快捷方式。

    要将其转换为 python,我将使用以下代码。注意:我不熟悉python,所以这段代码可能没有优化,我没有办法测试它。

    ...
    driver.get("http://www.ebay.com/sch/i.html?_ipg=200&_nkw=" + keyword.replace(' ', '+'))
    products = driver.find_elements_by_css_selector("a.vip")
    links = []
    for product in products:
        links.append(product.get_attribute('href'))
    print(links)
    driver.quit()
    

    【讨论】:

    • 首先感谢您的回复。我知道上面提到的解决方案,但我试图执行一个用户场景。这就是为什么我专门询问这个错误,而不仅仅是问一个更一般的问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-27
    • 1970-01-01
    • 2019-01-08
    • 2017-12-13
    • 2016-10-13
    相关资源
    最近更新 更多