def getBrowser(self): global _browser if _browser is None: # Use selenium logging.info("Creating Selenium webdriver using " + str(self.browser_type)) # FIREFOX if self.browser_type == EBrowser.FIREFOX: binary = FirefoxBinary(self.firefox_binary) # see http://selenium-python.readthedocs.io/faq.html#how-to-auto-save-files-using-custom-firefox-profile if self.browser_user_profile: profile = webdriver.FirefoxProfile(self.firefox_profile_path) else: profile = webdriver.FirefoxProfile() _browser = webdriver.Firefox(firefox_binary=binary, firefox_profile=profile) self.sleep(1) _browser.set_window_position(0, 0) # PHANTOMJS elif self.browser_type == EBrowser.PHANTOMJS: dcap = dict(DesiredCapabilities.PHANTOMJS) useragent = ua.random dcap["phantomjs.page.settings.userAgent"] = useragent logging.info("Useragent='" + useragent + "'") _browser = webdriver.PhantomJS(executable_path=self.phantomjs_path, desired_capabilities=dcap) self.sleep(1) _browser.set_window_size(1920, 1080) self.sleep(1) # CHROME elif self.browser_type == EBrowser.CHROME: chrome_options = ChromeOptions() chrome_options.add_argument("--start-maximized") chrome_options.binary_location = self.chrome_binary _browser = webdriver.Chrome(chrome_options=chrome_options) self.sleep(3) # CHROME (headless) elif self.browser_type == EBrowser.CHROME_HEADLESS: chrome_options = ChromeOptions() chrome_options.add_argument("--headless") chrome_options.add_argument("--window-size={},{}".format(self.chrome_headless_screen_size[0], self.chrome_headless_screen_size[1])) chrome_options.binary_location = self.chrome_binary _browser = webdriver.Chrome(chrome_options=chrome_options) self.sleep(1) # OPERA elif self.browser_type == EBrowser.OPERA: opera_options = OperaOptions() opera_options.binary_location = r"C:\Program Files\Opera\launcher.exe" _browser = webdriver.Opera(opera_options=opera_options) self.sleep(5) return _browser
def setup_method(self, method): self.assert_errors = '\n' browser_name = os.environ.get('BROWSER') if browser_name == 'firefox': profile = webdriver.FirefoxProfile() profile.set_preference("devtools.console.stdout.content", True) # if hasattr(method, 'portable'): # profile.set_preference( # "general.useragent.override", # "Mozilla/5.0 (Linux; Android 8.0;" # "Pixel 2 Build/OPD3.170816.012) " # "AppleWebKit/537.36 (KHTML, like Gecko)" # "Chrome/70.0.3538.77 Mobile Safari/537.36" # ) # profile.update_preferences() firefox_options = FirefoxOptions() firefox_options.add_argument("-no-remote") firefox_options.log.level = 'trace' self.driver = webdriver.Firefox(firefox_profile=profile, options=firefox_options) elif browser_name == 'opera': opera_options = OperaOptions() opera_options.add_argument("--verbose") opera_options.add_argument("--enable-logging --v=1") opera_options.add_argument("--no-sandbox") opera_options.add_argument("--ignore-certificate-errors") opera_options.add_argument("--disable-notifications") opera_options.add_argument("--disable-gpu") opera_options.add_experimental_option('w3c', False) # if hasattr(method, 'portable'): # opera_options.add_experimental_option( # "mobileEmulation", # {'deviceName': 'Nexus 5'} # ) opera_options.binary_location = '/usr/bin/opera' self.driver = webdriver.Opera(options=opera_options) else: options = ChromeOptions() options.add_argument("--verbose") options.add_argument("--enable-logging --v=1") options.add_argument("--no-sandbox") options.add_argument("--ignore-certificate-errors") options.add_argument("--disable-notifications") options.add_argument("--disable-gpu") options.add_experimental_option('w3c', False) if hasattr(method, 'portable'): options.add_experimental_option("mobileEmulation", {'deviceName': 'Nexus 5'}) self.driver = webdriver.Chrome(chrome_options=options) self.driver.maximize_window()
def get(self): cap = DesiredCapabilities.OPERA.copy() opt = OperaOptions() opt.binary_location = self.binary_location drv = webdriver.Opera(executable_path=self.driver_path, desired_capabilities=cap, options=opt) self.set_window_size(drv) self.set_window_position(drv) return drv
def driver_factory(browser_name: str, is_headless: bool = False, webdriver_logging: bool = False) -> webdriver: """ Return webdriver object for a specified browser name """ driver = None if browser_name == "firefox": firefox_options = webdriver.FirefoxOptions() if is_headless: firefox_options.add_argument("--headless") if webdriver_logging: driver = EventFiringWebDriver( driver=webdriver.Firefox(options=firefox_options), event_listener=DriverEventListener( log_filename=f"{browser_name}.log")) else: driver = webdriver.Firefox(options=firefox_options) elif browser_name == "chrome": chrome_options = webdriver.ChromeOptions() if is_headless: chrome_options.add_argument("--headless") capabilities = DesiredCapabilities.CHROME.copy() capabilities['acceptSslCerts'] = True capabilities['acceptInsecureCerts'] = True capabilities['loggingPrefs'] = {'browser': 'ALL'} if webdriver_logging: driver = EventFiringWebDriver( driver=webdriver.Chrome(options=chrome_options, desired_capabilities=capabilities), event_listener=DriverEventListener( log_filename=f"{browser_name}.log")) else: driver = webdriver.Chrome(options=chrome_options, desired_capabilities=capabilities) elif browser_name == "yandex": # TODO: implement Yandex browser pass elif browser_name == "opera": opera_options = OperaOptions() opera_options.binary_location = OPERA_BROWSER_EXECUTABLE if is_headless: opera_options.add_argument("--headless") capabilities = DesiredCapabilities.OPERA.copy() capabilities['acceptSslCerts'] = True capabilities['acceptInsecureCerts'] = True if webdriver_logging: driver = EventFiringWebDriver( driver=webdriver.Opera( options=opera_options, executable_path=OPERA_WEBDRIVER_EXECUTABLE, desired_capabilities=capabilities), event_listener=DriverEventListener( log_filename=f"{browser_name}.log")) else: driver = webdriver.Opera( options=opera_options, executable_path=OPERA_WEBDRIVER_EXECUTABLE, desired_capabilities=capabilities) elif browser_name == "safari": # TODO: implement Safari browser pass else: raise NameError("Browser not supported") return driver
def translate(src_lang, tgt_lang, list_text): global translate_file_name list_tgt = list() count = 1 search_text = "" sign = ";" #f = open("link.test.txt", "w", encoding="utf-8") loop = 1 opera_profile = "C:\\Users\\\Admin\\AppData\\Roaming\\Opera Software\\Opera Stable" options = Options() options.binary_location = r'C:\Users\\Admin\AppData\Local\Programs\Opera\74.0.3911.107\opera.exe' options.add_argument("--headless") options.add_argument('--no-sandbox') #options.add_argument('--user-data-dir=' + opera_profile) #driver = webdriver.PhantomJS(os.path.abspath(os.getcwd()+"\phantomjs")) #driver = webdriver.Opera(executable_path=os.path.abspath(os.getcwd()+"/operadriver.exe"), options = options) driver = ChromeDriver.getChromeDriver() url = "https://translate.google.com/?sl={}&tl={}&text={}&op=translate".format( src_lang, tgt_lang, "") driver.get(url) textarea = driver.find_element_by_tag_name("textarea") old_text = "" print(len(list_text)) len_of_batch = 0 lim_of_batch = 3800 text_to_translate = "" list_transed_text = list() try: for text in list_text: if (loop % 100 == 0): driver.close() driver = ChromeDriver.getChromeDriver() url = "https://translate.google.com/?sl={}&tl={}&text={}&op=translate".format( src_lang, tgt_lang, "") driver.get(url) textarea = driver.find_element_by_tag_name("textarea") loop = 1 textarea = driver.find_element_by_tag_name("textarea") if (len_of_batch > lim_of_batch): last_index = len(text_to_translate) - 1 if (text_to_translate[last_index] == "\n"): text_to_translate = text_to_translate[0:last_index - 1] textarea.send_keys(text_to_translate) list_transed_text = getTranslateContent(driver, list_tgt) textarea.clear() list_tgt = list() len_of_batch = 0 text_to_translate = "" loop = loop + 1 time.sleep(0.5) else: text_len = len(text) if (text_len + len_of_batch > 4000): last_index = len(text_to_translate) - 1 if (text_to_translate[last_index] == "\n"): text_to_translate = text_to_translate[0:last_index - 1] textarea.send_keys(text_to_translate) list_transed_text = getTranslateContent(driver, list_tgt) textarea.clear() len_of_batch = 0 list_tgt = list() loop = loop + 1 text_to_translate = "" time.sleep(0.5) len_of_batch = len_of_batch + text_len text = bytes(text, "utf-8").decode('utf-8', 'ignore') text = text.replace("\r", "") not_bmp = x = re.search(u'[\U00010000-\U0010ffff]', text) if not_bmp: continue text_to_translate = text_to_translate + text + " \n" list_tgt.append(text) if list_transed_text: saveFile(translate_file_name, list_transed_text) list_transed_text = list() except Exception as e: print(e) traceback.print_exc() time.sleep(3) #pdb.set_trace() pass #f.close() if (len_of_batch > 0): textarea.send_keys(text_to_translate) list_transed_text = getTranslateContent(driver, list_tgt) if list_transed_text: if (len(list_transed_text) > 0): saveFile(translate_file_name, list_transed_text) driver.close()
from selenium import webdriver from time import sleep from selenium.webdriver.chrome import service from selenium.webdriver.opera.options import Options options = Options() options.binary_location = "path_to_driver" webdriver_service = service.Service("path_to_driver") webdriver_service.start() driver = webdriver.Opera(options=options, executable_path="path_to_.exe_file") driver.get('http://www.google.com') sleep(2) driver.quit()
from selenium import webdriver from selenium.webdriver.opera.options import Options import time import requests import os import json # required to do if opera is not installed in default loacation options = Options() options.binary_location = "A:\\Opera\\launcher.exe" options.add_argument("--remote-debugging-port=9222") # driver initiliazation driver_path = webdriver.Opera( executable_path= "C:\\Users\\nouamane\\Downloads\\Programms\\operadriver.exe", options=options) def play_video(): # global youtube watch url watch_on_yotube = 'http://www.youtube.com/watch?v=' api_key = os.environ.get( 'TUBE_KEY') # get it from google developer api key channel_id = 'UCwO_xoYm2vjhu4kZSxFO5mA' # your favorite youtuber id channel base_url = 'https://www.googleapis.com/youtube/v3/search?' # optional parameters to be passed to the url
def _set_driver(self): if self.browser == 'opera': options = Options() options.binary_location = DriverPath.OPERA_BINARY_PATH print('\nBinary Path: {}'.format(DriverPath.OPERA_BINARY_PATH)) try: self.driver = webdriver.Opera(options=options) except SessionNotCreatedException as e: print('\nSessionNotCreatedException:', e.msg) raise except WebDriverException as e: print('\nWebDriverException:', e.msg) path = self._get_driver_path() print('Trying to look for a \'operadriver\' under:\n{}'.format(path)) self.driver = webdriver.Opera(options=options, executable_path=path) if self.browser == 'chrome': try: self.driver = webdriver.Chrome() except WebDriverException as e: print('\nPlease note:', e.msg) path = self._get_driver_path() print('Trying to look for a \'chromedriver\' under:\n{}'.format(path)) self.driver = webdriver.Chrome(executable_path=path) if self.browser == 'ie': try: self.driver = webdriver.Ie() except WebDriverException as e: print('\nPlease note:', e.msg) path = self._get_driver_path() print('Trying to look for a \'IEDriverServer\' under:\n{}'.format(path)) self.driver = webdriver.Ie(executable_path=path) if self.browser == 'mozilla': try: self.driver = webdriver.Firefox() except WebDriverException as e: print('\nPlease note:', e.msg) path = self._get_driver_path() print('Trying to look for a \'geckodriver\' under:\n{}'.format(path)) self.driver = webdriver.Firefox(executable_path=path) if self.browser == 'edge': # Purpose: Probe the underlying platform’s hardware, operating system, # and interpreter version information. # print('Version tuple:', platform.python_version_tuple()) # print('Compiler :', platform.python_compiler()) # print('Build :', platform.python_build()) if sum(int(i) for i in platform.python_version_tuple()) > 13: print('\nVersion:', platform.python_version()) print('WebDriver is now a Feature On Demand') print('For more info please check: {}'.format( 'https://blogs.windows.com/msedgedev/2018/06/14/' 'webdriver-w3c-recommendation-feature-on-demand/#Rg8g2hRfjBQQVRXy.97\n')) self.driver = webdriver.Edge() else: path = self._get_driver_path() print('Trying to look for a \'MicrosoftWebDriver\' under:\n{}'.format(path)) self.driver = webdriver.Edge(executable_path=path)