Skip to content

Commit

Permalink
fix(undetected_browser): manual mixin and test.
Browse files Browse the repository at this point in the history
  • Loading branch information
2e0byo committed Jan 27, 2022
1 parent 81ce832 commit 86ff1a7
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 7 deletions.
14 changes: 14 additions & 0 deletions tests/test_undetected_browser.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from selenium.webdriver.common.by import By
from yadc.undetected_browser import ManualBusterUndetectedBrowser


def test_manual_buster_undetected_browser(tmp_path):
br = ManualBusterUndetectedBrowser(
buster=True, profile_dir=(tmp_path) / "blank-profile"
)
unique_url = (
"chrome-extension://mpbjkejclgfgadiemmefgebjfooflfhl/src/options/index.html"
)
with br as driver:
driver.get(unique_url)
assert "Buster" in driver.page_source
27 changes: 20 additions & 7 deletions yadc/undetected_browser.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
"""Browser and CaptchaChrome objects based around `undetected_chromedriver`."""
from pathlib import Path

from .browser import CaptchaChromeBase, Browser, TorBrowser, BrowserError
import undetected_chromedriver as us
import undetected_chromedriver as uc
from selenium.webdriver.common.by import By

from .browser import Browser, BrowserError, CaptchaChromeBase, TorBrowser


class UndetectedCaptchaChrome(CaptchaChromeBase, uc.Chrome):
Expand All @@ -16,7 +18,7 @@ def launch_chrome(self):
pass

def _connect(self, *args, chrome_options: uc.ChromeOptions = None):
chrome_options = ChromeOptions or uc.ChromeOptions()
chrome_options = chrome_options or uc.ChromeOptions()
if self._buster:
chrome_options.add_argument(self.buster_arg)

Expand All @@ -32,7 +34,7 @@ def tor_arg(self):
return f'--proxy-server="socks4://localhost:{self.tor_port}"'

def _connect(self, *args, chrome_options: uc.ChromeOptions = None):
chrome_options = ChromeOptions or uc.ChromeOptions()
chrome_options = chrome_options or uc.ChromeOptions()
chrome_options.add_argument(self.tor_arg)
return super()._connect(*args, chrome_options=chrome_options)

Expand All @@ -41,22 +43,33 @@ class ManualBusterMixin:
BUSTER_URL = "https://chrome.google.com/webstore/detail/buster-captcha-solver-for/mpbjkejclgfgadiemmefgebjfooflfhl?hl=en"
PROFILE_DIR = "blank-profile"

def __init__(self, *args, profile_dir: Path = None, **kwargs):
buster = kwargs.get("buster", None)
if buster:
del kwargs["buster"]
super().__init__(*args, **kwargs)
if buster:
self._buster = buster
self.profile_dir = profile_dir or self.PROFILE_DIR

def generate_profile(self):
"""Generate a profile and then install buster manually."""
chrome_options = uc.ChromeOptions()
chrome_options.user_data_dir = "blank-profile"
chrome_options.user_data_dir = self.profile_dir
driver = UndetectedCaptchaChrome(options=chrome_options)
driver.get(self.BUSTER_URL)
driver.find_element(By.XPATH, "//*[text()='I agree']").click()
driver.find_element(
By.CSS_SELECTOR, ".g-c-R.webstore-test-button-label"
).click()
input("Accept installation and press enter: ")
driver.close()
del driver

def _connect(self, *args, chrome_options: uc.ChromeOptions = None):
chrome_options = chrome_options or uc.ChromeOptions()
if self._buster:
profile_dir = Path(self.PROFILE_DIR)
profile_dir = Path(self.profile_dir)
if not profile_dir.is_dir():
if profile_dir.exists():
raise BrowserError(
Expand All @@ -65,7 +78,7 @@ def _connect(self, *args, chrome_options: uc.ChromeOptions = None):
self.generate_profile()
chrome_options.user_data_dir = self.PROFILE_DIR
self._buster = None
return super()._connext(*args, chrome_options=chrome_options)
return super()._connect(*args, chrome_options=chrome_options)


class ManualBusterUndetectedBrowser(ManualBusterMixin, UndetectedBrowser):
Expand Down

0 comments on commit 86ff1a7

Please # to comment.