removed "delay" from constructor, added user_data_dir

simplify specifying a custom user_data_dir by passing it directly to the constructor.
however if a user_data_dir is specified in the options object,  the one in options will take precedence.
This commit is contained in:
Leon 2021-12-21 16:31:04 +00:00 committed by GitHub
parent b60820a600
commit 8a3870bd6d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 13 additions and 29 deletions

View File

@ -99,6 +99,7 @@ class Chrome(selenium.webdriver.chrome.webdriver.WebDriver):
def __init__( def __init__(
self, self,
user_data_dir=None,
executable_path=None, executable_path=None,
port=0, port=0,
options=None, options=None,
@ -109,9 +110,9 @@ class Chrome(selenium.webdriver.chrome.webdriver.WebDriver):
keep_alive=False, keep_alive=False,
log_level=0, log_level=0,
headless=False, headless=False,
delay=5,
version_main=None, version_main=None,
patcher_force_close=False, patcher_force_close=False,
**kw
): ):
""" """
Creates a new instance of the chrome driver. Creates a new instance of the chrome driver.
@ -120,6 +121,11 @@ class Chrome(selenium.webdriver.chrome.webdriver.WebDriver):
Parameters Parameters
---------- ----------
user_data_dir: str , optional, default: None (creates temp profile)
if user_data_dir is a path to a valid chrome profile directory, use it,
and turn off automatic removal mechanism at exit.
executable_path: str, optional, default: None - use find_chrome_executable executable_path: str, optional, default: None - use find_chrome_executable
Path to the executable. If the default is used it assumes the executable is in the $PATH Path to the executable. If the default is used it assumes the executable is in the $PATH
@ -158,12 +164,6 @@ class Chrome(selenium.webdriver.chrome.webdriver.WebDriver):
Specify whether you want to use the browser in headless mode. Specify whether you want to use the browser in headless mode.
warning: this lowers undetectability and not fully supported. warning: this lowers undetectability and not fully supported.
delay: int, optional, default: 5
delay in seconds to wait before giving back control.
this is used only when using the context manager
(`with` statement) to bypass, for example CloudFlare.
5 seconds is a foolproof value.
version_main: int, optional, default: None (=auto) version_main: int, optional, default: None (=auto)
if you, for god knows whatever reason, use if you, for god knows whatever reason, use
an older version of Chrome. You can specify it's full rounded version number an older version of Chrome. You can specify it's full rounded version number
@ -211,9 +211,10 @@ class Chrome(selenium.webdriver.chrome.webdriver.WebDriver):
options.add_argument("--remote-debugging-host=%s" % debug_host) options.add_argument("--remote-debugging-host=%s" % debug_host)
options.add_argument("--remote-debugging-port=%s" % debug_port) options.add_argument("--remote-debugging-port=%s" % debug_port)
user_data_dir, language, keep_user_data_dir = None, None, None language, keep_user_data_dir = None, bool(user_data_dir)
# see if a custom user profile is specified
# see if a custom user profile is specified in options
for arg in options.arguments: for arg in options.arguments:
if "lang" in arg: if "lang" in arg:
@ -315,14 +316,7 @@ class Chrome(selenium.webdriver.chrome.webdriver.WebDriver):
self.browser_pid = start_detached(options.binary_location, *options.arguments) self.browser_pid = start_detached(options.binary_location, *options.arguments)
# self.browser = subprocess.Popen(
# [options.binary_location, *options.arguments],
# stdin=subprocess.PIPE,
# stdout=subprocess.PIPE,
# stderr=subprocess.PIPE,
# close_fds=IS_POSIX,
# )
super(Chrome, self).__init__( super(Chrome, self).__init__(
executable_path=patcher.executable_path, executable_path=patcher.executable_path,
port=port, port=port,
@ -332,17 +326,7 @@ class Chrome(selenium.webdriver.chrome.webdriver.WebDriver):
service_log_path=service_log_path, service_log_path=service_log_path,
keep_alive=keep_alive, keep_alive=keep_alive,
) )
# intentional
# self.webdriver = selenium.webdriver.chrome.webdriver.WebDriver(
# executable_path=patcher.executable_path,
# port=port,
# options=options,
# service_args=service_args,
# desired_capabilities=desired_capabilities,
# service_log_path=service_log_path,
# keep_alive=keep_alive,
# )
self.reactor = None self.reactor = None
if enable_cdp_events: if enable_cdp_events:
@ -705,7 +689,7 @@ def find_chrome_executable():
candidates = set() candidates = set()
if IS_POSIX: if IS_POSIX:
for item in os.environ.get("PATH").split(os.pathsep): for item in os.environ.get("PATH").split(os.pathsep):
for subitem in ("google-chrome", "chromium", "chromium-browser"): for subitem in ("google-chrome", "chromium", "chromium-browser", "chrome"):
candidates.add(os.sep.join((item, subitem))) candidates.add(os.sep.join((item, subitem)))
if "darwin" in sys.platform: if "darwin" in sys.platform:
candidates.update( candidates.update(