簡體   English   中英

如何使用 Selenium Python 無頭運行 Microsoft Edge?

[英]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 的驅動程序不接受文件路徑以外的任何其他參數。 有什么方法可以像在 Chrome 中一樣無頭運行 Edge 並添加更多選項嗎?

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

試試上面的代碼,你必須啟用鉻才能啟用無頭

https://learn.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 版本是:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM