def get_driver(): if 'DYNO' in os.environ: heroku = True else: heroku = False from selenium.webdriver.firefox.options import Options options = Options() options.add_argument("--headless") options.add_argument("--disable-dev-shm-usage") options.add_argument("--no-sandbox") try: driver = webdriver.Firefox(options=options) except: try: options.binary_location = "/app/vendor/firefox/firefox" driver = webdriver.Firefox(options=options) GECKODRIVER_PATH = str(os.getcwd()) + str("/geckodriver") driver = webdriver.Firefox(options=options, executable_path=GECKODRIVER_PATH) except: try: chrome_options = webdriver.ChromeOptions() chrome_options.binary_location = os.environ.get( "GOOGLE_CHROME_BIN") chrome_options.add_argument("--headless") chrome_options.add_argument("--disable-dev-shm-usage") chrome_options.add_argument("--no-sandbox") driver = webdriver.Chrome( executable_path=os.environ.get("CHROMEDRIVER_PATH"), chrome_options=chrome_options) except: try: GECKODRIVER_PATH = str(os.getcwd()) + str("/geckodriver") options.binary_location = str('./firefox') driver = webdriver.Firefox( options=options, executable_path=GECKODRIVER_PATH) except: os.system( "wget wget https://ftp.mozilla.org/pub/firefox/releases/45.0.2/linux-x86_64/en-GB/firefox-45.0.2.tar.bz2" ) os.system( "wget https://github.com/mozilla/geckodriver/releases/download/v0.26.0/geckodriver-v0.26.0-linux64.tar.gz" ) os.system("tar -xf geckodriver-v0.26.0-linux64.tar.gz") os.system("tar xvf firefox-45.0.2.tar.bz2") GECKODRIVER_PATH = str(os.getcwd()) + str("/geckodriver") options.binary_location = str('./firefox') driver = webdriver.Firefox( options=options, executable_path=GECKODRIVER_PATH) return driver
def get_fx_options(self): options = FirefoxOptions() options.binary_location = self.fx_path profile_dir = self.user_dir + '/.mozilla/firefox/' profile_fname = Popen('cat "$HOME/.mozilla/firefox/profiles.ini" | sed -n -e \'s/^.*Path=//p\' | head -n 1', shell=True, stdout=PIPE).communicate()[0].decode().strip() options.profile = profile_dir + profile_fname return options
def setUpClass(cls): call_command('migrate', verbosity=0) cls.profile = fake.simple_profile() cls.profile['password'] = fake.password() super(SeleniumTestCase, cls).setUpClass() # Default to using firefox for selenium testing, if not try Chrome try: os.environ['MOZ_HEADLESS'] = '1' options = FirefoxOptions() options.add_argument('-headless') cls.driver = webdriver.Firefox(firefox_options=options) except WebDriverException: options = ChromeOptions() if os.environ.get('GOOGLE_CHROME_BINARY', None): options.binary_location = \ os.environ['GOOGLE_CHROME_BINARY'] options.add_argument('--headless') options.add_argument('--disable-gpu') options.add_argument('--no-sandbox') options.add_argument('--log-level=3') options.add_argument('--window-size=1280,720') cls.driver = webdriver.Chrome(chrome_options=options) cls.driver.implicitly_wait(10) cls.wait_time = 5
def init_browser(): opts = Options() selenum_url = os.environ.get('PA_SELENUM_URL', None) if selenum_url != None: o = webdriver.ChromeOptions() o.add_argument("disable-dev-shm-usage") o.add_argument("--no-sandbox") o.add_argument('--disable-gpu') o.add_argument('--disable-setuid-sandbox') d = webdriver.Remote(selenum_url, DesiredCapabilities.CHROME, options=o) else: headless = os.environ.get('PA_HEADLESS', 'no') if headless == 'no': opts.headless = False else: opts.headless = True opts.binary_location = shutil.which('firefox') d = webdriver.Firefox(options=opts, log_path='test.log') d.set_window_size(1600, 900) return d
def getDriver(): """ 返回selenium的driver :return: """ config = ReadConfig() path = config.find_path("config.ini") config.__read__(path) browser_path = config.get_browser("path") driver_path = config.get_driver("path") # self.driver = webdriver.Firefox() # self.driver.set_window_position(0, 0) # self.driver.set_window_size(1400, 900) # self.driver.maximize_window() # 让窗口最大化 # 使用以下三行代码可以不弹出界面,实现无界面爬取 options = Options() options.add_argument('--headless') options.add_argument('--disable-gpu') options.binary_location = browser_path # chrome浏览器配置,添加options参数, executable_path 可选,配置了环境变量后可省略,不然传该驱动的绝对路径 driver = webdriver.Chrome(executable_path=driver_path, options=options) # 火狐浏览器配置 # driver = webdriver.Firefox(executable_path='geckodriver', options=options) return driver
def a_deepl(txt_arg): options = Options() options = webdriver.ChromeOptions() options.add_argument('--headless') options.add_argument('--ignore-certificate-errors') options.add_argument("--test-type") options.binary_location = "/usr/bin/chromium-browser" browser = webdriver.Chrome(chrome_options=options) browser.get('https://www.deepl.com/translator') menu = browser.find_element_by_xpath( "/html/body/div[1]/div[1]/div[1]/div[1]/div[1]/div/button") menu.click() lang = browser.find_element_by_xpath( "/html/body/div[1]/div[1]/div[1]/div[1]/div[1]/div/div/button[4]") lang.click() text = browser.find_element_by_xpath( "/html/body/div[1]/div[1]/div[1]/div[1]/div[2]/textarea") text.send_keys(txt_arg) wait = WebDriverWait(browser, 2) ok = wait.until( ec.presence_of_element_located( (By.XPATH, "/html/body/div[1]/div[1]/div[1]/div[2]/div[3]/p[1]"))) browser.execute_script( 'document.evaluate("/html/body/div[1]/div[1]/div[1]/div[2]/div[3]/p[1]", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue.style.display = "block";', ok) answer = browser.find_element_by_xpath( "/html/body/div[1]/div[1]/div[1]/div[2]/div[3]/p[1]") return answer.text
def setup_browser(self, browser="Firefox", headless=True): # pylint: disable=C0202; # noqa """Initialize the browser This browser instance is re-used for all tests in order to improve functional tests speed, as this operation is very expensive""" if browser == "Firefox": options = Options() if headless: options.add_argument('-headless') self.browser = webdriver.Firefox(options=options) # fallback to chromium otherwise else: options = webdriver.ChromeOptions() options.add_argument('--ignore-certificate-errors') options.add_argument("--test-type") options.add_argument('--no-proxy-server') if headless: options.add_argument('--headless') # Note: if running the tests as root (why would we!?) add: # options.add_argument('--no-sandbox') options.binary_location = "/usr/bin/chromium-browser" self.browser = webdriver.Chrome(chrome_options=options) test_target = os.environ.get('TEST_TARGET') if test_target: self.live_server_url = f"http://{test_target}"
def __init__(self, headless): __options = Options() __options.headless = headless __options.set_preference('devtools.jsonview.enabled', False) __options.binary_location = "C:/firefox_binary/firefox.exe" self.__driver = webdriver.Firefox( firefox_options=__options, executable_path='./driver/geckodriver.exe')
def get_options(self): if self.get_type() == 'firefox': options = FirefoxOptions() if self.get_args(): for arg in self.get_args().split(): options.add_argument('--' + arg) if self.bin_path: options.binary_location(self.bin_path) if self.get_headless(): options.headless = True if self.get_window_size(): win_size = self.get_window_size().split(',') options.add_argument('--width=' + win_size[0]) options.add_argument('--height=' + win_size[1]) # Set download path options.set_preference('browser.download.folderList', 2) options.set_preference('browser.download.dir', get_download_path()) options.set_preference('browser.download.manager.showWhenStarting', False) options.set_preference('browser.helperApps.alwaysAsk.force', False) options.set_preference('browser.helperApps.neverAsk.saveToDisk', "application/octet-stream") # options.set_preference('browser.helperApps.neverAsk.openFile', "application/octet-stream") elif self.get_type() == 'ie': options = IeOptions() options.ignore_protected_mode_settings = False options.require_window_focus = True options.native_events = False # proceed IE options here else: options = ChromeOptions() if self.get_args(): for arg in self.get_args().split(): options.add_argument(arg) if self.bin_path: options.binary_location(self.bin_path) if self.get_headless(): options.headless = True if self.get_window_size(): options.add_argument('--window-size=' + self.get_window_size()) # Set download path prefs = {} prefs["download.prompt_for_download"] = 0 prefs["download.default_directory"] = get_download_path() options.add_experimental_option("prefs", prefs) return options
def __init__(self): opt = Options() opt.binary_location = r'C:\Program Files\Mozilla Firefox\firefox.exe' self.driver = webdriver.Firefox( options=opt, executable_path= r'C:\\Users\\Kaarel Vesilind\\Documents\\Botid\\Draiverid\\geckodriver.exe' )
def initialize_selenium_driver(): options = Options() options.headless = True options.binary_location = "/usr/bin/firefox" driver = webdriver.Firefox(options=options, executable_path=get_driver_executable()) print("WebAdvisor Saver: Initiated!") return driver
def setup_driver(): global wd driver_options=Options() driver_options.binary_location = r"PATH TO YOUR FIREFOX EXECUTABLE" driver_options.add_argument('--disable-dev-shm-usage') driver_options.set_preference("media.navigator.permission.disabled", True) wd=webdriver.Firefox(executable_path="PATH TO YOUR GECKODRIVER",options=driver_options)
def setUp(self): print("xxx xxx xxx BaseTestCase.setup xxx") #_download_dir = "/tmp" self._download_dir = tempfile.mkdtemp() print("...download_dir: {d}".format(d=self._download_dir)) browser = config[BROWSER_SECTION][BROWSER_KEY] options = Options() #options.add_argument("--headless") options.add_argument("--foreground") #mode = "--headless" mode = "--foreground" if browser == 'chrome': #options.binary_location = "/usr/local/bin/chromedriver" chrome_options = webdriver.ChromeOptions() #chrome_options.binary_location = "/usr/local/bin/chromedriver" chrome_options.add_argument(mode) preferences = {"download.default_directory": self._download_dir, "directory_upgrade": True, "safebrowsing.enabled": True, "prompt_for_download": "false"} chrome_options.add_experimental_option("prefs", preferences) self.driver = webdriver.Chrome(options=chrome_options) elif browser == 'firefox': firefox_profile = FirefoxProfile() # profile firefox_profile.set_preference('extensions.logging.enabled', False) firefox_profile.set_preference('network.dns.disableIPv6', False) firefox_profile.set_preference('browser.download.dir', self._download_dir) firefox_profile.set_preference('browser.download.folderList', 2) firefox_profile.set_preference('browser.download.useDownloadDir', True) firefox_profile.set_preference('browser.download.panel.shown', False) firefox_profile.set_preference('browser.download.manager.showWhenStarting', False) firefox_profile.set_preference('browser.download.manager.showAlertOnComplete', False) firefox_profile.set_preference("browser.helperApps.neverAsk.saveToDisk", "application/x-netcdf"); firefox_capabilities = DesiredCapabilities().FIREFOX firefox_capabilities['marionette'] = True #firefox_capabilities['moz:firefoxOptions'] = {'args': ['--headless']} options.binary_location = "/usr/local/bin/geckodriver" firefox_binary = FirefoxBinary("/usr/bin/firefox") #self.driver = webdriver.Firefox(firefox_profile=firefox_profile, # firefox_binary=firefox_binary, # options=options, # capabilities = firefox_capabilities) self.driver = webdriver.Firefox(firefox_profile=firefox_profile, firefox_binary=firefox_binary, executable_path="/usr/local/bin/geckodriver", options=options, capabilities = firefox_capabilities) self.driver.implicitly_wait(10) idp_server = self._get_idp_server() self.driver.get("https://{n}".format(n=idp_server)) time.sleep(3)
def initiateFFbrowser(): options = Options() options.binary_location = r'C:\\Program Files\\Mozilla Firefox\\firefox.exe' options.add_argument('-headless') options.profile = 'C:\\Users\\yedaya\\AppData\\Roaming\\Mozilla\\Firefox\\Profiles\\ri0nggib.bot_profile' driver = webdriver.Firefox( executable_path="C:\\Users\\yedaya\\Gechodriver\\geckodriver.exe", firefox_options=options) return driver
def __init__(self): # self.logger setup self.logger = logging.getLogger(__name__) self.logger.setLevel(logging.INFO) # create handlers file_handler = logging.FileHandler('meh.log') file_handler.setLevel(logging.INFO) # custom timezone def converter(x, y): return (datetime.utcnow() - timedelta(hours=4)).timetuple() logging.Formatter.converter = converter # create a logging format formatter = logging.Formatter( '%(asctime)s - %(name)s - %(levelname)s: %(message)s' ) file_handler.setFormatter(formatter) # add the handlers to the self.logger self.logger.addHandler(file_handler) if not exists('.env'): print('You don\'t have a .env file') self.logger.info('You don\'t have a .env file') exit(1) # browser initialization options = Options() options.set_headless(True) choices = ['chromium-browser', 'google-chrome', 'firefox'] browser_choice = getenv('BROWSER_CHOICE') if browser_choice not in choices: self.browser = www.Firefox( firefox_options=options, executable_path='./geckodriver' ) elif browser_choice.lower() == choices[2]: self.browser = www.Firefox( firefox_options=options, executable_path='./geckodriver' ) elif browser_choice.lower() in choices[:2]: options.binary_location = '/usr/bin/' + browser_choice self.browser = www.Chrome( chrome_options=options, executable_path='./chromedriver' ) else: self.logger.error('Browser startup failure') exit(1) # shit happens # driver wait self.wait = WebDriverWait(self.browser, 300) # maximum wait time self.logged_in = False
def Start(self): """ Opens the browser maximized and goes to defined URL. Usage: >>> # Calling the method: >>> oHelper.Start() """ print("Starting the browser") if self.config.browser.lower() == "firefox": if sys.platform == 'linux': driver_path = os.path.join(os.path.dirname(__file__), r'drivers/linux64/geckodriver') else: driver_path = os.path.join( os.path.dirname(__file__), r'drivers\\windows\\geckodriver.exe') log_path = os.devnull options = FirefoxOpt() options.set_headless(self.config.headless) self.driver = webdriver.Firefox(firefox_options=options, executable_path=driver_path, log_path=log_path) elif self.config.browser.lower() == "chrome": driver_path = os.path.join(os.path.dirname(__file__), r'drivers\\windows\\chromedriver.exe') options = ChromeOpt() options.set_headless(self.config.headless) options.add_argument('--log-level=3') if self.config.headless: options.add_argument('force-device-scale-factor=0.77') self.driver = webdriver.Chrome(chrome_options=options, executable_path=driver_path) elif self.config.browser.lower() == "electron": driver_path = os.path.join( os.path.dirname(__file__), r'drivers\\windows\\electron\\chromedriver.exe' ) # TODO chromedriver electron version options = ChromeOpt() options.add_argument('--log-level=3') options.binary_location = self.config.electron_binary_path self.driver = webdriver.Chrome(chrome_options=options, executable_path=driver_path) if not self.config.browser.lower() == "electron": if self.config.headless: self.driver.set_window_position(0, 0) self.driver.set_window_size(1366, 768) else: self.driver.maximize_window() self.driver.get(self.config.url) self.wait = WebDriverWait(self.driver, 90)
def search(self, keyword): count = 0 options = Options() options.add_argument("--disable-gpu") options.add_argument("--no-sandbox") options.binary_location = GOOGLE_CHROME_PATH driver = webdriver.Chrome(options=options, executable_path=CHROMEDRIVER_PATH) search_url = self.BASE_URL.format(keyword) driver.get(search_url) grid = driver.find_element_by_class_name('gridRow') # gridRow products = grid.find_elements(By.CLASS_NAME, 'productItem') for product in products: count += 1 if count >= 10: break details_container = product.find_element_by_class_name( "containerDescription") # Get title title_ = details_container.find_element_by_class_name("title") title_ = title_.text # Get type brand_ = details_container.find_element_by_class_name("type") brand_ = brand_.text # Get image image_container = driver.find_element_by_class_name('lazy') image_ = image_container.get_attribute("src") # Get price price_container = product.find_element_by_class_name( "containerPrice") price_uni_ = price_container.find_element_by_class_name( "priceFirstRow") price_uni_ = price_uni_.text price_kg_ = price_container.find_element_by_class_name( "priceSecondRow") price_kg_ = price_kg_.text self.products.append( ContinentProduct(title_, brand_, image_, price_uni_, price_kg_)) driver.quit() print(self.products[0].supermarket, " - ", count, "products found.")
def get_firefox_options(self): opts = FirefoxOptions() if self.browser_binary: opts.binary_location = self.browser_binary #opts.log.level = "trace" opts.set_capability("acceptInsecureCerts", False) opts.set_capability("unhandledPromptBehavior", "ignore") opts.set_preference('extensions.webextensions.uuids', f'{{"{FF_EXT_ID}": "{FF_UUID}"}}') opts.set_preference("dom.webdriver.enabled", False) # disable prefetching opts.set_preference("network.dns.disablePrefetch", True) opts.set_preference("network.prefetch-next", False) # disable OpenH264 codec downloading opts.set_preference("media.gmp-gmpopenh264.enabled", False) opts.set_preference("media.gmp-manager.url", "") # disable health reports opts.set_preference("datareporting.healthreport.service.enabled", False) opts.set_preference("datareporting.healthreport.uploadEnabled", False) opts.set_preference("datareporting.policy.dataSubmissionEnabled", False) # disable experiments opts.set_preference("experiments.enabled", False) opts.set_preference("experiments.supported", False) opts.set_preference("experiments.manifest.uri", "") # disable telemetry opts.set_preference("toolkit.telemetry.enabled", False) opts.set_preference("toolkit.telemetry.unified", False) opts.set_preference("toolkit.telemetry.archive.enabled", False) if self.firefox_tracking_protection == "off": # disable all content blocking/Tracking Protection features # https://wiki.mozilla.org/Security/Tracking_protection opts.set_preference("privacy.trackingprotection.enabled", False) opts.set_preference("privacy.trackingprotection.pbmode.enabled", False) opts.set_preference( "privacy.trackingprotection.cryptomining.enabled", False) opts.set_preference( "privacy.trackingprotection.fingerprinting.enabled", False) opts.set_preference( "privacy.trackingprotection.socialtracking.enabled", False) # always allow third-party cookies opts.set_preference("network.cookie.cookieBehavior", 0) elif self.firefox_tracking_protection == "strict": opts.set_preference("browser.contentblocking.category", "strict") return opts
def load_firefox_driver(): options = Options() options.binary_location = os.environ.get('FIREFOX_BIN') options.add_argument('--headless') # options.add_argument('--disable-gpu') options.add_argument('--disable-dev-smh-usage') options.add_argument('--no-sandbox') return webdriver.Firefox(executable_path=str(os.environ.get('GECKODRIVER_PATH')), options=options)
def launch_driver(self, url, options, browser_build_path): import webkitpy.thirdparty.autoinstalled.selenium from selenium import webdriver from selenium.webdriver.firefox.options import Options options = Options() if browser_build_path: binary_path = os.path.join(browser_build_path, 'firefox-bin') options.binary_location = binary_path driver_executable = self.webdriver_binary_path driver = webdriver.Firefox(firefox_options=options, executable_path=driver_executable) super(LinuxFirefoxDriver, self).launch_webdriver(url, driver) return driver
def launch_driver(self, url, options, browser_build_path): import webkitpy.thirdparty.autoinstalled.selenium from selenium import webdriver from selenium.webdriver.firefox.options import Options firefox_options = Options() if browser_build_path: app_path = os.path.join(browser_build_path, self.app_name) binary_path = os.path.join(app_path, "Contents/MacOS", self.process_name) firefox_options.binary_location = binary_path driver_executable = self.webdriver_binary_path driver = webdriver.Firefox(firefox_options=firefox_options, executable_path=driver_executable) self._launch_webdriver(url=url, driver=driver) return driver
def search(self, keyword): count = 0 options = Options() options.add_argument("--disable-gpu") options.add_argument("--no-sandbox") options.binary_location = GOOGLE_CHROME_PATH driver = webdriver.Chrome(options=options, executable_path=CHROMEDRIVER_PATH) search_url = self.BASE_URL.format(keyword) driver.get(search_url) grid = driver.find_element_by_class_name('content_vignettes') # gridRow products = grid.find_elements(By.CLASS_NAME, 'vignette_produit_info') for product in products: count += 1 if count >= 10: break details_container = product.find_element_by_class_name( "vignette_info") # Get brand, title and package weight brand_, title_, _ = details_container.text.split("\n") # Get image image_container = product.find_element_by_class_name( "vignette_img") image_ = image_container.find_elements_by_tag_name( "img")[0].get_attribute("src") # Get price price_container = product.find_element_by_class_name( "vignette_picto_prix") prices = price_container.find_elements_by_tag_name( "div")[0].text.split("\n") if len(prices) == 3: _, price_uni_, price_kg_ = prices else: price_uni_, price_kg_ = prices self.products.append( IntermarcheProduct(title_, brand_, image_, price_uni_, price_kg_)) driver.close() print(self.products[0].supermarket, " - ", count, "products found.")
def search(self, keyword): count = 0 options = Options() options.add_argument("--disable-gpu") options.add_argument("--no-sandbox") options.binary_location = GOOGLE_CHROME_PATH driver = webdriver.Chrome(options=options, executable_path=CHROMEDRIVER_PATH) search_url = self.BASE_URL.format(keyword) driver.get(search_url) grid = driver.find_element_by_class_name('justify-content-start') # gridRow products = grid.find_elements(By.CLASS_NAME, 'product') for product in products: count += 1 if count >= 10: break details_container = product.find_element_by_class_name( "product-tile") # Get title title_ = details_container.find_element_by_class_name( 'auc-product-tile__name') title_ = title_.text # Get image image_ = details_container.find_element_by_class_name( "image-container").find_element_by_class_name("tile-image") image_ = image_.get_attribute("src") # Get price price_uni = product.find_element_by_class_name( "auc-product-tile__prices") price_uni_ = price_uni.text.split("\n")[-1] price_kg = product.find_element_by_class_name( "auc-product-tile__measures") price_kg_ = price_kg.text self.products.append( AuchanProduct(title_, image_, price_uni_, price_kg_)) driver.close() print(self.products[0].supermarket, " - ", count, "products found.")
def search(self, keyword): count = 0 options = Options() options.add_argument("--disable-gpu") options.add_argument("--no-sandbox") options.binary_location = GOOGLE_CHROME_PATH driver = webdriver.Chrome(options=options, executable_path=CHROMEDRIVER_PATH) search_url = self.BASE_URL.format(keyword) driver.get(search_url) grid = driver.find_element_by_class_name('_3zhFFG0Bu9061elHI8VCq3') # gridRow products = grid.find_elements(By.CLASS_NAME, 'P9eg53AkHYfXRP7gt5njS') for product in products: count += 1 if count >= 10: break details_container = product.find_element_by_class_name( "product-details") # Get title title_ = details_container.find_element_by_class_name( 'pdo-heading-s') title_ = title_.text # Get image image_ = product.find_element_by_class_name("pdo-block") image_ = image_.get_attribute("src") # Get price price_container = product.find_element_by_class_name("bottom-info") price_uni_ = price_container.find_element_by_class_name( "detail-price").text weight_description = price_container.find_element_by_class_name( "pdo-block").text _, price_kg_ = weight_description.split("|") self.products.append( PingoDoceProduct(title_, image_, price_uni_, price_kg_)) driver.close() print(self.products[0].supermarket, " - ", count, "products found.")
def launch_driver(self, url, options, browser_build_path): from selenium import webdriver from selenium.webdriver.firefox.options import Options firefox_options = Options() if not browser_build_path: browser_build_path = '/Applications/' app_path = os.path.join(browser_build_path, self.app_name) binary_path = os.path.join(app_path, "Contents/MacOS", self.process_name) firefox_options.binary_location = binary_path driver_executable = self.webdriver_binary_path driver = webdriver.Firefox(firefox_options=firefox_options, executable_path=driver_executable) self._launch_webdriver(url=url, driver=driver) return driver
def __init__(self, website='medium', driver = 'geckoDriver'): #website specific details self.url = config.get(website).get('url') self.email = config.get(website).get('email') self.password = config.get(website).get('password') #firefox specific details options = Options() options.add_argument("--headless") # options.add_argument('--no-sandbox') options.binary_location = "/usr/bin/firefox" #Firefox driver self.driver = webdriver.Firefox(executable_path = config.get(driver).get('path'), firefox_options = options)
def lambda_handler(event, context): options = FirefoxOptions() options.headless = True options.binary_location = '/usr/local/bin/firefox' driver = webdriver.Firefox(executable_path='/usr/local/bin/geckodriver', log_path='/tmp/geckodriver.log', firefox_options=options) title = get_title(driver) print(title) driver.quit() return {'statusCode': 200, 'body': json.dumps("LGTM")}
def browser(boolean): options = Options() # driver = webdriver.PhantomJS() # driver = Firefox(executable_path='geckodriver', firefox_options=options) if boolean == True: print('-------------------->无头模式<--------------------') options.add_argument('--disable-gpu') options.add_argument('--headless') options.binary_location = r'C:\Program Files (x86)\Google\Chrome\Application\chrome.exe' driver = webdriver.Chrome(options=options) return driver else: print('-------------------->有头模式<--------------------') # 自动下载目录,禁止下载弹窗 prefs = { 'profile.default_content_settings.popups': 0, 'download.default_directory': 'F:\\WorkPlace\\Python\\python\\AutoBaikeProject\\website\\test_data\imgs' } options.add_experimental_option('prefs', prefs) options.binary_location = r'C:\Program Files (x86)\Google\Chrome\Application\chrome.exe' driver = webdriver.Chrome(options=options) return driver
def browser_firefox(browser_path="", driver_path=""): """ 火狐内核浏览器兼容, :param browser_path: 浏览器路径, :param driver_path: 驱动路径, :return: driver, """ global driver if len(browser_path) > 0: options = FirefoxOptions() options.binary_location = browser_path else: options = None if len(driver_path) == 0 : driver_path = "geckodriver" driver = webdriver.Firefox(executable_path=driver_path, options=options) return driver
def get_driver(self): from selenium.webdriver import Chrome from selenium.webdriver.chrome.options import Options from selenium.common.exceptions import WebDriverException from selenium.webdriver.common.desired_capabilities import DesiredCapabilities options = Options() options.binary_location = '/usr/bin/google-chrome-stable' options.add_argument('--headless') options.add_argument('--enable-precise-memory-info') d = DesiredCapabilities.CHROME d['loggingPrefs'] = {'browser': 'ALL'} self.JavascriptException = WebDriverException return Chrome(chrome_options=options, desired_capabilities=d)
from selenium import webdriver from selenium.webdriver.firefox.options import Options from selenium.webdriver.firefox.firefox_profile import FirefoxProfile from selenium.webdriver.common.keys import Keys import sys # macos_firefox.py # /Applications/Firefox.app/Contents/MacOS/firefox --private-window https://www.uol.com.br # Options firefox_options = Options() firefox_options.log.level = 'debug' firefox_options.add_argument('-private') firefox_options.accept_untrusted_certs = True firefox_options.assume_untrusted_cert_issuer = True firefox_options.binary_location = '/Applications/Firefox.app/Contents/MacOS/firefox' # firefox_options.headless = True # FirefoxProfile firefox_profile = FirefoxProfile(); firefox_profile.set_preference('browser.privatebrowsing.autostart', True) firefox_profile.set_preference('pdfjs.disabled', True) firefox_profile.set_preference('browser.download.folderList', 2) firefox_profile.set_preference('browser.download.panel.shown', False) firefox_profile.set_preference('browser.tabs.warnOnClose', False) firefox_profile.set_preference('browser.tabs.animate', False) firefox_profile.set_preference('browser.fullscreen.animateUp', 0) firefox_profile.set_preference('geo.enabled', False) firefox_profile.set_preference('browser.urlbar.suggest.searches', False) firefox_profile.set_preference('browser.tabs.warnOnCloseOtherTabs', False) firefox_profile.update_preferences()