Python 脚本无法打开 Microsoft edge

Python script can't open Microsoft edge

[找到解决方案,见下文]

我正在使用以下 Python 脚本(使用 Python 2.7)打开 Microsoft Edge 并浏览至 www.freelancer.in(使用 Selenium 3.8.1):

import os
from selenium import webdriver

# create new Edge session
dir = os.path.dirname(__file__)
edge_path = dir + "\MicrosoftWebDriver.exe"
driver = webdriver.Edge(edge_path)
driver.implicitly_wait(10)

driver.get("https://www.freelancer.in/")

它在我的本地机器上正常工作:Windows 专业版 1709,OS 16299.125。但是,它在我的虚拟机上不起作用...我不明白为什么,因为我安装了完全相同的 Windows 10 Pro,我使用的是相同的 Microsoft Webdriver.exe (16299.15)。 Microsoft WebDriver.exe 似乎在工作,因为它说:

[15:32:45.548] - Listening on http://localhost:17556/

但是之后,我收到以下错误:

Traceback (most recent call last):
  File "C:\Users\program.py", line 9, in <module>
    driver = webdriver.Edge(edge_path)
  File "C:\Python27\lib\site-packages\selenium\webdriver\edge\webdriver.py", line 43, in __init__
    desired_capabilities=capabilities)
  File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 154, in __init__
    self.start_session(desired_capabilities, browser_profile)
  File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 243, in start_session
    response = self.execute(Command.NEW_SESSION, parameters)
  File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 312, in execute
    self.error_handler.check_response(response)
  File "C:\Python27\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 208, in check_response
    raise exception_class(value)
WebDriverException: Message: Unknown error

我没有在 Microsoft Edge 中找到任何特定的配置。 关于什么可能导致此错误的任何想法?

=> 在那里找到解决方案:selenium.common.exceptions.WebDriverException: Message: Unknown error while trying to use Edge and MicrosoftWebDriver.exe through Selenium 它无法在虚拟机上运行,​​因为用户帐户控制设置已关闭...打开 UAC 解决了该问题。

错误说明了一切:

selenium.common.exceptions.WebDriverException: Message: Unknown error

很明显 webdriver 实例没有被调用。所以你必须通过 edge_path 连同 argument executable_path 如下:

driver = webdriver.Chrome(executable_path=edge_path)

我遇到了与 Edge 相同的问题。调用 Edge 浏览器不需要任何特定配置。以下代码应该足以打开它:

from selenium.webdriver import Edge
driver = Edge()

这对我来说在笔记本电脑上工作正常,就像你的情况一样 - 但在使用 Win10 的虚拟机上不行...所以我想我们这里有一个可能的模式。

你写道你用 Microsoft Webdriver.exe 16299.15 试过了。您也可以尝试 microsoft 的更新版本 17134。它对我不起作用,但对你有用。

还应该可以直接从您的 Win10 安装中获取 Microsoft Webdriver.exe:设置 → 应用程序 → 管理可选功能 → 添加功能 → Microsoft WebDriver。这应该将 Microsoft Webdriver 直接安装到您的计算机并将其添加到 PATH。

顺便说一句...如果您在 PATH 中设置了 MicrosoftWebDriver.exe,则不需要通过 edge_path。

=> 在那里找到了解决方案:selenium.common.exceptions.WebDriverException:消息:尝试通过 Selenium 使用 Edge 和 MicrosoftWebDriver.exe 时发生未知错误它无法在虚拟机上运行,​​因为用户帐户控制设置已打开关闭...打开 UAC 解决了问题。