import time from selenium import webdriver from selenium.webdriver.chrome.options import Options from selenium.common.exceptions import NoSuchElementException, TimeoutException from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.common.by import By from django.conf import settings class Browser: download_path = None driver = None def __init__(self, download_path): self.download_path = download_path self.set_driver() def set_driver(self): options = Options() options.headless = True options.add_argument('--no-sandbox') options.add_argument('headless') options.add_argument("--headless=new") options.add_argument('--disable-infobars') options.add_argument('--disable-dev-shm-usage') #if settings.APP_ENV == 'production': # options.add_argument('--remote-debugging-port=9222') options.add_argument("--window-size=1920,1200") options.add_argument("--disable-notifications") options.add_argument('--verbose') options.add_experimental_option("prefs", { "download.default_directory": self.download_path, "download.prompt_for_download": False, "download.directory_upgrade": True, "safebrowsing_for_trusted_sources_enabled": False, "safebrowsing.enabled": False }) options.add_argument('--disable-gpu') options.add_argument('--disable-software-rasterizer') #options.add_argument("user-data-dir=chrome-data") options.add_argument("start-maximized") # open Browser in maximized mode options.add_argument("--disable-extensions") # disabling extensions chrome_driver_path = settings.RESOURCE_PATH + '/chromedriver/chromedriver' #self.driver = webdriver.Chrome(options=options) self.driver = webdriver.Chrome(options=options, executable_path=chrome_driver_path) def get_driver(self): return self.driver def destroy_driver(self): self.driver.quit() @staticmethod def click_by_xpath(driver, xpath): element = WebDriverWait(driver, 60).until( EC.element_to_be_clickable((By.XPATH, xpath)) ) element.click() @staticmethod def download_excel(self, timeout=25, download_func=None): seconds = 0 has_file_download = False while has_file_download == False and seconds < timeout: time.sleep(1) if download_func != None: has_file_download = download_func() seconds += 1 return has_file_download