[英]Python Selenium - How to fix WebDriverException: Message: target frame detached
I am coding a program that crawls through a search query.我正在编写一个通过搜索查询爬行的程序。
It works fine for most, and it did for 480 titles last night.它对大多数人来说都很好,昨晚它为 480 个标题做了。 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.并且在不同的标题上,有一次,第二个查询页面的第8个标题可能会弹出一个错误,另一次,第一页的第6个标题会弹出一个错误,依此类推。
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.我正在使用 Anaconda - Jupyter Notebook、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)```
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. ...不加选择地一个可能的原因可能是您使用的二进制文件版本之间的不兼容。
Ensure that:确保这件事:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.