一、起因
今天打算用selenium跑个爬虫,下面是代码:
from selenium import webdriver
from selenium.webdriver.edge.service import Service
import time
ser = Service("C:/YB/bin/msedgedriver.exe")
edge_options = webdriver.EdgeOptions()
edge_options.add_argument("user-data-dir=C:/Users/yuanb/AppData/Local/Microsoft/Edge/User Data")
edge_options.add_argument("profile-directory=Default")
driver = webdriver.Edge(service=ser, options=edge_options)
driver.get('https://bing.com')
time.sleep(20)
driver.quit()
最近因为Edge浏览器换成chromium内核后很好用,我就在换了新电脑后,没有装chrome,一直用Edge,于是就用Edge浏览器来跑,结果就给我报错了:
Traceback (most recent call last):
File "C:\Users\yuanb\PycharmProjects\ChinaJudgementsOL\spider2.py", line 14, in <module>
driver = webdriver.Edge(service=ser, options=edge_options)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\YB\Software\anaconda3\envs\selenium\Lib\site-packages\selenium\webdriver\edge\webdriver.py", line 45, in __init__
super().__init__(
File "C:\YB\Software\anaconda3\envs\selenium\Lib\site-packages\selenium\webdriver\chromium\webdriver.py", line 61, in __init__
super().__init__(command_executor=executor, options=options)
File "C:\YB\Software\anaconda3\envs\selenium\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 208, in __init__
self.start_session(capabilities)
File "C:\YB\Software\anaconda3\envs\selenium\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 292, in start_session
response = self.execute(Command.NEW_SESSION, caps)["value"]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\YB\Software\anaconda3\envs\selenium\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 347, in execute
self.error_handler.check_response(response)
File "C:\YB\Software\anaconda3\envs\selenium\Lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 229, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.SessionNotCreatedException: Message: session not created: Microsoft Edge failed to start: exited normally.
(chrome not reachable)
(The process started from msedge location C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe is no longer running, so msedgedriver is assuming that msedge has crashed.)
Stacktrace:
GetHandleVerifier [0x00007FF6C3A35B82+59842]
Microsoft::Applications::Events::ILogConfiguration::operator* [0x00007FF6C39C4AB2+220434]
(No symbol) [0x00007FF6C37EDCD7]
(No symbol) [0x00007FF6C382043F]
(No symbol) [0x00007FF6C381C2C0]
(No symbol) [0x00007FF6C385DE97]
(No symbol) [0x00007FF6C3853B93]
(No symbol) [0x00007FF6C382960E]
(No symbol) [0x00007FF6C38287CC]
(No symbol) [0x00007FF6C38291C1]
Microsoft::Applications::Events::EventProperty::to_string [0x00007FF6C3BE58FC+1052188]
(No symbol) [0x00007FF6C389FFC6]
Microsoft::Applications::Events::EventProperty::~EventProperty [0x00007FF6C394150C+37916]
Microsoft::Applications::Events::EventProperty::~EventProperty [0x00007FF6C3938E95+3493]
Microsoft::Applications::Events::EventProperty::to_string [0x00007FF6C3BE4865+1047941]
Microsoft::Applications::Events::ILogConfiguration::operator* [0x00007FF6C39CD2C1+255265]
Microsoft::Applications::Events::ILogConfiguration::operator* [0x00007FF6C39C85B4+235540]
Microsoft::Applications::Events::ILogConfiguration::operator* [0x00007FF6C39C86EB+235851]
Microsoft::Applications::Events::ILogConfiguration::operator* [0x00007FF6C39BFEE1+201025]
BaseThreadInitThunk [0x00007FFB626E257D+29]
RtlUserThreadStart [0x00007FFB63BAAA58+40]
二、解决
网上搜各种解决方案,最终找到了一个点:
需要在任务管理器中杀掉edge进程!!!
需要在任务管理器中杀掉edge进程!!!
需要在任务管理器中杀掉edge进程!!!
这让我想起了前几年写过的一篇博客,当时是用chrome浏览器,就是因为只关闭了chrome浏览器而没有杀掉chrome进程,也导致启动报错!好吧,同一个坑踩了两次。
三、参考
1、python - MSEdge failed to start: crashed (chrome not reachable) - Stack Overflow
2、selenium.webdriver unknow error:cannot connect to chrome at 127.0.0.1:9222-CSDN博客