【发布时间】:2019-12-24 18:56:07
【问题描述】:
以下是正在使用的代码:
linkedin_urls = driver.find_elements_by_class_name('r')
sub = 'linkedin.com'
for linkedin_url in linkedin_urls:
tag = linkedin_url.find_element_by_tag_name('a')
URL = tag.get_attribute('href')
if sub in URL:
try:
driver.get(URL)
sleep(5)
driver.back()
driver.get(URL)
except:
pass
- 以下是我得到的错误:
Traceback(最近一次调用最后一次):
文件“”,第 25 行,在 tag = linkedin_url.find_element_by_tag_name('a')
文件 "C:\Users\deepankar.garg\AppData\Roaming\Python\Python37\site-packages\selenium\webdriver\remote\webelement.py", 第 305 行,在 find_element_by_tag_name 中 return self.find_element(by=By.TAG_NAME, value=name)
文件 "C:\Users\deepankar.garg\AppData\Roaming\Python\Python37\site-packages\selenium\webdriver\remote\webelement.py", 第 659 行,在 find_element 中 {"using": by, "value": value})['value']
文件 "C:\Users\deepankar.garg\AppData\Roaming\Python\Python37\site-packages\selenium\webdriver\remote\webelement.py", 第 633 行,在 _execute return self._parent.execute(command, params)
文件 "C:\Users\deepankar.garg\AppData\Roaming\Python\Python37\site-packages\selenium\webdriver\remote\webdriver.py", 第 321 行,在执行中 self.error_handler.check_response(response)
文件 "C:\Users\deepankar.garg\AppData\Roaming\Python\Python37\site-packages\selenium\webdriver\remote\errorhandler.py", 第 242 行,在 check_response 中 raise exception_class(message, screen, stacktrace)
StaleElementReferenceException:过时的元素引用:元素是 未附加到页面文档(会话信息: 铬=79.0.3945.79)
下面是IF条件之前的输出:
https://www.linkedin.com/in/elena-grewal
https://www.quora.com/What-is-the-difference-between-Data-Science-and-Analytics
https://www.edureka.co/blog/what-is-data-science/
以下是IF条件后的输出:
https://www.linkedin.com/in/elena-grewal
https://in.linkedin.com/in/bsatya
https://www.linkedin.com/in/kylemckiou
我知道错误的含义,但我不知道如何解决它。我只想在单独的网络浏览器中打开“if”(true)条件下的每个链接。上述“IF 之后”条件中的链接是我希望在每个选项卡中打开的。
任何帮助将不胜感激!
【问题讨论】:
标签: python python-3.x selenium selenium-webdriver web-scraping