本文介绍了如何使用 Selenium Python 无头运行 Microsoft Edge?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 Chrome,您可以在创建驱动程序时添加选项.你只要做

With Chrome you can add options when creating the driver. You just do

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

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

But for some reason when trying to do the same with 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 无头并添加更多选项?

For some reason Edge's driver doesn't accept any other parameters than the file path. Is there any way to run Edge headless and add more options just like in Chrome?

推荐答案

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

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

Try above code , you have to enable chromium to enable headless

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

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

This works only for new edge chromium not for edge legacy versions . In legacy versions headless is not supported

完整代码

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)

这篇关于如何使用 Selenium Python 无头运行 Microsoft Edge?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-16 22:51
查看更多