def check_system(context, config): if platform.system() == 'Linux': print("OS LINUX") linix_path = config['PATH'] executable_path = linix_path['LinuxPathChromeDriver'] opts = webdriver.ChromeOptions() context.browser = webdriver.Chrome(executable_path=executable_path, options=opts) # options=opts str1 = context.browser.capabilities['browserVersion'] str2 = context.browser.capabilities['chrome'][ 'chromedriverVersion'].split(' ')[0] if str1[0:2] != str2[0:2]: print("Верия chromedriver не соответствует браузеру") print("Верия chromedriver {}".format(str1[0:2])) print("Верися Браузера {}".format(str2[0:2])) print("Скачайте соответствующую версияю и положите в папку res") else: print("OS Windows") linix_path = config['PATH'] executable_path = linix_path['WindowsPathChromeDriver'] opts = webdriver.ChromeOptions() context.browser = webdriver.Chrome(executable_path=executable_path, options=opts) # options=opts str1 = context.browser.capabilities['browserVersion'] str2 = context.browser.capabilities['chrome'][ 'chromedriverVersion'].split(' ')[0] if str1[0:2] != str2[0:2]: print("Верия chromedriver не соответствует браузеру") print("Верия chromedriver {}".format(str1[0:2])) print("Верися Браузера {}".format(str2[0:2])) print("Скачайте соответствующую версияю и положите в папку res") return context.browser
def scrape_week( username: str, password: str, week_option: Optional[WeekOption] = WeekOption.CURRENT, driver: Optional[selenium.webdriver.chrome.webdriver.WebDriver] = None, ) -> Week: if driver is None: # The following options are required to make headless Chrome # work in a Docker container options = webdriver.ChromeOptions() options.add_argument("--headless") options.add_experimental_option("excludeSwitches", ["enable-logging"]) options.add_argument("--disable-gpu") # Initialize a new driver driver = webdriver.Chrome(options=options) driver_created = True else: driver_created = False # https://selenium-python.readthedocs.io/waits.html#implicit-waits driver.implicitly_wait(10) driver.maximize_window() driver.get("https://app.thedinnerdaily.com") login_fields_ = login_fields(driver) login_fields_.email.send_keys(username) login_fields_.password.send_keys(password) login_fields_.password.send_keys(Keys.ENTER) # Retry 5 times to read access token from local storage; wait 1 second between for i in range(5): try: local_storage = driver.execute_script( "return window.localStorage;") session = Session.parse_obj(local_storage) break except Exception as exception: time.sleep(1) else: raise exception base_url = "https://db.thedinnerdaily.com/api/v2" menu_url = f"{ base_url }/week-menu?week={ week_option }" shopping_list_url = f"{ base_url }/shopping-list?week={ week_option }" menu_dict = fetch_json(driver, session, menu_url) shopping_list_dict = fetch_json(driver, session, shopping_list_url) menu = Menu.parse_obj(menu_dict) shopping_list = ShoppingList.parse_obj(shopping_list_dict) result = Week(menu=menu, shopping_list=shopping_list) if driver_created: driver.quit() return result
def get_driver(): # gets a silent Chrome driver from selenium import webdriver options_driver = webdriver.ChromeOptions() options_driver.add_argument('headless') options_driver.add_argument("--silent") driver = HiddenChromeWebDriver(chrome_options=options_driver) return driver
def _get_driver_options(): """Return options for Google Chrome WebDriver. :return: An instance of ChromeOptions() with additional arguments. """ options = webdriver.ChromeOptions() # The env variable HEADLESS_CHROME can be set to true in order # to run Google Chrome in a headless mode. if os.getenv('HEADLESS_CHROME') is not None: options.add_argument('--headless') options.add_argument('--no-sandbox') options.add_argument('--disable-dev-shm-usage') options.add_argument('--window-size=1920,1080') options.add_argument( '--user-agent=Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 ' '(KHTML, like Gecko) Chrome/80.0.3987.106 Safari/537.36') return options