如何 运行 Microsoft Edge 与 Selenium Python 无头?
How to run Microsoft Edge headless with Selenium Python?
使用 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 的驱动程序不接受文件路径以外的任何其他参数。有什么方法可以 运行 Edge headless 并像 Chrome 一样添加更多选项吗?
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
这仅适用于新的 edge chromium,不适用于 edge 旧版本。在旧版本中,不支持无头
完整代码
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
不接受任何 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 位)
这对我有用。
对于 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()
对于 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()
使用 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 的驱动程序不接受文件路径以外的任何其他参数。有什么方法可以 运行 Edge headless 并像 Chrome 一样添加更多选项吗?
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
这仅适用于新的 edge chromium,不适用于 edge 旧版本。在旧版本中,不支持无头
完整代码
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
不接受任何 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 位)
这对我有用。
对于 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()
对于 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()