Headless Edge Selenium 错误:没有打开浏览器

Posted

技术标签:

【中文标题】Headless Edge Selenium 错误:没有打开浏览器【英文标题】:Headless Edge Selenium Error: No browser is open 【发布时间】:2021-08-31 14:48:33 【问题描述】:

我正在尝试让 Edge 打开 Headless,但我不断收到错误消息“没有打开浏览器”。

这是 Python 代码:

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

def function():
    edge_options = EdgeOptions()
    edge_options.use_chromium = True
    edge_options.add_argument('headless')
    edge_options.add_argument('disable-gpu')

    driver = Edge(executable_path='C:/Users/ID75143/PycharmProjects/TestProject/venv/Scripts/MicrosoftWebDriver.exe', options=edge_options)

无论我在最后一行后面放什么(我尝试打开一个 URL 并最大化窗口),它都说这是不可能的,因为“没有打开浏览器”。我试过在有头模式下打开 Edge,然后它工作得很好,但不是无头。

有人知道该怎么做吗?

【问题讨论】:

您能检查一下您使用的驱动程序是否与您的浏览器版本兼容吗? 是的,它是正确的驱动程序。 【参考方案1】:

而不是这个:

edge_options.add_argument('headless')

这样做:

edge_options.add_argument('--headless')

【讨论】:

这不起作用。它仍然显示“没有打开浏览器”。【参考方案2】:

根据您提供的代码,我发现您使用的是MicrosoftWebDriver。在 Legacy Edge 中使用,更多详情请查看:What is Microsoft Edge Legacy?

所以我必须再次确认,您使用的是正确版本的 Edge 浏览器和驱动程序吗?也许你应该在测试之前尝试升级它们。

这是一个简单的测试,它工作正常(版本 91.0.864.48):

from msedge.selenium_tools import Edge, EdgeOptions

options = EdgeOptions()
options.use_chromium = True
options.add_experimental_option("prefs", 
  "download.default_directory": r"E:\Downloads"  #change the route you need
)
options.add_argument("headless")
options.add_argument("disable-gpu")
driver = Edge(executable_path=           #put your edgedriver here
    r'C:\Users\Administrator\Desktop\msedgedriver.exe', options=options)
driver.get("https://www.seleniumhq.org/download/");
m = driver.find_element_by_link_text("32 bit Windows IE")
m.click()

【讨论】:

以上是关于Headless Edge Selenium 错误:没有打开浏览器的主要内容,如果未能解决你的问题,请参考以下文章

Selenium-Chrome-Headless:无法从渲染器接收消息

Headless谷歌selenium使用

弃用警告:在 Python 中使用 Geckodriver 和 Selenium 将 setter 用于无头属性而不是 set_headless opts.set_headless(headless=

Chrome Headless模式——Python+selenium+headerless

使用 chrome headless 和 selenium 下载

Selenium Chrome Headless 下载文件