提问人:ALEX 提问时间:6/6/2023 更新时间:6/7/2023 访问量:281
Microsoft 边缘驱动程序无法启动
Microsoft Edge Driver Failed to start
问:
这是我在Google colab中运行的代码:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.edge.service import Service
# Prompt the user for Microsoft Edge browser sign-in credentials
username = input("Enter your username: ")
password = input("Enter your password: ")
# Set the path to the Microsoft Edge WebDriver executable
webdriver_path = "/usr/local/bin/microsoft-edgedriver.exe" # Replace with the actual path
# Create a new instance of the Service object
# service = webdriver.Service(webdriver_path)
# Start the service
# service.start()
edge_options = webdriver.EdgeOptions()
# Create a new instance of the Microsoft Edge driver
driver = webdriver.Edge(executable_path=webdriver_path, options=edge_options)
# Maximize the browser window (optional)
driver.maximize_window()
# Navigate to the Microsoft sign-in page
driver.get("https://login.live.com/")
# Wait until the username field is visible
username_field = WebDriverWait(driver, 10).until(
EC.visibility_of_element_located((By.NAME, "loginfmt"))
)
# Enter the username
username_field.send_keys(username)
username_field.send_keys(Keys.RETURN)
# Wait until the password field is visible
password_field = WebDriverWait(driver, 10).until(
EC.visibility_of_element_located((By.NAME, "passwd"))
)
# Enter the password
password_field.send_keys(password)
password_field.send_keys(Keys.RETURN)
# Wait until the search box is visible
search_box = WebDriverWait(driver, 10).until(
EC.visibility_of_element_located((By.NAME, "q"))
)
# Enter the search query
search_box.send_keys("hello")
search_box.send_keys(Keys.RETURN)
# Close the browser
driver.quit()
这是我收到的完整错误块
WebDriverException Traceback (most recent call last)
<ipython-input-24-4739c578a60b> in <cell line: 30>()
28 # driver = webdriver.Edge(service=service)
29 # driver = webdriver.Edge(service=Service(EdgeDriverManager().install()))
---> 30 driver = webdriver.Edge(executable_path=webdriver_path, options=edge_options)
31
32 # Maximize the browser window (optional)
5 frames
/usr/local/lib/python3.10/dist-packages/selenium/webdriver/remote/errorhandler.py in check_response(self, response)
243 alert_text = value["alert"].get("text")
244 raise exception_class(message, screen, stacktrace, alert_text) # type: ignore[call-arg] # mypy is not smart enough here
--> 245 raise exception_class(message, screen, stacktrace)
WebDriverException: Message: unknown error: Microsoft Edge failed to start: exited abnormally.
(unknown error: DevToolsActivePort file doesn't exist)
(The process started from msedge location /usr/bin/microsoft-edge is no longer running, so msedgedriver is assuming that msedge has crashed.)
Stacktrace:
#0 0x5586b70e7f93 <unknown>
#1 0x5586b6df48c1 <unknown>
#2 0x5586b6e20cdf <unknown>
#3 0x5586b6e1c1ed <unknown>
#4 0x5586b6e5cea5 <unknown>
#5 0x5586b6e54043 <unknown>
#6 0x5586b6e27941 <unknown>
#7 0x5586b6e28b5e <unknown>
#8 0x5586b70b4536 <unknown>
#9 0x5586b70b8049 <unknown>
#10 0x5586b70b7b39 <unknown>
#11 0x5586b70b8505 <unknown>
#12 0x5586b70bf9db <unknown>
#13 0x5586b70b88ae <unknown>
#14 0x5586b709218c <unknown>
#15 0x5586b70d3d08 <unknown>
#16 0x5586b70d3e44 <unknown>
#17 0x5586b70e1766 <unknown>
#18 0x7fefa654d609 start_thread
我使用了相同版本的 edgedriver 和 microsoft edge,路径也是正确的,selenium 版本也是最新的,但我仍然收到错误。我还使用了chmod + x msedgedriver来获得执行权限
答:
0赞
Michael Mintz
6/7/2023
#1
如果您让 Python Selenium 框架(例如 SeleniumBase)为您正确处理配置 Edge 的繁重工作,那会更容易。
pip install seleniumbase
,然后运行以下脚本:
from seleniumbase import Driver
driver = Driver(browser="edge", headless=False)
driver.get("https://login.live.com/")
# ...
driver.quit()
还有一种上下文管理器格式,可在块的末尾自动关闭驱动程序:with
from seleniumbase import DriverContext
with DriverContext(browser="edge", headless=False) as driver:
driver.get("https://login.live.com/")
# ...
SeleniumBase 也有自己的 API,其中包含简化的方法,可以在与页面元素交互之前自动等待页面元素加载:
from seleniumbase import SB
with SB(browser="edge", headless=False) as sb:
sb.open("https://login.live.com/")
sb.type('input[name="loginfmt"]', "[email protected]")
sb.click('input[value="Next"]')
sb.type('input[name="passwd"]', "test")
sb.sleep(3)
对于所有 SeleniumBase 语法格式,请参阅: https://github.com/seleniumbase/SeleniumBase/blob/master/help_docs/syntax_formats.md
评论
0赞
ALEX
6/11/2023
它显示相同的错误 WebDriverException
0赞
ALEX
6/11/2023
注意:我在google colab中运行此代码
评论