Created
July 27, 2022 08:42
-
-
Save cmin764/577cb01e05c970fdb65bbe60e763fecb to your computer and use it in GitHub Desktop.
Extends Selenium with logging support
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
from pathlib import Path | |
from RPA.Browser.Selenium import Selenium | |
from SeleniumLibrary.base import keyword | |
from selenium import webdriver | |
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities | |
from webdrivermanager import ChromeDriverManager | |
class ExtendedSelenium(Selenium): | |
USER_DATA = Path("output") / "Chrome" | |
USER_DATA_PATH = os.getenv("USER_DATA", str(USER_DATA)).strip() | |
CHROME_CAPS = DesiredCapabilities.CHROME.copy() | |
CHROME_CAPS['goog:loggingPrefs'] = { | |
'browser': 'ALL', | |
'driver': 'ALL', | |
'performance': 'ALL', | |
} | |
def __init__(self, *args, **kwargs): | |
Selenium.__init__(self, *args, **kwargs) | |
cdm = ChromeDriverManager(link_path="AUTO") | |
cdm.download_and_install() | |
@keyword | |
def open_site(self, url, **kwargs): | |
options = webdriver.ChromeOptions() | |
if self.USER_DATA_PATH: | |
options.add_argument(f"user-data-dir={self.USER_DATA_PATH}") | |
# Details here: https://stackoverflow.com/questions/50642308/webdriverexception-unknown-error-devtoolsactiveport-file-doesnt-exist-while-t | |
options.add_argument("start-maximized") | |
options.add_argument("disable-infobars") | |
options.add_argument("--disable-gpu") | |
options.add_argument("--disable-dev-shm-usage") | |
options.add_argument("--no-sandbox") | |
self.open_browser( | |
url=url, | |
desired_capabilities=self.CHROME_CAPS, | |
options=options, | |
**kwargs | |
) | |
@keyword | |
def print_webdriver_log(self, logtype): | |
print(f"\n{logtype.capitalize()} Log") | |
logs = self.driver.get_log(logtype) | |
return logs |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment