(self)
| 202 | return driver |
| 203 | |
| 204 | def chrome_driver(self): |
| 205 | chrome_options = webdriver.ChromeOptions() |
| 206 | # 此步骤很重要,设置为开发者模式,防止被各大网站识别出来使用了Selenium |
| 207 | chrome_options.add_experimental_option("excludeSwitches", ["enable-automation"]) |
| 208 | chrome_options.add_experimental_option("useAutomationExtension", False) |
| 209 | # docker 里运行需要 |
| 210 | chrome_options.add_argument("--no-sandbox") |
| 211 | try: |
| 212 | from selenium.webdriver.chrome.service import Service |
| 213 | except (ImportError, ModuleNotFoundError): |
| 214 | Service = None |
| 215 | |
| 216 | if self._proxy: |
| 217 | chrome_options.add_argument( |
| 218 | "--proxy-server={}".format( |
| 219 | self._proxy() if callable(self._proxy) else self._proxy |
| 220 | ) |
| 221 | ) |
| 222 | if self._user_agent: |
| 223 | chrome_options.add_argument( |
| 224 | "user-agent={}".format( |
| 225 | self._user_agent() |
| 226 | if callable(self._user_agent) |
| 227 | else self._user_agent |
| 228 | ) |
| 229 | ) |
| 230 | if not self._load_images: |
| 231 | chrome_options.add_experimental_option( |
| 232 | "prefs", {"profile.managed_default_content_settings.images": 2} |
| 233 | ) |
| 234 | |
| 235 | if self._headless: |
| 236 | chrome_options.add_argument("--headless") |
| 237 | chrome_options.add_argument("--disable-gpu") |
| 238 | |
| 239 | if self._window_size: |
| 240 | chrome_options.add_argument( |
| 241 | "--window-size={},{}".format(self._window_size[0], self._window_size[1]) |
| 242 | ) |
| 243 | |
| 244 | if self._download_path: |
| 245 | os.makedirs(self._download_path, exist_ok=True) |
| 246 | prefs = { |
| 247 | "download.prompt_for_download": False, |
| 248 | "download.default_directory": self._download_path, |
| 249 | } |
| 250 | chrome_options.add_experimental_option("prefs", prefs) |
| 251 | |
| 252 | # 添加自定义的配置参数 |
| 253 | if self._custom_argument: |
| 254 | for arg in self._custom_argument: |
| 255 | chrome_options.add_argument(arg) |
| 256 | |
| 257 | kwargs = self.filter_kwargs(self._kwargs, self.__CHROME_ATTRS__) |
| 258 | if Service is None: |
| 259 | if self._executable_path: |
| 260 | kwargs.update(executable_path=self._executable_path) |
| 261 | elif self._auto_install_driver: |
no test coverage detected