(self)
| 306 | return driver |
| 307 | |
| 308 | def edge_driver(self): |
| 309 | edge_options = webdriver.EdgeOptions() |
| 310 | # 此步骤很重要,设置为开发者模式,防止被各大网站识别出来使用了Selenium |
| 311 | edge_options.add_experimental_option("excludeSwitches", ["enable-automation"]) |
| 312 | edge_options.add_experimental_option("useAutomationExtension", False) |
| 313 | # docker 里运行需要 |
| 314 | edge_options.add_argument("--no-sandbox") |
| 315 | try: |
| 316 | from selenium.webdriver.edge.service import Service |
| 317 | except (ImportError, ModuleNotFoundError): |
| 318 | Service = None |
| 319 | |
| 320 | if self._proxy: |
| 321 | edge_options.add_argument( |
| 322 | "--proxy-server={}".format( |
| 323 | self._proxy() if callable(self._proxy) else self._proxy |
| 324 | ) |
| 325 | ) |
| 326 | if self._user_agent: |
| 327 | edge_options.add_argument( |
| 328 | "user-agent={}".format( |
| 329 | self._user_agent() |
| 330 | if callable(self._user_agent) |
| 331 | else self._user_agent |
| 332 | ) |
| 333 | ) |
| 334 | if not self._load_images: |
| 335 | edge_options.add_experimental_option( |
| 336 | "prefs", {"profile.managed_default_content_settings.images": 2} |
| 337 | ) |
| 338 | |
| 339 | if self._headless: |
| 340 | edge_options.add_argument("--headless") |
| 341 | edge_options.add_argument("--disable-gpu") |
| 342 | |
| 343 | if self._window_size: |
| 344 | edge_options.add_argument( |
| 345 | "--window-size={},{}".format(self._window_size[0], self._window_size[1]) |
| 346 | ) |
| 347 | |
| 348 | if self._download_path: |
| 349 | os.makedirs(self._download_path, exist_ok=True) |
| 350 | prefs = { |
| 351 | "download.prompt_for_download": False, |
| 352 | "download.default_directory": self._download_path, |
| 353 | } |
| 354 | edge_options.add_experimental_option("prefs", prefs) |
| 355 | |
| 356 | # 添加自定义的配置参数 |
| 357 | if self._custom_argument: |
| 358 | for arg in self._custom_argument: |
| 359 | edge_options.add_argument(arg) |
| 360 | |
| 361 | kwargs = self.filter_kwargs(self._kwargs, self.__CHROME_ATTRS__) |
| 362 | if Service is None: |
| 363 | if self._executable_path: |
| 364 | kwargs.update(executable_path=self._executable_path) |
| 365 | elif self._auto_install_driver: |
no test coverage detected