Skip to content

Commit

Permalink
ChromeDriver Selenium fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
Dmitri-Sintsov committed Oct 21, 2023
1 parent f55cabc commit f64db48
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
11 changes: 10 additions & 1 deletion django_jinja_knockout/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -663,7 +663,16 @@ def selenium_factory(cls):
# DJK_WEBDRIVER='selenium.webdriver.phantomjs.webdriver' ./manage.py test
cls.webdriver_name = os.environ.get('DJK_WEBDRIVER', cls.DEFAULT_WEBDRIVER)
webdriver_module = import_module(cls.webdriver_name)
return webdriver_module.WebDriver()
driver_kwargs = {}
if 'chromium' in cls.webdriver_name:
# https://github.com/jsoma/selenium-github-actions/blob/main/scraper.py
from webdriver_manager.chrome import ChromeDriverManager
from webdriver_manager.core.os_manager import ChromeType
from selenium.webdriver.chrome.service import Service
driver_kwargs['service_kwargs'] = {
'executable_path': ChromeDriverManager(chrome_type=ChromeType.CHROMIUM).install()
}
return webdriver_module.WebDriver(**driver_kwargs)

def get_saved_fixtures(self):
fixture_dir = settings.FIXTURE_DIRS[0]
Expand Down
20 changes: 14 additions & 6 deletions django_jinja_knockout/webdriver/chromium/webdriver.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,21 @@ class WebDriver(ChromeWebDriver):
enable_logging = False
window_size = False
# Ubuntu snap path
chrome_path = '/usr/bin/chromium-browser'
chrome_path = '/snap/bin/chromium'
webdriver_path = '/snap/bin/chromium.chromedriver'

def _get_init_options(self):
chrome_options = Options()
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')
# https://stackoverflow.com/a/58748188
chrome_options.add_argument('--remote-debugging-port=9222')
if self.is_headless:
# https://stackoverflow.com/questions/22424737/unknown-error-chrome-failed-to-start-exited-abnormally
# https://stackoverflow.com/questions/50642308/webdriverexception-unknown-error-devtoolsactiveport-file-doesnt-exist-while-t
# https://github.com/jsoma/selenium-github-actions/blob/main/scraper.py
chrome_options.add_argument('--headless')
chrome_options.add_argument('--disable-gpu')
self.enable_logging = True
if self.window_size:
chrome_options.add_argument('--window-size={},{}'.format(*self.window_size))
Expand All @@ -29,14 +35,15 @@ def _get_init_options(self):
return chrome_options

# Selenium >=4.0
def _get_service(self):
service_kwargs = {
def _get_service(self, service_kwargs=None):
_service_kwargs = {
'executable_path': self.webdriver_path if self.webdriver_path else 'chromedriver'
}
if self.is_headless:
service_kwargs['service_args'] = ['--verbose']
_service_kwargs['service_args'] = ['--verbose']
if self.log_file:
service_kwargs['log_path'] = self.log_file.name
_service_kwargs['log_path'] = self.log_file.name
_service_kwargs.update(service_kwargs)
return Service(**service_kwargs)

def _get_legacy_kwargs(self):
Expand All @@ -57,7 +64,8 @@ def __init__(self, *args, **kwargs):
webdriver_args = inspect.signature(ChromeWebDriver.__init__)
if 'service' in webdriver_args.parameters:
# Selenium>=4.0
kwargs['service'] = self._get_service()
service_kwargs = kwargs.pop('service_kwargs', {})
kwargs['service'] = self._get_service(service_kwargs)
else:
kwargs.update(
self._get_legacy_kwargs()
Expand Down

0 comments on commit f64db48

Please sign in to comment.