简体   繁体   English

如何使用 Selenium Python 无头运行 Microsoft Edge?

[英]How to run Microsoft Edge headless with Selenium Python?

With Chrome you can add options when creating the driver.使用 Chrome,您可以在创建驱动程序时添加选项。 You just do你只要做

options = Options()
options.headless = True
driver = webdriver.Chrome(PATH\TO\DRIVER, options=options)

But for some reason when trying to do the same with Microsoft Edge但出于某种原因,当尝试对 Microsoft Edge 执行相同操作时

options = Options()
options.headless = True
driver = webdriver.Edge(PATH\TO\DRIVER, options=options)

I get this error我收到这个错误

TypeError: __init__() got an unexpected keyword argument 'options'

For some reason Edge's driver doesn't accept any other parameters than the file path.出于某种原因,Edge 的驱动程序不接受文件路径以外的任何其他参数。 Is there any way to run Edge headless and add more options just like in Chrome?有什么方法可以像在 Chrome 中一样无头运行 Edge 并添加更多选项吗?

  options = EdgeOptions()
  options.use_chromium = True
  options.add_argument("headless")
  options.add_argument("disable-gpu")

Try above code, you have to enable chromium to enable headless试试上面的代码,你必须启用铬才能启用无头

https://learn.microsoft.com/en-us/microsoft-edge/webdriver-chromium/?tabs=python https://learn.microsoft.com/en-us/microsoft-edge/webdriver-chromium/?tabs=python

This works only for new edge chromium not for edge legacy versions.这仅适用于新的 edge chromium,不适用于 edge 遗留版本。 In legacy versions headless is not supported在旧版本中,不支持无头

Full code完整代码

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='youredgedriverpath', options=edge_options)

webdriver.Edge does not accept any options so i switched it to the following: It worked for me. webdriver.Edge不接受任何options ,所以我将其切换为以下选项:它对我有用。

        # imports
        from selenium import webdriver
        from msedge.selenium_tools import EdgeOptions

        # options
        options = EdgeOptions()
        options.use_chromium = True
        options.add_argument("--headless")
        options.add_argument("disable-gpu")

        browser = webdriver.Chrome(
            executable_path="resources/msedgedriver.exe", options=options)
        browser.get(gen_token)

The version of Microsoft Edge that I am using is:我使用的 Microsoft Edge 版本是:

Microsoft Edge Version 89.0.774.57 (Official build) (64-bit) Microsoft Edge 版本 89.0.774.57(正式版)(64 位)

This worked for me.这对我有用。

for Edge browser适用于 Edge 浏览器

options = EdgeOptions()

options.use_chromium = True

options.add_argument('--allow-running-insecure-content')

options.add_argument("--ignore-certificate-errors")

self.wd = webdriver.Chrome(executable_path=EdgeChromiumDriverManager().install(), options=options)

self.wd.maximize_window()

For Edge headless对于 Edge 无头

options = EdgeOptions()

options.use_chromium = True

options.add_argument("--headless")

options.add_argument("disable-gpu")

options.add_argument('--allow-running-insecure-content')

options.add_argument('--ignore-certificate-errors')

self.wd = webdriver.Chrome(executable_path=EdgeChromiumDriverManager().install(), options=options)

self.wd.maximize_window()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM