selenium.webdriver.chrome.options 中add_argument 常用参数表

详细的文档介绍:List of Chromium Command Line Switches « Peter Beverlooicon-default.png?t=LA92https://peter.sh/experiments/chromium-command-line-switches/ 

eg: 

          chrome_options.add_argument('--incognito')              # 设置Chrome为隐身模式(无痕模式)

--allow-outdated-plugins             不停用过期的插件。
--allow-running-insecure-content     默认情况下,https 页面不允许从 http 链接引用 javascript/css/plug-ins。添加这一参数会放行这些内容。
--allow-scripting-gallery            允许拓展脚本在官方应用中心生效。默认情况下,出于安全因素考虑这些脚本都会被阻止。
--disable-accelerated-video          停用 GPU 加速视频。
--disable-dart                       停用 Dart。
--disable-desktop-notifications      禁用桌面通知,在 Windows 中桌面通知默认是启用的。
--disable-extensions                 禁用拓展。
--disable-file-system                停用 FileSystem API。
--disable-preconnect                 停用 TCP/IP 预连接。
--disable-remote-fonts               关闭远程字体支持。SVG 中字体不受此参数影响。
--disable-speech-input               停用语音输入。
--disable-web-security               不遵守同源策略。
--disk-cache-dir                     将缓存设置在给定的路径。
--disk-cache-size                    设置缓存大小上限,以字节为单位。
--dns-prefetch-disable               停用DNS预读。
--enable-print-preview               启用打印预览。
--extensions-update-frequency        设定拓展自动更新频率,以秒为单位。
--incognito                          让浏览器直接以隐身模式启动。
--keep-alive-for-test                最后一个标签关闭后仍保持浏览器进程。(某种意义上可以提高热启动速度,不过你最好得有充足的内存)
--kiosk                              启用kiosk模式。(一种类似于全屏的浏览模式)
--lang                               使用指定的语言。
--no-displaying-insecure-content     默认情况下,https 页面允许从 http 链接引用图片/字体/框架。添加这一参数会阻止这些内容。
--no-first-run                       跳过 Chromium 首次运行检查。
--no-referrers                       不发送 Http-Referer 头。
--no-sandbox                         彻底停用沙箱。
--no-startup-window                  启动时不建立窗口。
--proxy-pac-url                      使用给定 URL 的 pac 代理脚本。(也可以使用本地文件,如 --proxy-pac-url="file:\\\c:\proxy.pac")
--proxy-server                       使用给定的代理服务器,这个参数只对 http 和 https 有效。(例如 --proxy-server=127.0.0.1:8087 )
--single-process                     以单进程模式运行 Chromium。(启动时浏览器会给出不安全警告)
--start-maximized                    启动时最大化。
--user-agent                         使用给定的 User-Agent 字符串

参数:--user-data-dir=UserDataDir
用途:自订使用者帐户资料夹(如:–user-data-dir="D:\temp\Chrome User Data")
参数:--process-per-tab
用途:每个分页使用单独进程
参数:--process-per-site
用途:每个站点使用单独进程
参数:--in-process-plugins
用途:插件不启用单独进程

参数:--disable-popup-blocking
用途:禁用弹出拦截
参数:--disable-javascript
用途:禁用JavaScript
参数:--disable-java
用途:禁用Java
参数:--disable-plugins
用途:禁用插件
参数:–disable-images
用途:禁用图像
参数:--omnibox-popup-count=”num”
用途:将网址列弹出的提示选单数量改为num个
参数:--enable-vertical-tabs
用途:调整chrome游览器标签存放在左边,非顶部
from selenium import webdriver from selenium.webdriver.chrome.service import Service from selenium.webdriver.chrome.webdriver import WebDriver from selenium.webdriver.common.by import By from selenium.webdriver.chrome.options import Options from selenium.common.exceptions import TimeoutException, WebDriverException from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC def init_driver(): chrome_options = Options() # 基础配置 chrome_options.add_argument("--no-sandbox") chrome_options.add_argument("--disable-dev-shm-usage") chrome_options.add_argument("--headless=new") chrome_options.add_argument("--disable-gpu") chrome_options.add_argument("--single-process") # 强化反检测配置 chrome_options.add_argument("--disable-blink-features=AutomationControlled") chrome_options.add_experimental_option("excludeSwitches", ["enable-automation"]) chrome_options.add_experimental_option('useAutomationExtension', False) # 初始化Driver(确保路径正确) service = Service(executable_path="/usr/bin/sensible-browser") driver: WebDriver = webdriver.Chrome(service=service) # 覆盖WebDriver属性 driver.execute_cdp_cmd('Page.addScriptToEvaluateOnNewDocument', { 'source': ''' Object.defineProperty(navigator, 'webdriver', { get: () => undefined }); ''' }) return driver # 安全初始化Driver变量 driver = None try: driver = init_driver() driver.get('http://portal.sx.cmcc/home') # 显式等待元素可见(非仅存在) wait = WebDriverWait(driver, 15) main_content = wait.until( EC.visibility_of_element_located((By.ID, "main-content")) ) print("成功加载目标元素!") except TimeoutException as e: print(f"页面元素加载超时: {str(e)}") except WebDriverException as e: print(f"浏览器驱动异常: {str(e)}") if "net::ERR_NAME_NOT_RESOLVED" in str(e): print("提示:请检查网址是否正确或网络连接是否正常") except Exception as e: print(f"未知错误: {str(e)}") finally: if driver: driver.quit() else: print("警告:Driver未成功初始化,无需退出")
03-13
from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.chrome.service import Service from selenium.webdriver.chrome.options import Options from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from webdriver_manager.chrome import ChromeDriverManager import time import logging from selenium.common.exceptions import TimeoutException # 配置日志 logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s') # 初始化 WebDriver def init_driver(headless=True): chrome_options = Options() if headless: chrome_options.add_argument("--headless") # 无头模式,不打开浏览器窗口 # 设置 User-Agent chrome_options.add_argument("user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.6998.165 Safari/537.36") # 禁用图片加载 prefs = {"profile.managed_default_content_settings.images": 2} chrome_options.add_experimental_option("prefs", prefs) # 忽略 SSL 证书错误 chrome_options.add_argument('--ignore-certificate-errors') chrome_options.add_argument('--allow-insecure-localhost') # 启用 Unsafe SwiftShader chrome_options.add_argument('--enable-unsafe-swiftshader') # 使用 webdriver-manager 自动管理 chromedriver service = Service(ChromeDriverManager().install()) driver = webdriver.Chrome(service=service, options=chrome_options) logging.info("WebDriver 初始化成功") return driver # 提取视频信息 def extract_video_info(driver, url, scroll_pause_time=3, max_scroll_attempts=20): try: driver.get(url) logging.info(f"正在访问 URL: {url}") # 等待初始页面加载 try: WebDriverWait(driver, 20).until( EC.presence_of_element_located((By.ID, "root")) ) except TimeoutException: logging.error("页面加载超时") return # 滚动页面以加载更多内容 last_height = driver.exe
03-28
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Mr.Idleman

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值