简体   繁体   English

使用 Selenium Web 驱动程序调用时出错:driver.get(linked_url.get_atrribute(“href”)),过时的元素引用

[英]Error using Selenium web driver call: driver.get(linked_url.get_atrribute(“href”)), stale element reference

I am working with selenium in python.我正在 python 中使用硒。 I am extracting the links from google search results which I have done successfully lower now I am trying to navigate to these one by one using a for loop and a driver.get() method:我正在从谷歌搜索结果中提取链接,我现在已经成功地完成了这些链接,现在我正在尝试使用 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")) 
    

I have successfully extracted the urls.我已经成功提取了网址。 I verified this with this code:我用这个代码验证了这一点:

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)

this returns the list of urls that I have extracted.这将返回我提取的 url 列表。 However when running the first code snippet I get the following error:但是,当运行第一个代码片段时,我收到以下错误:

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)

from my understanding the error that the stale element is not in the page source is due to either the element, when called having been deleted or if the element is not longer in the DOM.根据我的理解,过时元素不在页面源中的错误是由于该元素在调用时已被删除或该元素不再存在于 DOM 中。 NO element has been deleted, and I can't seem to find a reason why the DOM might have changed.没有元素被删除,我似乎找不到 DOM 可能发生变化的原因。 Any ideas why this error is occurring?任何想法为什么会发生此错误?

You have probably been directed to the next page because of which you are getting StaleElementReferenceException exception because after coming back to the main page the elements which are extracted gets removed from temporary memory.您可能已被定向到下一页,因此您会收到StaleElementReferenceException异常,因为在返回主页面后,提取的元素会从临时内存中删除。 The solution for this can be extracting elements in the loop so that everytime you are trying to access some element it gets extracted that time only.对此的解决方案可以是在循环中提取元素,这样每次您尝试访问某个元素时,它只会在该时间被提取。

Now over here the for loop will not work, you have to use while loop.现在在这里for循环不起作用,您必须使用while循环。 A sample code be like:示例代码如下:

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

Over here the issue you will face is how many times the iteration need to be done.在这里,您将面临的问题是需要进行多少次迭代。 For that you may make a function like:为此,您可以创建一个函数,如:

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

This function is basically checking the presence and visibility of element given when calling the function so that the elements can be extracted easily and without any error.这个函数基本上是检查调用函数时给定的元素的存在和可见性,以便可以轻松地提取元素并且没有任何错误。

This has worked in my case.这在我的情况下有效。

I avoided this error by doing the following我通过执行以下操作避免了此错误

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM