MENU

记录selenium + chrome的一些配置

October 9, 2021 • 数据采集与数据分析(python)

chrome_options = webdriver.ChromeOptions()
        if visible == 0:
            chrome_options.add_argument('--headless')  # 无头浏览器
        # option.add_argument("--auto-open-devtools-for-tabs") #打开devtools
        # chrome_options.add_argument('--disable-extensions')  # 禁用拓展
        # chrome_options.add_argument('--profile-directory=Default')  # 自定义配置文件
        # chrome_options.add_argument('--incognito')  # 让浏览器直接以隐身模式启动
        # chrome_options.add_argument('--disable-plugins-discovery')
        # chrome_options.add_argument('--disable-logging')  # 禁用日志生成
        # chrome_options.add_argument('--disable-gpu')  # 关闭GPU加速
        # chrome_options.add_argument('--no-sandbox')  # 取消沙盒模式
        # chrome_options.add_argument('--single-process')  # 单进程运行
        chrome_options.add_argument('--disable-dev-shm-usage')  # docker部署时分配空间
        chrome_options.add_argument('user-agent="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.82 Safari/537.36"')
        chrome_options.add_argument('blink-settings=imagesEnabled=False')  # 无图片加载

        chrome_options.add_experimental_option('useAutomationExtension', False)  # 解决软件正在收到自动化检测
        chrome_options.add_experimental_option('excludeSwitches', ['enable-automation'])
        chrome_options.add_argument("--disable-blink-features=AutomationControlled")  # 去掉了webdriver痕迹
        if proxies:
            chrome_options.add_argument(f'--proxy-server=socks5://47.241.106.251:1080')
            # proxyauth_plugin_path = create_proxyauth_extension(
            #     proxy_host=proxies.split("@")[1].split(':')[0],
            #     proxy_port=proxies.split(':')[-1],
            #     proxy_username=proxies.split("//")[1].split(':')[0],
            #     proxy_password=proxies.split(":")[2].split('@')[0]
            # )
            # chrome_options.add_extension(proxyauth_plugin_path)

        filepath = get_root_path() + self._get_driver_path()
        self.driver = webdriver.Chrome(executable_path=filepath, chrome_options=chrome_options)
        self.driver.set_page_load_timeout(30)  # 浏览器页面加载超时时间
        self.driver.set_script_timeout(10)
        self.driver.implicitly_wait(10)

        injected_javascript = '''
            // overwrite the `languages` property to use a custom getter
            Object.defineProperty(navigator, "languages", {
              get: function() {
                return ["zh-CN","zh","zh-TW","en-US","en"];
              }
            });
            // Overwrite the `plugins` property to use a custom getter.
            Object.defineProperty(navigator, 'plugins', {
              get: () => [1, 2, 3, 4, 5],
            });
            // Pass the Webdriver test
            Object.defineProperty(navigator, 'webdriver', {
              get: () => false,
            });
            // Pass the Chrome Test.
            // We can mock this in as much depth as we need for the test.
            window.navigator.chrome = {
              runtime: {},
              // etc.
            };
            // Pass the Permissions Test.
            const originalQuery = window.navigator.permissions.query;
            window.navigator.permissions.query = (parameters) => (
              parameters.name === 'notifications' ?
                Promise.resolve({ state: Notification.permission }) :
                originalQuery(parameters)
            );

            '''
        self.driver.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", {
            "source": injected_javascript
        })
        self.driver.maximize_window()