From d9a2fb70c0e3beb745c5dc0b86b08a95262bdda1 Mon Sep 17 00:00:00 2001 From: naisanzaa Date: Thu, 25 Jul 2024 01:49:14 -0700 Subject: [PATCH] selenium: better logging --- .../integrations/seleniumWrapper/browser.py | 77 +++++++++++-------- 1 file changed, 44 insertions(+), 33 deletions(-) diff --git a/automon/integrations/seleniumWrapper/browser.py b/automon/integrations/seleniumWrapper/browser.py index b9278588..581148b0 100644 --- a/automon/integrations/seleniumWrapper/browser.py +++ b/automon/integrations/seleniumWrapper/browser.py @@ -323,7 +323,9 @@ async def autosave_cookies(self) -> bool: async def delete_all_cookies(self) -> None: result = self.webdriver.delete_all_cookies() - logger.info(f'{True}') + logger.debug(dict( + delete_all_cookies='done' + )) return result async def _url_filename(self, url: str): @@ -331,37 +333,46 @@ async def _url_filename(self, url: str): hostname = parsed.hostname cookie_file = f'cookies-{hostname}.json' logger.debug(dict( - _url_filename=cookie_file + cookie_file=cookie_file )) return cookie_file async def get_cookie(self, name: str) -> dict: - result = self.webdriver.get_cookie(name=name) - logger.info(f'{result}') - return result + get_cookie = self.webdriver.get_cookie(name=name) + logger.debug(dict( + name=name, + get_cookie=get_cookie, + )) + return get_cookie async def get_cookies(self) -> [dict]: - cookies = self.webdriver.get_cookies() + get_cookies = self.webdriver.get_cookies() logger.debug(dict( - get_cookies=len(cookies) + get_cookies=len(get_cookies) )) - return cookies + return get_cookies - async def get_cookies_base64(self) -> base64: - cookies = await self.get_cookies() - logger.debug(f'{True}') - return base64.b64encode( - json.dumps(cookies).encode() + async def get_cookies_base64(self) -> str: + get_cookies_base64 = await self.get_cookies() + get_cookies_base64 = base64.b64encode( + json.dumps(get_cookies_base64).encode() ).decode() + logger.debug(dict( + get_cookies_base64=get_cookies_base64 + )) + return get_cookies_base64 + async def get_cookies_json(self) -> json.dumps: - cookies = await self.get_cookies() + get_cookies_json = await self.get_cookies() + get_cookies_json = json.dumps(get_cookies_json) + logger.debug(dict( - get_cookies_json=len(cookies) + get_cookies_json=f'{len(get_cookies_json)} B', )) - return json.dumps(cookies) + return get_cookies_json - async def get_cookies_summary(self): + async def get_cookies_summary(self) -> dict: result = await self.get_cookies() summary = {} if result: @@ -376,12 +387,12 @@ async def get_cookies_summary(self): else: summary[domain] = [cookie] - logger.debug(f'{summary}') + logger.debug(summary) return summary async def close(self): """close browser""" - logger.info(f'closed') + logger.info(f'closing webdriver') self.webdriver.close() @staticmethod @@ -418,8 +429,8 @@ async def find_anything( find all tags find all matches within meta data """ - logger.info(dict( - current_url=self.current_url, + logger.debug(dict( + find_anything=self.current_url, value=value, by=by, case_sensitive=case_sensitive, @@ -507,8 +518,8 @@ async def find_elements( by: selenium.webdriver.common.by.By, **kwargs) -> list: """find elements""" - logger.info(dict( - current_url=self.current_url, + logger.debug(dict( + find_elements=self.current_url, value=value, by=by, )) @@ -521,7 +532,7 @@ async def find_xpath( **kwargs) -> selenium.webdriver.Chrome.find_element: """find xpath""" logger.info(dict( - current_url=self.current_url, + find_xpath=self.current_url, value=value, by=by, )) @@ -535,11 +546,11 @@ async def get(self, url: str, **kwargs) -> bool: try: if self.webdriver.get(url, **kwargs) is None: - logger.info(str(dict( - url=url, + logger.debug(dict( + get=url, current_url=self.current_url, kwargs=kwargs - ))) + )) if self.config.cookies_autosave: await self.autosave_cookies() @@ -552,9 +563,9 @@ async def get(self, url: str, **kwargs) -> bool: return False - async def get_page(self, *args, **kwargs): + async def get_page(self, *args, **kwargs) -> bool: """alias to get""" - return self.get(*args, **kwargs) + return await self.get(*args, **kwargs) async def get_page_source(self) -> str: """get page source""" @@ -609,14 +620,14 @@ async def get_screenshot_as_png(self, **kwargs): async def is_running(self) -> bool: """browser is running""" if self.webdriver: - logger.info(f'{True}') + logger.info(f'webdriver is running') return True - logger.error(f'{False}') + logger.error(f'webdriver is not running') return False async def load_cookies_for_current_url(self) -> bool: filename = await self._url_filename(url=self.url) - logger.info(dict( + logger.debug(dict( load_cookies_for_current_url=filename, url=self.url, )) @@ -649,7 +660,7 @@ async def quit(self) -> bool: return False return True - async def run(self): + async def run(self) -> bool: """run browser""" try: return await self.config.run()