From 69d1b518d824a930b636f44fd3142acde59d5e71 Mon Sep 17 00:00:00 2001 From: zxdawn Date: Tue, 13 Dec 2022 12:33:03 +0800 Subject: [PATCH 1/2] support undetected-chromedriver --- splinter/browser.py | 3 ++ .../driver/webdriver/undetected_chrome.py | 45 +++++++++++++++++++ 2 files changed, 48 insertions(+) create mode 100644 splinter/driver/webdriver/undetected_chrome.py diff --git a/splinter/browser.py b/splinter/browser.py index 53f578263..9dd6c39f3 100644 --- a/splinter/browser.py +++ b/splinter/browser.py @@ -28,6 +28,7 @@ _DRIVERS: Dict[str, Union[None, Type[DriverAPI]]] = { 'chrome': None, + 'undetected_chrome': None, 'edge': None, 'firefox': None, 'remote': None, @@ -38,10 +39,12 @@ try: from splinter.driver.webdriver.chrome import WebDriver as ChromeWebDriver + from splinter.driver.webdriver.undetected_chrome import WebDriver as undetected_ChromeWebDriver from splinter.driver.webdriver.firefox import WebDriver as FirefoxWebDriver from splinter.driver.webdriver.remote import WebDriver as RemoteWebDriver _DRIVERS['chrome'] = ChromeWebDriver + _DRIVERS['undetected_chrome'] = undetected_ChromeWebDriver _DRIVERS['firefox'] = FirefoxWebDriver _DRIVERS['remote'] = RemoteWebDriver except ImportError as e: diff --git a/splinter/driver/webdriver/undetected_chrome.py b/splinter/driver/webdriver/undetected_chrome.py new file mode 100644 index 000000000..5037f8a17 --- /dev/null +++ b/splinter/driver/webdriver/undetected_chrome.py @@ -0,0 +1,45 @@ +# -*- coding: utf-8 -*- + +# Copyright 2012 splinter authors. All rights reserved. +# Use of this source code is governed by a BSD-style +# license that can be found in the LICENSE file. + +import undetected_chromedriver as uc +from selenium.webdriver.chrome.options import Options +from splinter.driver.webdriver import BaseWebDriver + + +class WebDriver(BaseWebDriver): + + driver_name = "undetected_Chrome" + + def __init__( + self, + options=None, + user_agent=None, + wait_time=2, + fullscreen=False, + incognito=False, + headless=False, + **kwargs + ): + + options = options or Options() + + if user_agent is not None: + options.add_argument("--user-agent=" + user_agent) + + if incognito: + options.add_argument("--incognito") + + if fullscreen: + options.add_argument("--kiosk") + + if headless: + options.add_argument("--headless") + options.add_argument("--disable-gpu") + + # driver = Chrome(options=options, **kwargs) + driver = uc.Chrome() + + super(WebDriver, self).__init__(driver, wait_time) From d67911852fdcfe61c6c1d320b8c7b7af56fb7588 Mon Sep 17 00:00:00 2001 From: zxdawn Date: Tue, 13 Dec 2022 13:55:17 +0800 Subject: [PATCH 2/2] use uc option --- splinter/driver/webdriver/undetected_chrome.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/splinter/driver/webdriver/undetected_chrome.py b/splinter/driver/webdriver/undetected_chrome.py index 5037f8a17..874797a60 100644 --- a/splinter/driver/webdriver/undetected_chrome.py +++ b/splinter/driver/webdriver/undetected_chrome.py @@ -5,7 +5,6 @@ # license that can be found in the LICENSE file. import undetected_chromedriver as uc -from selenium.webdriver.chrome.options import Options from splinter.driver.webdriver import BaseWebDriver @@ -24,7 +23,7 @@ def __init__( **kwargs ): - options = options or Options() + options = uc.ChromeOptions() if user_agent is not None: options.add_argument("--user-agent=" + user_agent) @@ -39,7 +38,6 @@ def __init__( options.add_argument("--headless") options.add_argument("--disable-gpu") - # driver = Chrome(options=options, **kwargs) - driver = uc.Chrome() + driver = uc.Chrome(options=options, **kwargs) super(WebDriver, self).__init__(driver, wait_time)