From 373c5b35c05947f5d08a44b3a0a0603cc21e26db Mon Sep 17 00:00:00 2001 From: naisanzaa Date: Sun, 26 Nov 2023 21:41:13 -0800 Subject: [PATCH] selenium: explicit import ChromeWrapper --- automon/integrations/facebook/groups.py | 42 ++++--------------- .../integrations/instagram/client_browser.py | 6 ++- .../integrations/seleniumWrapper/browser.py | 17 ++------ .../seleniumWrapper/browser_types.py | 8 ++-- .../integrations/seleniumWrapper/config.py | 37 +--------------- .../config_webdriver_chrome.py | 2 +- .../seleniumWrapper/tests/test_browser.py | 4 +- .../tests/test_browser_headless.py | 4 +- .../tests/test_browser_useragent.py | 6 ++- .../seleniumWrapper/tests/test_new_browser.py | 4 +- 10 files changed, 35 insertions(+), 95 deletions(-) diff --git a/automon/integrations/facebook/groups.py b/automon/integrations/facebook/groups.py index 17a0764f..d84eb87b 100644 --- a/automon/integrations/facebook/groups.py +++ b/automon/integrations/facebook/groups.py @@ -5,6 +5,7 @@ from automon.log import logger from automon.helpers.sleeper import Sleeper from automon.integrations.seleniumWrapper import SeleniumBrowser +from automon.integrations.seleniumWrapper.config_webdriver_chrome import ChromeWrapper log = logger.logging.getLogger(__name__) log.setLevel(logger.DEBUG) @@ -64,7 +65,7 @@ def __init__(self, url: str = None): Depends on Selenium""" self._url = url - self._browser = None + self._browser = SeleniumBrowser() self._rate_per_minute = 2 self._rate_counter = [] @@ -72,8 +73,6 @@ def __init__(self, url: str = None): def content_unavailable(self): """This content isn't available right now""" - if not self._browser: - return try: xpath_content_unavailble = self._browser.wait_for_xpath(self._xpath_content_unavailble) @@ -91,8 +90,6 @@ def content_unavailable(self): self.screenshot_error() def creation_date(self): - if not self._browser: - return try: xpath_creation_date = self._browser.wait_for_xpath(self._xpath_creation_date) @@ -142,8 +139,6 @@ def average_rate(self): return 0 def history(self): - if not self._browser: - return try: xpath_history = self._browser.wait_for_xpath(self._xpath_history) @@ -181,8 +176,6 @@ def temporarily_blocked(self): self.screenshot_error() def members(self): - if not self._browser: - return try: xpath_members = self._browser.wait_for_xpath(self._xpath_members) @@ -201,8 +194,6 @@ def members(self): self.screenshot_error() def members_count(self): - if not self._browser: - return if self.members: count = [x for x in self.members()] @@ -234,8 +225,6 @@ def must_login(self): self.screenshot_error() def posts_monthly(self): - if not self._browser: - return try: xpath_monthly_posts = self._browser.wait_for_xpath(self._xpath_posts_monthly) @@ -253,8 +242,6 @@ def posts_monthly(self): self.screenshot_error() def posts_monthly_count(self): - if not self._browser: - return if self.posts_monthly: count = [x for x in self.posts_monthly()] @@ -266,8 +253,6 @@ def posts_monthly_count(self): return posts_monthly_count def posts_today(self): - if not self._browser: - return try: xpath_posts_today = self._browser.wait_for_xpath(self._xpath_posts_today) @@ -285,8 +270,6 @@ def posts_today(self): self.screenshot_error() def posts_today_count(self): - if not self._browser: - return if self.posts_today: count = [x for x in self.posts_today()] @@ -298,8 +281,6 @@ def posts_today_count(self): return posts_today_count def privacy(self): - if not self._browser: - return try: xpath_privacy = self._browser.wait_for_xpath(self._xpath_privacy) @@ -317,8 +298,6 @@ def privacy(self): self.screenshot_error() def privacy_details(self): - if not self._browser: - return try: xpath_privacy_details = self._browser.wait_for_xpath(self._xpath_privacy_details) @@ -336,8 +315,6 @@ def privacy_details(self): self.screenshot_error() def title(self) -> str: - if not self._browser: - return try: xpath_title = self._browser.wait_for_xpath(self._xpath_title) @@ -368,8 +345,6 @@ def url_cleaner(url: str): return url def visible(self) -> str: - if not self._browser: - return try: xpath_visible = self._browser.wait_for_xpath(self._xpath_visible) @@ -566,20 +541,19 @@ def set_url(self, url: str) -> str: def start(self, headless: bool = True, random_user_agent: bool = False, set_user_agent: str = None): """start new instance of selenium""" - self._browser = SeleniumBrowser() + self._browser.config.webdriver_wrapper = ChromeWrapper() if headless: - self._browser.set_webdriver().Chrome().in_headless().set_locale_experimental() - # self._browser.set_webdriver().Chrome().enable_headless().set_locale_experimental() + self._browser.config.webdriver_wrapper.enable_headless().set_locale_experimental() else: - self._browser.set_webdriver().Chrome().set_locale_experimental() + self._browser.config.webdriver_wrapper.set_locale_experimental() if random_user_agent: - self._browser.set_webdriver().Chrome().set_user_agent( + self._browser.config.webdriver_wrapper.set_user_agent( self._browser.get_random_user_agent() ) elif set_user_agent: - self._browser.set_webdriver().Chrome().set_user_agent( + self._browser.config.webdriver_wrapper.set_user_agent( set_user_agent ) @@ -587,7 +561,7 @@ def start(self, headless: bool = True, random_user_agent: bool = False, set_user browser=self._browser ))) browser = self._browser.run() - self._browser.set_webdriver().Chrome().set_window_size(width=1920 * 0.6, height=1080) + self._browser.config.webdriver_wrapper.set_window_size(width=1920 * 0.6, height=1080) return browser def stop(self): diff --git a/automon/integrations/instagram/client_browser.py b/automon/integrations/instagram/client_browser.py index 9bcaa3ef..d8d9e822 100644 --- a/automon/integrations/instagram/client_browser.py +++ b/automon/integrations/instagram/client_browser.py @@ -2,6 +2,7 @@ from automon.log import logger from automon.integrations.seleniumWrapper.browser import SeleniumBrowser +from automon.integrations.seleniumWrapper.config_webdriver_chrome import ChromeWrapper from automon.helpers.sleeper import Sleeper # from automon.integrations.minioWrapper import MinioClient @@ -28,13 +29,14 @@ def __init__(self, """Instagram Browser Client""" self.config = config or InstagramConfig(login=login, password=password) self.browser = SeleniumBrowser() + self.browser.config.webdriver_wrapper = ChromeWrapper() self.useragent = self.browser.get_random_user_agent() if headless: - self.browser.set_webdriver().Chrome().in_headless().set_user_agent(self.useragent) + self.browser.config.webdriver_wrapper.in_headless().set_user_agent(self.useragent) else: - self.browser.set_webdriver().Chrome().set_user_agent(self.useragent) + self.browser.config.webdriver_wrapper.set_user_agent(self.useragent) def __repr__(self): return f'{self.__dict__}' diff --git a/automon/integrations/seleniumWrapper/browser.py b/automon/integrations/seleniumWrapper/browser.py index 10ecedaa..3e97a9a9 100644 --- a/automon/integrations/seleniumWrapper/browser.py +++ b/automon/integrations/seleniumWrapper/browser.py @@ -3,7 +3,6 @@ import base64 import datetime import tempfile -import functools import selenium import selenium.webdriver @@ -19,7 +18,6 @@ from automon.integrations.requestsWrapper import RequestsClient from .config import SeleniumConfig -from .browser_types import SeleniumBrowserType from .user_agents import SeleniumUserAgentBuilder log = logger.logging.getLogger(__name__) @@ -100,10 +98,6 @@ def request_status(self): except: pass - # @property - # def type(self) -> SeleniumBrowserType: - # return SeleniumBrowserType(self.config) - @property def url(self): return self.current_url @@ -121,7 +115,7 @@ def current_url(self): @property def window_size(self): - return self.config.set_webdriver().window_size + return self.config.webdriver_wrapper.window_size def _screenshot_name(self, prefix=None): """Generate a unique filename""" @@ -365,7 +359,7 @@ def get_page_source_beautifulsoup(self, markdup: str = None, features: str = 'lx markdup = self.get_page_source() return BeautifulSoup(markup=markdup, features=features) - def get_random_user_agent(self, filter: list or str = None, case_sensitive: bool = False) -> list: + def get_random_user_agent(self, filter: list or str = None, case_sensitive: bool = False) -> str: return SeleniumUserAgentBuilder().get_random(filter=filter, case_sensitive=case_sensitive) def get_screenshot_as_base64(self, **kwargs): @@ -464,15 +458,12 @@ def save_screenshot( return False - def set_webdriver(self): - return self.config - def set_window_size(self, width=1920, height=1080, device_type=None) -> bool: """set browser resolution""" try: - self.config.set_webdriver().webdriver_wrapper.set_window_size(width=width, height=height, - device_type=device_type) + self.config.webdriver_wrapper.set_window_size(width=width, height=height, + device_type=device_type) except Exception as error: message, session, stacktrace = self.error_parsing(error) log.error(str(dict( diff --git a/automon/integrations/seleniumWrapper/browser_types.py b/automon/integrations/seleniumWrapper/browser_types.py index 1a4b8238..35288e63 100644 --- a/automon/integrations/seleniumWrapper/browser_types.py +++ b/automon/integrations/seleniumWrapper/browser_types.py @@ -49,8 +49,8 @@ def chrome(self, options: list = None) -> Chrome: try: if self.chromedriver: - return self.webdriver.Chrome(executable_path=self.chromedriver, options=chrome_options) - return self.webdriver.Chrome(options=chrome_options) + return self.webdriver.ChromeWrapper(executable_path=self.chromedriver, options=chrome_options) + return self.webdriver.ChromeWrapper(options=chrome_options) except Exception as e: log.error(f'Browser not set. {e}') @@ -77,8 +77,8 @@ def chrome_headless(self, options: list = None, **kwargs) -> Chrome: try: if self.chromedriver: - return self.webdriver.Chrome(self.chromedriver, options=chrome_options, **kwargs) - return self.webdriver.Chrome(options=chrome_options, **kwargs) + return self.webdriver.ChromeWrapper(self.chromedriver, options=chrome_options, **kwargs) + return self.webdriver.ChromeWrapper(options=chrome_options, **kwargs) except Exception as e: log.error(f'Browser not set. {e}') diff --git a/automon/integrations/seleniumWrapper/config.py b/automon/integrations/seleniumWrapper/config.py index 9cdfa095..a336261b 100644 --- a/automon/integrations/seleniumWrapper/config.py +++ b/automon/integrations/seleniumWrapper/config.py @@ -3,8 +3,6 @@ from automon.log import logger from automon.helpers.osWrapper import environ -from .config_webdriver_chrome import ConfigChrome - log = logger.logging.getLogger(__name__) log.setLevel(logger.DEBUG) @@ -12,15 +10,11 @@ class SeleniumConfig(object): def __init__(self): self._webdriver = None - self._webdriver_wrapper = None + self.webdriver_wrapper = None self._cookies_base64 = environ('SELENIUM_COOKIES_BASE64') self._cookies_file = environ('SELENIUM_COOKIES_FILE') - self._chrome = ConfigChrome() - self._edge = NotImplemented - self._firefox = NotImplemented - @property def webdriver(self): try: @@ -28,10 +22,6 @@ def webdriver(self): except: return self._webdriver - @property - def webdriver_wrapper(self): - return self._webdriver_wrapper - @property def window_size(self): """get window size @@ -50,28 +40,6 @@ def cookies_file(self): log.info(f'{self._cookies_file}') return self._cookies_file - def Chrome(self): - """selenium Chrome webdriver - - """ - self._webdriver_wrapper = self._chrome - log.info(str(dict( - webdriver_wrapper=self._webdriver_wrapper - ))) - return self.webdriver_wrapper - - def Edge(self): - """selenium Edge webdriver - - """ - return self._edge - - def Firefox(self): - """selenium Firefox webdriver - - """ - return self._firefox - def run(self): """run webdriver""" run = self.webdriver_wrapper.run() @@ -81,9 +49,6 @@ def run(self): ))) return run - def set_webdriver(self): - return self - def start(self): """alias to run""" return self.run() diff --git a/automon/integrations/seleniumWrapper/config_webdriver_chrome.py b/automon/integrations/seleniumWrapper/config_webdriver_chrome.py index 025a22b7..e4ab4410 100644 --- a/automon/integrations/seleniumWrapper/config_webdriver_chrome.py +++ b/automon/integrations/seleniumWrapper/config_webdriver_chrome.py @@ -12,7 +12,7 @@ log.setLevel(logger.DEBUG) -class ConfigChrome(object): +class ChromeWrapper(object): def __init__(self): self._webdriver = None diff --git a/automon/integrations/seleniumWrapper/tests/test_browser.py b/automon/integrations/seleniumWrapper/tests/test_browser.py index e325a34d..da0a856d 100644 --- a/automon/integrations/seleniumWrapper/tests/test_browser.py +++ b/automon/integrations/seleniumWrapper/tests/test_browser.py @@ -1,9 +1,11 @@ import unittest from automon.integrations.seleniumWrapper.browser import SeleniumBrowser +from automon.integrations.seleniumWrapper.config_webdriver_chrome import ChromeWrapper browser = SeleniumBrowser() -browser.config.set_webdriver().Chrome().enable_defaults().enable_headless() +browser.config.webdriver_wrapper = ChromeWrapper() +browser.config.webdriver_wrapper.enable_defaults().enable_headless() class SeleniumClientTest(unittest.TestCase): diff --git a/automon/integrations/seleniumWrapper/tests/test_browser_headless.py b/automon/integrations/seleniumWrapper/tests/test_browser_headless.py index ab61a2ea..1b829f70 100644 --- a/automon/integrations/seleniumWrapper/tests/test_browser_headless.py +++ b/automon/integrations/seleniumWrapper/tests/test_browser_headless.py @@ -1,9 +1,11 @@ import unittest from automon.integrations.seleniumWrapper.browser import SeleniumBrowser +from automon.integrations.seleniumWrapper.config_webdriver_chrome import ChromeWrapper browser = SeleniumBrowser() -browser.config.set_webdriver().Chrome().enable_defaults().enable_headless() +browser.config.webdriver_wrapper = ChromeWrapper() +browser.config.webdriver_wrapper.enable_defaults().enable_headless() class SeleniumClientTest(unittest.TestCase): diff --git a/automon/integrations/seleniumWrapper/tests/test_browser_useragent.py b/automon/integrations/seleniumWrapper/tests/test_browser_useragent.py index e38f3e7f..0352e7f6 100644 --- a/automon/integrations/seleniumWrapper/tests/test_browser_useragent.py +++ b/automon/integrations/seleniumWrapper/tests/test_browser_useragent.py @@ -1,13 +1,15 @@ import unittest from automon.integrations.seleniumWrapper.browser import SeleniumBrowser +from automon.integrations.seleniumWrapper.config_webdriver_chrome import ChromeWrapper browser = SeleniumBrowser() -browser.config.set_webdriver().Chrome().enable_defaults().enable_headless() +browser.config.webdriver_wrapper = ChromeWrapper() +browser.config.webdriver_wrapper.enable_defaults().enable_headless() agent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:112.0) Gecko/20100101 Firefox/112.0' -browser.config.set_webdriver().webdriver_wrapper.set_user_agent(agent) +browser.config.webdriver_wrapper.set_user_agent(agent) class SeleniumClientTest(unittest.TestCase): diff --git a/automon/integrations/seleniumWrapper/tests/test_new_browser.py b/automon/integrations/seleniumWrapper/tests/test_new_browser.py index cef9a1d7..bcde24f1 100644 --- a/automon/integrations/seleniumWrapper/tests/test_new_browser.py +++ b/automon/integrations/seleniumWrapper/tests/test_new_browser.py @@ -1,9 +1,11 @@ import unittest from automon.integrations.seleniumWrapper.browser import SeleniumBrowser +from automon.integrations.seleniumWrapper.config_webdriver_chrome import ChromeWrapper browser = SeleniumBrowser() -browser.config.set_webdriver().Chrome().enable_defaults().enable_headless() +browser.config.webdriver_wrapper = ChromeWrapper() +browser.config.webdriver_wrapper.enable_defaults().enable_headless() class SeleniumClientTest(unittest.TestCase):