Skip to content

Commit

Permalink
2.2.1 Fixed issue where arguments were not passed along to chrome. So…
Browse files Browse the repository at this point in the history
…me small improvements for v2 headless, which is still detectable, but it's getting better....,
  • Loading branch information
ultrafunkamsterdam committed Mar 25, 2021
1 parent 853f299 commit b3484a8
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 20 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

setup(
name="undetected-chromedriver",
version="2.2.0",
version="2.2.1",
packages=["undetected_chromedriver"],
install_requires=["selenium",],
url="https://github.com/ultrafunkamsterdam/undetected-chromedriver",
Expand Down
55 changes: 36 additions & 19 deletions undetected_chromedriver/v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,8 @@ def __init__(
self.options = options
self.user_data_dir = user_data_dir

extra_args = []
extra_args = options.arguments

if options.headless:
extra_args.append("--headless")
extra_args.append("--window-size=1920,1080")
Expand All @@ -191,7 +192,7 @@ def __init__(
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)

self.webdriver = selenium.webdriver.chrome.webdriver.WebDriver(
executable_path=p.target_path,
port=port,
Expand All @@ -207,7 +208,10 @@ def __init__(

orig_get = self.webdriver.get

logger.info("setting properties for headless")

def get_wrapped(*args, **kwargs):

if self.execute_script("return navigator.webdriver"):
self.execute_cdp_cmd(
"Page.addScriptToEvaluateOnNewDocument",
Expand All @@ -224,10 +228,20 @@ def get_wrapped(*args, **kwargs):
: target[key]
})
});
Object.defineProperty(Notification, "permission", {
configurable: true,
enumerable: true,
get: () => {
return "unknown"
},
});
"""
},
)

logger.info("removing headless from user-agent string")

self.execute_cdp_cmd(
"Network.setUserAgentOverride",
{
Expand All @@ -236,21 +250,22 @@ def get_wrapped(*args, **kwargs):
).replace("Headless", "")
},
)
logger.info("fixing notifications permission in headless browsers")

if emulate_touch:
self.execute_cdp_cmd(
"Page.addScriptToEvaluateOnNewDocument",
{
"source": """
Object.defineProperty(navigator, 'maxTouchPoints', {
get: () => 1
})"""
},
)
return orig_get(*args, **kwargs)

self.webdriver.get = get_wrapped

if emulate_touch:
self.execute_cdp_cmd(
"Page.addScriptToEvaluateOnNewDocument",
{
"source": """
Object.defineProperty(navigator, 'maxTouchPoints', {
get: () => 1
})"""
},
)

def __getattribute__(self, attr):
try:
return object.__getattribute__(self, attr)
Expand All @@ -263,13 +278,11 @@ def __getattribute__(self, attr):
def __dir__(self):
return object.__dir__(self) + object.__dir__(self.webdriver)


def start_session(self, capabilities=None, browser_profile=None):
if not capabilities:
capabilities = self.options.to_capabilities()
self.webdriver.start_session(capabilities, browser_profile)


def get_in(self, url: str, delay=2, factor=1):
"""
:param url: str
Expand Down Expand Up @@ -310,20 +323,24 @@ def get_in(self, url: str, delay=2, factor=1):
self.start_session()

def quit(self):
logger.debug("closing webdriver")
try:
self.webdriver.quit()
except Exception: # noqa
pass
try:
logger.debug("killing browser")
self.browser.kill()
self.browser.wait(1)
except TimeoutError as e:
logger.debug(e, exc_info=True)
except Exception: # noqa
pass
try:
self.webdriver.quit()
except Exception: # noqa
pass
try:
logger.debug("removing profile : %s" % self.user_data_dir)
shutil.rmtree(self.user_data_dir, ignore_errors=False)
except PermissionError:
logger.debug("permission error. files are still in use/locked. retying...")
time.sleep(1)
self.quit()

Expand Down

0 comments on commit b3484a8

Please sign in to comment.