Python硒 - 如何修复WebDriverexception:消息:目标框架独立

发布于 2025-01-21 20:20:30 字数 2602 浏览 0 评论 0 原文

我正在编码一个通过搜索查询爬网的程序。

  1. 删除第一个查询页面中的所有标题
  2. 单击每个标题的每个链接,并删除链接的主体和退出的主体,然后
  3. 移动到下一页上,

大多数人都可以正常工作,并且昨晚它可以使用480个标题。但是,使用相同的代码,它会不断返回以下内容。

因此,在不同的标题上,一次错误可能会出现在第二查询页面的第8个标题上,另一个时间,一个错误在第一页的第六个标题上弹出。

对于为什么以前很好,以及为什么它以现在的方式行事感到非常困惑。我很感谢一些指示。

我正在与Anaconda -Jupyter笔记本,Selenium(4.1.0。)和Python合作。

WebDriverException                        Traceback (most recent call last)
<ipython-input-100-21e892bd2f24> in <module>
      1 #to_next_page(browser)
----> 2 crawl_page(browser)
      3 #x = XPath_finder(10, browser)
      4 #x.click()

<ipython-input-95-32c6665608a1> in crawl_page(browser)
     10     # 본문 구하는 부분
     11     for i, _ in enumerate(page_titles, 1):
---> 12         XPATH = XPath_finder(i, browser)
     13         XPATH.click()
     14         bs2 = BeautifulSoup(browser.page_source, 'html.parser')

<ipython-input-96-1904e01f1b97> in XPath_finder(i, browser)
      1 def XPath_finder(i, browser):
----> 2     elements = browser.find_elements(By.CLASS_NAME, 'txt_wrap')
      3     target = elements[i-1].find_element(By.TAG_NAME, 'a')
      4     return target

C:\ProgramData\Anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py in find_elements(self, by, value)
   1277         # Return empty list if driver returns null
   1278         # See https://github.com/SeleniumHQ/selenium/issues/4555
-> 1279         return self.execute(Command.FIND_ELEMENTS, {
   1280             'using': by,
   1281             'value': value})['value'] or []

C:\ProgramData\Anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py in execute(self, driver_command, params)
    422         response = self.command_executor.execute(driver_command, params)
    423         if response:
--> 424             self.error_handler.check_response(response)
    425             response['value'] = self._unwrap_value(
    426                 response.get('value', None))

C:\ProgramData\Anaconda3\lib\site-packages\selenium\webdriver\remote\errorhandler.py in check_response(self, response)
    245                 alert_text = value['alert'].get('text')
    246             raise exception_class(message, screen, stacktrace, alert_text)  # type: ignore[call-arg]  # mypy is not smart enough here
--> 247         raise exception_class(message, screen, stacktrace)
    248 
    249     def _value_or_default(self, obj: Mapping[_KT, _VT], key: _KT, default: _VT) -> _VT:

WebDriverException: Message: target frame detached
  (Session info: chrome=100.0.4896.88)```

I am coding a program that crawls through a search query.

  1. scraps all titles from the first query page
  2. clicks each link of each title and scraps main body of the link and exits
  3. moves onto the next page

It works fine for most, and it did for 480 titles last night. But with the same code, it keeps returning the following.

And at different titles, so once, an error might pop up on the 8th title of the 2nd query page, and another time, an error pops up on 6th title of first page and so.

I am utterly baffled as to why it was fine before, and why it is acting the way it is now. I would appreciate some pointers.

I am working with Anaconda - Jupyter Notebook, Selenium (4.1.0.) and Python.

WebDriverException                        Traceback (most recent call last)
<ipython-input-100-21e892bd2f24> in <module>
      1 #to_next_page(browser)
----> 2 crawl_page(browser)
      3 #x = XPath_finder(10, browser)
      4 #x.click()

<ipython-input-95-32c6665608a1> in crawl_page(browser)
     10     # 본문 구하는 부분
     11     for i, _ in enumerate(page_titles, 1):
---> 12         XPATH = XPath_finder(i, browser)
     13         XPATH.click()
     14         bs2 = BeautifulSoup(browser.page_source, 'html.parser')

<ipython-input-96-1904e01f1b97> in XPath_finder(i, browser)
      1 def XPath_finder(i, browser):
----> 2     elements = browser.find_elements(By.CLASS_NAME, 'txt_wrap')
      3     target = elements[i-1].find_element(By.TAG_NAME, 'a')
      4     return target

C:\ProgramData\Anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py in find_elements(self, by, value)
   1277         # Return empty list if driver returns null
   1278         # See https://github.com/SeleniumHQ/selenium/issues/4555
-> 1279         return self.execute(Command.FIND_ELEMENTS, {
   1280             'using': by,
   1281             'value': value})['value'] or []

C:\ProgramData\Anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py in execute(self, driver_command, params)
    422         response = self.command_executor.execute(driver_command, params)
    423         if response:
--> 424             self.error_handler.check_response(response)
    425             response['value'] = self._unwrap_value(
    426                 response.get('value', None))

C:\ProgramData\Anaconda3\lib\site-packages\selenium\webdriver\remote\errorhandler.py in check_response(self, response)
    245                 alert_text = value['alert'].get('text')
    246             raise exception_class(message, screen, stacktrace, alert_text)  # type: ignore[call-arg]  # mypy is not smart enough here
--> 247         raise exception_class(message, screen, stacktrace)
    248 
    249     def _value_or_default(self, obj: Mapping[_KT, _VT], key: _KT, default: _VT) -> _VT:

WebDriverException: Message: target frame detached
  (Session info: chrome=100.0.4896.88)```

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

离笑几人歌 2025-01-28 20:20:31

当您看到错误消息时...

WebDriverException: Message: target frame detached

...偶然的一个可能的原因可能是不兼容在您使用的二进制文件之间。


解决方案

确保:


tl; dr

As you are seeing the error message...

WebDriverException: Message: target frame detached

...indiscriminately one possible reason can be incompatibility between the version of the binaries you are using.


Solution

Ensure that:


tl; dr

Supported platforms

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文