Skip to content

Commit 5c3fe7b

Browse files
committed
ChromeDriverManager & Selenium4+ support for SeleniumMiddleware when no SELENIUM_DRIVER_EXECUTABLE_PATH | SELENIUM_COMMAND_EXECUTOR
1 parent 2e557f6 commit 5c3fe7b

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

scrapy_selenium/middlewares.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
from .http import SeleniumRequest
1111

12-
1312
class SeleniumMiddleware:
1413
"""Scrapy middleware handling the requests using selenium"""
1514

@@ -64,6 +63,17 @@ def __init__(self, driver_name, driver_executable_path,
6463
capabilities = driver_options.to_capabilities()
6564
self.driver = webdriver.Remote(command_executor=command_executor,
6665
desired_capabilities=capabilities)
66+
# webdriver-manager
67+
else:
68+
# selenium4+ & webdriver-manager
69+
from selenium import webdriver
70+
from webdriver_manager.chrome import ChromeDriverManager
71+
from selenium.webdriver.chrome.service import Service as ChromeService
72+
if driver_name and driver_name.lower() == 'chrome':
73+
# options = webdriver.ChromeOptions()
74+
# options.add_argument(o)
75+
self.driver = webdriver.Chrome(options=driver_options,
76+
service=ChromeService(ChromeDriverManager().install()))
6777

6878
@classmethod
6979
def from_crawler(cls, crawler):
@@ -78,7 +88,8 @@ def from_crawler(cls, crawler):
7888
if driver_name is None:
7989
raise NotConfigured('SELENIUM_DRIVER_NAME must be set')
8090

81-
if driver_executable_path is None and command_executor is None:
91+
# let's use webdriver-manager when nothing specified instead | RN just for Chrome
92+
if (driver_name.lower() != 'chrome') and (driver_executable_path is None and command_executor is None):
8293
raise NotConfigured('Either SELENIUM_DRIVER_EXECUTABLE_PATH '
8394
'or SELENIUM_COMMAND_EXECUTOR must be set')
8495

tests/test_cases.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,16 @@ def parse(self, response):
2121
def setUpClass(cls):
2222
"""Create a scrapy process and a spider class to use in the tests"""
2323

24+
'''
2425
cls.settings = {
2526
'SELENIUM_DRIVER_NAME': 'firefox',
2627
'SELENIUM_DRIVER_EXECUTABLE_PATH': which('geckodriver'),
2728
'SELENIUM_DRIVER_ARGUMENTS': ['-headless']
2829
}
30+
'''
31+
cls.settings = {
32+
'SELENIUM_DRIVER_NAME': 'chrome',
33+
'SELENIUM_DRIVER_EXECUTABLE_PATH': None,
34+
'SELENIUM_DRIVER_ARGUMENTS': ['--headless']
35+
}
2936
cls.spider_klass = cls.SimpleSpider

0 commit comments

Comments
 (0)