【问题标题】:How to run Microsoft Edge headless with Selenium Python?如何使用 Selenium Python 无头运行 Microsoft Edge?
【发布时间】:2020-12-06 17:51:07
【问题描述】:

使用 Chrome,您可以在创建驱动程序时添加选项。你只是这样做

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

但出于某种原因,在尝试对 Microsoft Edge 执行相同操作时

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

我收到此错误

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

出于某种原因,Edge 的驱动程序不接受文件路径以外的任何其他参数。有什么方法可以像在 Chrome 中一样无头运行 Edge 并添加更多选项?

【问题讨论】:

  • 你使用的是什么版本的硒?
  • @Sharmiko Selenium 3.141.0

标签: python selenium microsoft-edge


【解决方案1】:
  options = EdgeOptions()
  options.use_chromium = True
  options.add_argument("headless")
  options.add_argument("disable-gpu")

试试上面的代码,你必须启用 chromium 才能启用 headless

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

这仅适用于新边缘铬,不适用于边缘旧版本。在旧版本中,不支持无头

完整代码

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)

【讨论】:

  • 我收到AttributeError: 'Options' object has no attribute 'AddArgument'
  • 之前是java代码,添加了python代码和edge chromium文档。如果您使用的是 edge legacy,那么您不能在 headless 中运行它
  • 我运行最新版本的边缘,仍然得到同样的错误
  • 什么错误,我更新了代码你能粘贴错误
  • pip install msedge-selenium-tools ,您必须阅读该文档来安装它。
【解决方案2】:

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)

我使用的 Microsoft Edge 版本是:

微软边缘 版本 89.0.774.57(正式版)(64 位)

这对我有用。

【讨论】:

    【解决方案3】:

    用于边缘浏览器

    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()

    对于 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()

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-09-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-10
      • 1970-01-01
      • 1970-01-01
      • 2020-12-10
      相关资源
      最近更新 更多