美文网首页
爬虫:Selenium 启动配置说明

爬虫:Selenium 启动配置说明

作者: dex0423 | 来源:发表于2020-12-24 14:46 被阅读0次

Selenium 常用启动配置说明

from selenium import webdriver


option = webdriver.ChromeOptions()

# 手动指定使用的浏览器位置
options.binary_location = r"C:\XXX\xxx\chrome.exe"

# 设置开发者模式启动,该模式下webdriver属性为正常值
options.add_experimental_option("excludeSwitches", ["enable-automation"])

# 添加 User Agent
options.add_argument(f'--user-agent={user_agent}')

# 添加代理
options.add_argument(f'--proxy-server=http://{proxy}')

# 在 root 权限下跑
options.add_argument('--no-sandbox')              

# 除window.navigator.webdriver,否则调用远程webdriver时则会报错
options.add_argument("--disable-blink-features=AutomationControlled")

# 不加载图片, 提升速度
options.add_argument('blink-settings=imagesEnabled=false') 

# 无头浏览
options.add_argument('--headless')              

# 禁用GPU加速
options.add_argument('--disable-gpu')                   

# 禁用提示
options.add_argument('--disable-infobars')              

# 无痕浏览
options.add_argument('--incognito')                     

# 指定浏览器分辨率
options.add_argument("--window-size=1920,1050")

# 启动时窗口最大化
options.add_argument("--start-maximized")

# 禁用缓存
options.add_argument("--disable-application-cache")     

# 不做浏览器默认检查
options.add_argument('--no-default-browser-check) 

# 允许弹窗
options.add_argument("--disable-popup-blocking") 

# 忽略不信任证书
options.add_argument("--ignore-certificate-errors") 

# 禁用通知警告
options.add_argument(’--disable-notifications’) 

# 禁止xss防护
options.add_argument(’--disable-xss-auditor’) 

# 关闭安全策略
options.add_argument(’--disable-web-security’) 

# 禁用webgl
options.add_argument(’--disable-webgl’) 

# 指定临时文件目录
options.add_argument(’--disk-cache-dir={临时文件目录}’)

# 通知(通知用户其浏览器正由自动化测试控制)
options.add_argument(’--enable-automation’) 

# 禁用 java
options.add_argument("--disable-java")   

# 禁用Javascript
options.add_argument("--disable-javascript")   

# 禁用插件
options.add_argument("--disable-plugins")   

# 禁用图像
options.add_argument("--disable-images")

# 禁用扩展
options.add_argument('--disable-extensions')

prefs = {
            'profile.default_content_setting_values': {
                'images': 2,         # 不加载图片
                'javascript': 2,     # 不加载JS
                'notifications' : 2  # 禁用弹窗
            }
        }
options.add_experimental_option("prefs", prefs)
desired_capabilities = DesiredCapabilities.CHROME
# desired_capabilities["pageLoadStrategy"] = "none"
self.browser = webdriver.Chrome(
            # 添加执行路径
            executable_path=r'C:\Program Files\Google\Chrome\Application\chromedriver.exe',    
            chrome_options=options,
            desired_capabilities=desired_capabilities
        )
# 
self.browser.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", {
      "source": """
                 Object.defineProperty(navigator, 'webdriver', {
                      get: () => undefined
                    })
                  """
        })

相关文章

网友评论

      本文标题:爬虫:Selenium 启动配置说明

      本文链接:https://www.haomeiwen.com/subject/pqmxsktx.html