如何用硒正确调用边缘浏览器?

how to properly call the edge browser with selenium?

这个项目的目标是使用 selenium-python 使用 Microsoft edge 浏览器自动检查站点我从 this link 下载了 edge legacy 的 webdriver 并且我去了最新版本 17134 提取它没有任何问题,现在可以说我想使用 geckodriver

以自动方式使用 firefox 访问 facebook

使用 selenium 的 firefox 代码示例

from  selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.firefox.options import Options

# setting up headless option for faster execution
options = Options()
options.headless = True


browser = (webdriver.Firefox(options=options))
browser.get('https://www.facebook.com/')

但是当我尝试使用 windows 10 中内置的 Microsoft edge 时,我收到属性错误

from  selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.edge.options import Options



options = Options()
options.headless = True

#browser = webdriver.edge(options=options)
browser = webdriver.edge()

ps :当我取消注释这部分时(浏览器 = webdriver.edge(选项=选项))我得到模块未找到错误

调用 Microsoft edge 浏览器的正确方法是什么,或者我做错了什么

我尝试参考 WebDriver for Microsoft Edge (EdgeHTML) 的官方文档。但是我没有得到任何关于Headless模式的信息。

WebDriver (EdgeHTML)

我也尝试参考一些旧线程以查找有关此主题的任何信息。看来我们无法在 MS Edge 旧版浏览器中使用 Headless 模式。

我发现一篇文章也说 'User cannot use IE10, IE11, Edge, Opera & Safari for headless testing.'

Headless Browsers Testing using Selenium Webdriver

根据以上参考资料,您似乎无法在 MS Edge 旧版浏览器中使用 Headless 模式。

作为解决方法,我建议您尝试使用 MS Edge Chromium 浏览器进行测试。我发现它支持Headless模式。

Using Chromium-Specific Options

当我使用 Edge 并尝试让 Edge 无头时。我也发现很难通过 Chrome 的微小变化来做到这一点。并且我参考了官方文档,得到了官方的解决方案。除了selenium,你还需要安装msedge-selenium-tools,只需pip install itpip install msedge-selenium-tools。并在 msedge 工具中使用 Edge Class。就像:

from msedge.selenium_tools import Edge
driver = Edge(executable_path='where')

如果我们想让 Edge 无头,我们需要使用 EdgeOptions Class,selenium.webdriver 不提供。 selenium.webdriver只为我们提供了Chrome选项,FirefoxOptions和Ie的。 EdgeOptions 在一个单独的包中 msedge.selenium_tools。然后我们添加参数,就像我们在 Firefox 或 Chrome 上所做的那样。在此之前,我们需要将属性 use_chromium 设置为 True。全部代码:

from msedge.selenium_tools import EdgeOptions
from msedge.selenium_tools import Edge

# make Edge headless
edge_options = EdgeOptions()
edge_options.use_chromium = True  # if we miss this line, we can't make Edge headless
# A little different from Chrome cause we don't need two lines before 'headless' and 'disable-gpu'
edge_options.add_argument('headless')
edge_options.add_argument('disable-gpu')

driver = Edge(executable_path='where', options=edge_options)

希望对您有所帮助。对不起,我的解释很笨拙。