Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pytest = "==6.2.3"
meilisearch = "==0.16.1"
requests-iap = "==0.2.0"
python-keycloak-client = "==0.2.3"
webdriver-manager = "==3.4.2"

[dev-packages]
pylint = "==2.8.2"
Expand Down
151 changes: 110 additions & 41 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,8 @@ If used, `min_indexed_level` is ignored.

When `js_render` is set to `true`, the scraper will use ChromeDriver. This is needed for pages that are rendered with JavaScript, for example, pages generated with React, Vue, or applications that are running in development mode: `autoreload` `watch`.

After installing ChromeDriver, provide the path to the bin using the following environment variable `CHROMEDRIVER_PATH` (default value is `/usr/bin/chromedriver`).
After installing ChromeDriver, provide the path to the bin using the following environment variable `CHROMEDRIVER_PATH`. If the variable is not set, the scraper
will automatically download and use a compatible version of ChromeDriver.

The default value of `js_render` is `false`.

Expand Down
45 changes: 39 additions & 6 deletions scraper/src/config/browser_handler.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import re
import os
import sys
from distutils.util import strtobool
from selenium import webdriver

from selenium.webdriver.chrome.options import Options
from webdriver_manager.chrome import ChromeDriverManager
from ..custom_downloader_middleware import CustomDownloaderMiddleware
from ..js_executor import JsExecutor

Expand All @@ -26,12 +29,42 @@ def init(config_original_content, js_render, user_agent):
chrome_options.add_argument('--headless')
chrome_options.add_argument('user-agent={0}'.format(user_agent))

CHROMEDRIVER_PATH = os.environ.get('CHROMEDRIVER_PATH',
"/usr/bin/chromedriver")
if not os.path.isfile(CHROMEDRIVER_PATH):
raise Exception(
"Env CHROMEDRIVER_PATH='{}' is not a path to a file".format(
CHROMEDRIVER_PATH))
CHROMEDRIVER_PATH = os.environ.get('CHROMEDRIVER_PATH', '')
if not CHROMEDRIVER_PATH or not os.path.isfile(CHROMEDRIVER_PATH):
print("Could not find ChromeDriver.")
print("Either the Env CHROMEDRIVER_PATH='{}' path is incorrect or "
"ChromeDriver is not installed.".format(CHROMEDRIVER_PATH))
print("Do you want to automatically download ChromeDriver?")
while(True):
user_input = input("[Y/n]: ")
try:
yes = strtobool(user_input)
break
except ValueError:
print("Please enter a valid input.")
continue
if yes:
try:
CHROMEDRIVER_PATH = ChromeDriverManager().install()

except Exception as e:
print("Could not download ChromeDriver. "
"Please install ChromeDriver manually.")
print(e)
if sys.platform == "linux" or sys.platform == "darwin":
os.system('read -s -n 1 -p "Press any key to continue..."')
if sys.platform == "win32":
os.system('pause')
sys.exit(1)
else:
print("Please install ChromeDriver and set the CHROMEDRIVER_PATH "
"environment variable or remove the render_js option.")
if sys.platform == "linux" or sys.platform == "darwin":
os.system('read -s -n 1 -p "Press any key to continue..."')
if sys.platform == "win32":
os.system('pause')
sys.exit(1)

driver = webdriver.Chrome(
CHROMEDRIVER_PATH,
options=chrome_options)
Expand Down
4 changes: 4 additions & 0 deletions tests/config_loader/get_extra_facets_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from .abstract import config
from .mocked_init import MockedInit


class TestGetExtraFacets:
def test_extra_facets_should_be_empty_by_default(self):
c = config()
Expand All @@ -19,6 +20,7 @@ def test_extra_facets_should_be_set_from_start_urls_variables_browser(self,
monkeypatch):
monkeypatch.setattr("selenium.webdriver.chrome",
lambda x: MockedInit())
monkeypatch.setattr('builtins.input', lambda _: "y")

c = config({
"start_urls": [
Expand All @@ -43,6 +45,7 @@ def test_extra_facets_should_be_set_from_start_urls_variables_with_two_start_url
self, monkeypatch):
monkeypatch.setattr("selenium.webdriver.chrome",
lambda x: MockedInit())
monkeypatch.setattr('builtins.input', lambda _: "y")

c = config({
"js-render": True,
Expand Down Expand Up @@ -74,6 +77,7 @@ def test_extra_facets_should_be_set_from_start_urls_variables_with_multiple_tags
self, monkeypatch):
monkeypatch.setattr("selenium.webdriver.chrome",
lambda x: MockedInit())
monkeypatch.setattr('builtins.input', lambda _: "y")

c = config({
"start_urls": [
Expand Down
2 changes: 2 additions & 0 deletions tests/config_loader/open_selenium_browser_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ def test_browser_not_needed_by_default(self):
def test_browser_needed_when_js_render_true(self, monkeypatch):
monkeypatch.setattr("selenium.webdriver.chrome",
lambda x: MockedInit())
monkeypatch.setattr('builtins.input', lambda _: "y")
# When
c = config({
"js_render": True
Expand All @@ -37,6 +38,7 @@ def test_browser_needed_when_config_contains_automatic_tag(self,
monkeypatch):
monkeypatch.setattr("selenium.webdriver.chrome",
lambda x: MockedInit())
monkeypatch.setattr('builtins.input', lambda _: "y")

# When
c = config({
Expand Down
1 change: 1 addition & 0 deletions tests/config_loader/start_urls_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ def test_start_urls_should_be_generated_when_there_is_automatic_tagging_browser(
self, monkeypatch):
monkeypatch.setattr("selenium.webdriver.chrome",
lambda x: MockedInit())
monkeypatch.setattr('builtins.input', lambda _: "y")

# When
c = config({
Expand Down