selenium常用配置
def init_chrome_options(self,): chrome_options = webdriver.ChromeOptions() # 设置浏览器初始 位置x,y & 宽高x,y chrome_options.add_argument(f'--window-position={217},{172}') chrome_options.add_argument(f'--window-size={1200},{1000}') # 关闭自动测试状态显示 // 会导致浏览器报:请停用开发者模式 # window.navigator.webdriver还是返回True,当返回undefined时应该才可行。 chrome_options.add_experimental_option("excludeSwitches", ['enable-automation']) # 关闭开发者模式 chrome_options.add_experimental_option("useAutomationExtension", False) # 禁止图片加载 # prefs = {"profile.managed_default_content_settings.images": 2} # chrome_options.add_experimental_option("prefs", prefs) # 设置中文 chrome_options.add_argument('lang=zh_CN.UTF-8') # 更换头部 chrome_options.add_argument( 'user-agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36"') path = './../chromedriver' driver = webdriver.Chrome(executable_path=path, options=chrome_options) driver.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", { "source": """Object.defineProperty(navigator, 'webdriver', {get: () => undefined})""", }) return driver ———————————————— 版权声明:本文为CSDN博主「井中蛙@」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。 原文链接:https://blog.csdn.net/happiness0617/article/details/109264521