如何使用selenium python以无头模式运行浏览器

问题描述 投票:0回答:1

这是我项目的一段代码

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.edge.service import Service
from selenium.webdriver.edge.options  import Options
import time
def extract_data(self, usrname : str):

        self.edge_options  = Options()
        self.edge_options.add_argument("--headless")

        self.usrname = usrname
        service = Service(executable_path=r'C:\web\msedgedriver.exe')
        self.driver = webdriver.Edge(service=service)
        self.driver.get(f'https://leetcode.com/{usrname}/') 

我也使用 --headless open ,但浏览器窗口仍然打开,而不是仅在后台工作。 怎么解决?

python python-3.x selenium-webdriver web-scraping
1个回答
0
投票
self.driver = webdriver.Edge(service=service)

将上面更改为:

self.driver = webdriver.Edge(service=service, options=self.edge_options)
© www.soinside.com 2019 - 2024. All rights reserved.