【问题标题】:Error using Selenium web driver call: driver.get(linked_url.get_atrribute("href")), stale element reference使用 Selenium Web 驱动程序调用时出错:driver.get(linked_url.get_atrribute("href")), stale element reference
【发布时间】:2020-09-12 01:43:46
【问题描述】:

我正在 python 中使用 selenium。我正在从谷歌搜索结果中提取链接,这些链接已经成功降低,现在我正在尝试使用 for 循环和 driver.get() 方法一一导航到这些链接:

search_gog=driver.find_element_by_name('q')
search_gog.send_keys(parameters.search_gog)
sleep(0.5)
search_gog.send_keys(Keys.RETURN)
sleep(3)linkedin_urls = driver.find_elements_by_xpath("//div[@class='g']//div[@class='r']/a[contains(@href, 'https://www.linkedin.com')]") 

for linkedin_url in linkedin_urls:
  
    driver.get(linkedin_url.get_attribute("href")) 
    

我已成功提取网址。我用这段代码验证了这一点:

linkedin_urls = driver.find_elements_by_xpath("//div[@class='g']//div[@class='r']/a[contains(@href, 'https://www.linkedin.com')]") 

for linkedin_url in linkedin_urls:
  
  print(linkedin_url)

这将返回我提取的 url 列表。但是,在运行第一个代码 sn-p 时,出现以下错误:

Traceback (most recent call last):
  File "app2.py", line 45, in <module>
    driver.get(linkedin_url.get_attribute("href")) #linkedin_url.get_attribute("href")
  File "/Users/rubenolmos/opt/anaconda3/lib/python3.8/site-packages/selenium/webdriver/remote/webelement.py", line 139, in get_attribute
    attributeValue = self.parent.execute_script(
  File "/Users/rubenolmos/opt/anaconda3/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 634, in execute_script
    return self.execute(command, {
  File "/Users/rubenolmos/opt/anaconda3/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "/Users/rubenolmos/opt/anaconda3/lib/python3.8/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: element is not attached to the page document
  (Session info: chrome=85.0.4183.102)

据我了解,过时元素不在页面源中的错误是由于该元素在调用时已被删除,或者该元素不再位于 DOM 中。没有元素被删除,我似乎找不到 DOM 可能发生变化的原因。任何想法为什么会发生此错误?

【问题讨论】:

    标签: python html selenium-webdriver dom


    【解决方案1】:

    您可能已被定向到下一页,因此您收到StaleElementReferenceException 异常,因为在返回主页后,提取的元素会从临时内存中删除。解决方案可以是在循环中提取元素,这样每次您尝试访问某个元素时,它只会在该时间被提取。

    现在for 循环在这里不起作用,你必须使用while 循环。示例代码如下:

    while(elementPresent(locatorType, locator)):
        link = driver.find_element_by_xpath(locator)
        #take action what you want to 
    

    在这里,您将面临的问题是需要进行多少次迭代。为此,您可以编写如下函数:

    def elementPresent(locatorType, locator):
    #present = true
    #not present = false
    wait = WebDriverWait(driver, 20)
    try:
        wait.until(EC.presence_of_element_located((locatorType, locator)))
        wait.until(EC.visibility_of_element_located((locatorType, locator)))
    except Exception:
        return False
    return True
    

    这个函数基本上是在调用函数时检查元素的存在和可见性,以便可以轻松提取元素并且没有任何错误。

    这对我来说很有效。

    【讨论】:

    • 谢谢你,但是我用不同的方式做了。我按如下list_links = [link.get_attribute('href') for link in driver.find_elements_by_xpath("//div[@class='g']//div[@class='r']/a[contains(@href, 'https://www.linkedin.com')]")] for list_links 中的链接进行操作:driver.get(link)
    【解决方案2】:

    我通过执行以下操作避免了此错误

    list_links = [link.get_attribute('href') for link in driver.find_elements_by_xpath("//div[@class='g']//div[@class='r']/a[contains(@href, 'https://www.linkedin.com')]")]
    
        for link in list_links:
            driver.get(link)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-09
      相关资源
      最近更新 更多