def browser(): """ 全局定义浏览器驱动 :return: """ global driver global driver_type if driver_type == "chrome": # 本地chrome浏览器 driver = webdriver.Chrome() driver.set_window_size(1920, 1080) elif driver_type == "firefox": # 本地firefox浏览器 driver = webdriver.Firefox() driver.set_window_size(1920, 1080) elif driver_type == "chrome-headless": # chrome headless模式 chrome_options = CH_Options() chrome_options.add_argument("--headless") chrome_options.add_argument('--disable-gpu') chrome_options.add_argument("--window-size=1920x1080") driver = webdriver.Chrome(options=chrome_options) elif driver_type == "firefox-headless": # firefox headless模式 firefox_options = FF_Options() firefox_options.headless = True driver = webdriver.Firefox(firefox_options=firefox_options) elif driver_type == "chrome-mobile": # chrome mobile模式 # 设置成手机模式 chrome_options = CH_Options() mobile_emulation = {"deviceName": "iPhone 6"} chrome_options.add_experimental_option("mobileEmulation", mobile_emulation) driver = webdriver.Chrome(options=chrome_options) elif driver_type == "grid": # 通过远程节点运行 driver = Remote(command_executor='http://10.2.16.182:4444/wd/hub', desired_capabilities={ "browserName": "chrome", }) driver.set_window_size(1920, 1080) else: raise NameError("driver驱动类型定义错误!") return driver
def browser(): """全局定义浏览器驱动""" global driver if RunConfig.driver_type == "chrome": driver = webdriver.Chrome() driver.maximize_window() elif RunConfig.driver_type == "firefox": driver = webdriver.Firefox() driver.maximize_window() elif RunConfig.driver_type == "chrome-headless": # chrome headless模式 chrome_options = CH_Options() chrome_options.add_argument("--headless") chrome_options.add_argument('--disable-gpu') # chrome_options.add_argument("--window-size=1920x1080") driver = webdriver.Chrome(options=chrome_options) elif RunConfig.driver_type == "firefox-headless": # firefox headless模式 firefox_options = FF_Options() firefox_options.headless = True driver = webdriver.Firefox(firefox_options=firefox_options) elif RunConfig.driver_type == "grid": # 通过远程节点运行 driver = Remote(command_executor='http://localhost:4444/wd/hub', desired_capabilities={"browserName": "chrome"}) # driver.set_window_size(1920, 1080) else: logger.error("driver驱动类型定义错误!") raise NameError("driver驱动类型定义错误!") RunConfig.driver = driver return driver
def browser(name=None): """ Run class initialization method, the default is proper to drive the Firefox browser. Of course, you can also pass parameter for other browser, Chrome browser for the "Chrome", the Internet Explorer browser for "internet explorer" or "ie". :param name: Browser name :return: """ if name is None: name = "chrome" if name == "firefox" or name == "ff": return webdriver.Firefox() elif name == "chrome": return webdriver.Chrome() elif name == "internet explorer" or name == "ie": return webdriver.Ie() elif name == "opera": return webdriver.Opera() elif name == "chrome_headless": chrome_options = CH_Options() chrome_options.add_argument('--headless') return webdriver.Chrome(chrome_options=chrome_options) elif name == "firefox_headless": firefox_options = FF_Options() firefox_options.headless = True return webdriver.Firefox(firefox_options=firefox_options) elif name == 'edge': return webdriver.Edge() else: raise NameError( "Not found {} browser, You can enter 'ie', 'ff', 'opera', 'edge', 'chrome'." .format(name))
def browser(): global driver global driver_type if driver_type == "chrome": driver = webdriver.Chrome() driver.set_window_size(1920, 1080) elif driver_type == "firefox": driver = webdriver.Firefox() driver.set_window_size(1920, 1080) elif driver_type == "chrome-headless": chrome_options = CH_Options() chrome_options.add_argument("--headless") chrome_options.add_argument("--disanle-gpu") chrome_options.add_argument("--windows-size=1920x1080") driver = webdriver.Chrome(options=chrome_options) elif driver_type == "firefox-headless": firefox_options = FF_Options firefox_options.headless = True driver = webdriver.Firefox(firefox_options=firefox_options) else: raise NameError("driver驱动类型定义错误!") return driver
def browser(name=None, driver_path=None, grid_url=None): """ Run class initialization method, the default is proper to drive the Firefox browser. Of course, you can also pass parameter for other browser, Chrome browser for the "Chrome", the Internet Explorer browser for "internet explorer" or "ie". :param name: Browser name :param driver_path: Browser driver path :param grid_url: Either a string representing URL of the remote server or a custom remote_connection.RemoteConnection object. :return: """ if name is None: name = "chrome" if name == "firefox" or name == "ff": if driver_path is not None: return webdriver.Firefox(executable_path=driver_path) if grid_url is not None: webdriver.Remote( command_executor=grid_url, desired_capabilities=DesiredCapabilities.FIREFOX.copy()) return webdriver.Firefox() elif name == "chrome": if driver_path is not None: return webdriver.Chrome(executable_path=driver_path) if grid_url is not None: webdriver.Remote( command_executor=grid_url, desired_capabilities=DesiredCapabilities.CHROME.copy()) return webdriver.Chrome() elif name == "internet explorer" or name == "ie": return webdriver.Ie() elif name == "opera": return webdriver.Opera() elif name == "chrome_headless": chrome_options = CH_Options() chrome_options.add_argument('--headless') if driver_path is not None: return webdriver.Chrome(chrome_options=chrome_options, executable_path=driver_path) return webdriver.Chrome(chrome_options=chrome_options) elif name == "firefox_headless": firefox_options = FF_Options() firefox_options.headless = True if driver_path is not None: return webdriver.Firefox(firefox_options=firefox_options, executable_path=driver_path) return webdriver.Firefox(firefox_options=firefox_options) elif name == 'edge': return webdriver.Edge() elif name == 'safari': return webdriver.Safari() else: raise NameError( "Not found {} browser, You can enter 'ie', 'ff', 'opera', 'edge', 'chrome'." .format(name))
def browser(name=None, driver_path=None, remote_url=None): """ Run class initialization method, the default is proper to drive the Firefox browser. Of course, you can also pass parameter for other browser, Chrome browser for the "Chrome", the Internet Explorer browser for "internet explorer" or "ie". :param name: Browser name :param driver_path: Browser driver path :param remote_url: selenium grid hub url. eg: "127.0.0.1/wd/hub" :return: """ if name is None: name = "chrome" if name == "firefox" or name == "ff": if driver_path is not None: return webdriver.Firefox(executable_path=driver_path) if remote_url is not None: return webdriver.Remote( command_executor=remote_url, desired_capabilities=DesiredCapabilities.FIREFOX) return webdriver.Firefox() elif name == "chrome": if driver_path is not None: return webdriver.Chrome(executable_path=driver_path) if remote_url is not None: return webdriver.Remote( command_executor=remote_url, desired_capabilities=DesiredCapabilities.CHROME) return webdriver.Chrome() elif name == "internet explorer" or name == "ie": return webdriver.Ie() elif name == "opera": return webdriver.Opera() elif name == "chrome_headless": chrome_options = CH_Options() chrome_options.add_argument('--headless') if driver_path is not None: return webdriver.Chrome(chrome_options=chrome_options, executable_path=driver_path) return webdriver.Chrome(chrome_options=chrome_options) elif name == "firefox_headless": firefox_options = FF_Options() firefox_options.headless = True if driver_path is not None: return webdriver.Firefox(firefox_options=firefox_options, executable_path=driver_path) return webdriver.Firefox(firefox_options=firefox_options) elif name == 'edge': return webdriver.Edge() else: raise NameError( "Not found {} browser, You can enter 'ie', 'ff', 'opera', 'edge', 'chrome'." .format(name))
def browser(): """ 全局定义浏览器驱动 :return: """ global driver global driver_type if driver_type == "chrome": # 本地chrome浏览器 options = webdriver.ChromeOptions() prefs = { 'profile.default_content_settings.popups': 0, # 禁止弹出下载窗口 'download.default_directory': r'E:\yuxichen\Product\test_dir\data\down_file' } # 设置下载路径 options.add_experimental_option('prefs', prefs) driver = webdriver.Chrome(options=options) driver.maximize_window() elif driver_type == "firefox": # 本地firefox浏览器 driver = webdriver.Firefox() driver.maximize_window() elif driver_type == "chrome-headless": # chrome headless模式 chrome_options = CH_Options() chrome_options.add_argument("--headless") chrome_options.add_argument('--disable-gpu') chrome_options.add_argument("--window-size=1920x1080") driver = webdriver.Chrome(options=chrome_options) elif driver_type == "firefox-headless": # firefox headless模式 firefox_options = FF_Options() firefox_options.headless = True driver = webdriver.Firefox(firefox_options=firefox_options) elif driver_type == "grid": # 通过远程节点运行 driver = Remote(command_executor='http://10.2.16.182:4444/wd/hub', desired_capabilities={ "browserName": "chrome", }) driver.set_window_size(1920, 1080) else: raise NameError("driver驱动类型定义错误!") #driver=webdriver.Chrome() return driver
def browser(): """ 全局定义浏览器驱动 :return: """ global driver global driver_type if driver_type == "chrome": # 本地chrome浏览器 option = webdriver.ChromeOptions() option.add_argument('--headless') option.add_argument('--disable-gpu') option.add_argument("user-agent='Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108-1 Safari/537.36'") driver = webdriver.Chrome(chrome_options=option,executable_path="/opt/google/chrome/chromedriver") driver.set_window_size(1920, 1080) elif driver_type == "firefox": # 本地firefox浏览器 driver = webdriver.Firefox() driver.set_window_size(1920, 1080) elif driver_type == "chrome-headless": # chrome headless模式 chrome_options = CH_Options() chrome_options.add_argument("--headless") chrome_options.add_argument('--disable-gpu') chrome_options.add_argument("--window-size=1920x1080") driver = webdriver.Chrome(options=chrome_options) elif driver_type == "firefox-headless": # firefox headless模式 firefox_options = FF_Options() firefox_options.headless = True driver = webdriver.Firefox(firefox_options=firefox_options) elif driver_type == "grid": # 通过远程节点运行 driver = Remote(command_executor='http://10.2.16.182:4444/wd/hub', desired_capabilities={ "browserName": "chrome", }) driver.set_window_size(1920, 1080) else: raise NameError("driver驱动类型定义错误!") return driver
def browser(): """ 全局定义浏览器驱动 :return: """ global driver global driver_type if driver_type == "chrome": # 本地chrome浏览器 option = webdriver.ChromeOptions() option.add_argument("disable-infobars") path = "D:\\PyCharm\\workspace\\webdriver\\75.0.3770.90\\chromedriver.exe" driver = webdriver.Chrome(options=option, executable_path=path) driver.maximize_window() # driver.set_window_size(1920, 1080) elif driver_type == "firefox": # 本地firefox浏览器 driver = webdriver.Firefox() driver.set_window_size(1920, 1080) elif driver_type == "chrome-headless": # chrome headless模式 chrome_options = CH_Options() chrome_options.add_argument("--headless") chrome_options.add_argument('--disable-gpu') chrome_options.add_argument("--window-size=1920x1080") driver = webdriver.Chrome(options=chrome_options) elif driver_type == "firefox-headless": # firefox headless模式 firefox_options = FF_Options() firefox_options.headless = True driver = webdriver.Firefox(firefox_options=firefox_options) elif driver_type == "grid": # 通过远程节点运行 driver = Remote(command_executor='http://10.2.16.182:4444/wd/hub', desired_capabilities={ "browserName": "chrome", }) driver.set_window_size(1920, 1080) else: raise NameError("driver驱动类型定义错误!") return driver
def browser(self, driver): """ Define global browser driver :return: """ if driver == "chrome": driver = webdriver.Chrome() driver.maximize_window() logger.info("启动chrome浏览器") elif driver == "firefox": driver = webdriver.Firefox() driver.maximize_window() logger.info("启动firefox浏览器") elif driver == "IE": driver = webdriver.Ie() driver.maximize_window() logger.info("启动IE浏览器") elif driver == "chrome-headless": chrome_options = CH_Options() chrome_options.add_argument("--headless") chrome_options.add_argument("--disable-gpu") chrome_options.add_argument("--window-size=1920x1080") driver = webdriver.Chrome(chrome_options=chrome_options) logger.info("启动Chrome headless浏览器") elif driver == "firefox-headless": firefox_options = FF_Options() firefox_options.headless = True driver = webdriver.Firefox(firefox_options=firefox_options) elif driver == "grid": #Remote selenium grid driver = Remote(command_executor='http://10.12.16.118:4444/wd/hub', desired_capabilities={ "browserName": "chrome", }) driver.maximize_window() else: raise NameError("driver驱动类型定义错误!") driver.implicitly_wait(5) logger.info("设置5秒隐式等待") return driver
def driver(): """ 全局定义浏览器驱动 :return: """ global dr if driver_type == "chrome": # 本地chrome浏览器 dr = SD.Chrome() dr.maximize_window() elif driver_type == "firefox": # 本地firefox浏览器 dr = SD.Firefox() dr.maximize_window() elif driver_type == "chrome-headless": # chrome headless模式 chrome_options = CH_Options() chrome_options.add_argument("--headless") chrome_options.add_argument('--disable-gpu') chrome_options.add_argument("--window-size=1920x1080") dr = SD.Chrome(options=chrome_options) elif driver_type == "firefox-headless": # firefox headless模式 firefox_options = FF_Options() firefox_options.headless = True dr = SD.Firefox(firefox_options=firefox_options) elif driver_type == "grid": # 通过远程节点运行 dr = Remote(command_executor='http://10.2.16.182:4444/wd/hub', desired_capabilities={ "browserName": "chrome", }) dr.maximize_window() elif driver_type == "app": # appium dr = AD.Remote("http://localhost:4723/wd/hub", caps) CommonPage(dr).skip_welcome_page() else: raise NameError("driver驱动类型定义错误!") return dr
def browser(): """ 全局定义浏览器驱动 :return: """ global driver if RunConfig.driver_type == "chrome": # 本地chrome浏览器 options = webdriver.ChromeOptions() options.add_argument( 'user-agent="Mozilla/5.0 (iPhone; CPU iPhone OS 9_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B143 Safari/601.1 MucWebAutoTester/dale"' ) driver = webdriver.Chrome(chrome_options=options) elif RunConfig.driver_type == "firefox": # 本地firefox浏览器 driver = webdriver.Firefox() driver.maximize_window() elif RunConfig.driver_type == "chrome-headless": # chrome headless模式 chrome_options = CH_Options() chrome_options.add_argument("--headless") chrome_options.add_argument('--disable-gpu') chrome_options.add_argument("--window-size=1920x1080") driver = webdriver.Chrome(options=chrome_options) elif RunConfig.driver_type == "firefox-headless": # firefox headless模式 firefox_options = FF_Options() firefox_options.headless = True driver = webdriver.Firefox(firefox_options=firefox_options) elif RunConfig.driver_type == "grid": # 通过远程节点运行 driver = Remote(command_executor='http://localhost:4444/wd/hub', desired_capabilities={ "browserName": "chrome", }) driver.set_window_size(1920, 1080) else: raise NameError("driver驱动类型定义错误!") return driver
def browser(driver_type): """ 全局定义浏览器驱动 :return: """ global driver if driver_type == "Chrome": # 本地chrome浏览器 driver = webdriver.Chrome() driver.maximize_window() driver.implicitly_wait(5) # driver.set_window_size(1920, 1080) elif driver_type == "firefox": # 本地firefox浏览器 driver = webdriver.Firefox() driver.maximize_window() driver.implicitly_wait(5) # driver.set_window_size(1920, 1080) elif driver_type == "chrome-headless": # chrome headless模式 : 可以在后台运行浏览器 chrome_options = CH_Options() chrome_options.add_argument("--headless") chrome_options.add_argument('--disable-gpu') chrome_options.add_argument("--window-size=1920x1080") driver = webdriver.Chrome(options=chrome_options) elif driver_type == "firefox-headless": # firefox headless模式 firefox_options = FF_Options() firefox_options.headless = True driver = webdriver.Firefox(firefox_options=firefox_options) elif driver_type == "grid": # 通过远程节点运行 driver = Remote(command_executor='http://10.2.16.182:4444/wd/hub', desired_capabilities={ "browserName": "chrome", }) driver.set_window_size(1920, 1080) else: raise NameError("driver驱动类型定义错误!") return driver
def get_driver(request): browser = request.config.getoption('--browser') url = request.config.getoption('--url') results_dir = path.join(getcwd(), 'tests/results') if not path.exists(results_dir): makedirs(results_dir) log_path = path.join(results_dir, 'driver.log') drivers_dir = path.join(getcwd(), 'tests/drivers') driver = None driver_name = dict(firefox='geckodriver', chrome='chromedriver')[browser] driver_path = path.join(drivers_dir, driver_name) if browser == 'firefox': options = FX_Options() options.headless = True driver = webdriver.Firefox(executable_path=driver_path, service_log_path=log_path, options=options) elif browser == 'chrome': options = CH_Options() options.add_argument('headless') driver = webdriver.Chrome(executable_path=driver_path, service_log_path=log_path, options=options) elif browser == 'ie': driver = webdriver.Ie(executable_path=driver_path, service_log_path=log_path) driver.maximize_window() driver.get(url) driver.delete_all_cookies() def driver_finalizer(): driver.quit() request.addfinalizer(driver_finalizer) return driver
def browser(): """ 定义全局浏览器驱动 """ global driver global driver_type if driver_type == "chrome": driver = webdriver.Chrome() driver.set_window_size(1920, 1080) elif driver_type == "firefox": driver = webdriver.Firefox() driver.set_window_size(1920, 1080) elif driver_type == "safari": driver = webdriver.Safari() driver.set_window_size(1920, 1080) elif driver_type == "chrome-headless": # chrome 无头模式 chrome_options = CH_Options() chrome_options.add_argument("--headles") chrome_options.add_argument("--disable-gpu") chrome_options.add_argument("--window-size=1920x1080") driver = webdriver.Chrome(chrome_options=chrome_options) elif driver_type == "firefox-headless": # firefox 无头模式 firefox_options = FF_Options() firefox_options.headless = True driver = webdriver.Firefox(firefox_options=firefox_options) elif driver_type == "grid": # 通过远程节点运行 driver = Remote(command_executor='http://10.2.16.182:4444/wd/hub', desired_capabilities={"browserName": "chrome"}) driver.set_window_size(1920, 1080) else: raise NameError("driver驱动类型定义错误") return driver
# # 记录用户信息(登录态) # option = webdriver.ChromeOptions() # option.add_argument(RunConfig.option_address) # # 本地chrome浏览器 driver = webdriver.Chrome(BASE_DIR+'/chromedriver.exe') >>>>>>> 4ecf11f61d068bb3d95b2cdfadc20314c1548c47 driver.maximize_window() elif RunConfig.driver_type == "firefox": # 本地firefox浏览器 driver = webdriver.Firefox() driver.maximize_window() elif RunConfig.driver_type == "chrome-headless": # chrome headless模式 chrome_options = CH_Options() chrome_options.add_argument("--headless") chrome_options.add_argument('--disable-gpu') chrome_options.add_argument("--window-size=1920x1080") driver = webdriver.Chrome(options=chrome_options) elif RunConfig.driver_type == "firefox-headless": # firefox headless模式 firefox_options = FF_Options() firefox_options.headless = True driver = webdriver.Firefox(firefox_options=firefox_options) elif RunConfig.driver_type == "grid": # 通过远程节点运行 driver = Remote(command_executor='http://localhost:4444/wd/hub', desired_capabilities={
def browser(name=None, driver_path=None, grid_url=None): """ Run class initialization method, the default is proper to drive the Firefox browser. Of course, you can also pass parameter for other browser, Chrome browser for the "Chrome", the Internet Explorer browser for "internet explorer" or "ie". :param name: Browser name :param driver_path: Browser driver path :param grid_url: Either a string representing URL of the remote server or a custom remote_connection.RemoteConnection object. :return: """ CHROMEDRIVER = "chromedriver" FIREFOXDRIVER = "geckodriver" IEDRIVER = "IEDriverServer.exe" OPERADRIVER = "operadriver" EDGEDRIVER = "MicrosoftWebDriver.exe" SAFAIRDRIVER = "/usr/bin/safaridriver" # Prevention of detection option = ChromeOptions() option.add_experimental_option('excludeSwitches', ['enable-automation']) if name is None: name = "chrome" if name in ["firefox", "ff"]: if driver_path is None: driver_path = FIREFOXDRIVER if grid_url is not None: return webdriver.Remote( command_executor=grid_url, desired_capabilities=DesiredCapabilities.FIREFOX.copy()) return webdriver.Firefox(executable_path=driver_path) elif name in ["chrome", "google chrome", "gc"]: if driver_path is None: driver_path = CHROMEDRIVER if grid_url is not None: return webdriver.Remote( command_executor=grid_url, desired_capabilities=DesiredCapabilities.CHROME.copy()) driver = webdriver.Chrome(options=option, executable_path=driver_path) driver.execute_cdp_cmd( "Page.addScriptToEvaluateOnNewDocument", { "source": """ Object.defineProperty(navigator, 'webdriver', { get: () => undefined })""" }) return driver elif name in ["internet explorer", "ie", "IE"]: if driver_path is None: driver_path = IEDRIVER return webdriver.Ie(executable_path=driver_path) elif name == "opera": if driver_path is None: driver_path = OPERADRIVER return webdriver.Opera(executable_path=driver_path) elif name == "chrome_headless": if driver_path is None: driver_path = CHROMEDRIVER chrome_options = CH_Options() chrome_options.add_argument('--headless') driver = webdriver.Chrome(chrome_options=chrome_options, options=option, executable_path=driver_path) driver.execute_cdp_cmd( "Page.addScriptToEvaluateOnNewDocument", { "source": """ Object.defineProperty(navigator, 'webdriver', { get: () => undefined })""" }) return driver elif name == "firefox_headless": firefox_options = FF_Options() firefox_options.headless = True if driver_path is None: driver_path = FIREFOXDRIVER return webdriver.Firefox(firefox_options=firefox_options, executable_path=driver_path) elif name == 'edge': if driver_path is None: driver_path = EDGEDRIVER return webdriver.Edge(executable_path=driver_path) elif name == 'safari': if driver_path is None: driver_path = SAFAIRDRIVER return webdriver.Safari(executable_path=driver_path) elif name in PHONE_LIST: if driver_path is None: driver_path = CHROMEDRIVER options = CH_Options() options.add_experimental_option("mobileEmulation", {"deviceName": name}) driver = webdriver.Chrome(chrome_options=options, executable_path=driver_path, options=option) driver.set_window_size(width=480, height=900) driver.execute_cdp_cmd( "Page.addScriptToEvaluateOnNewDocument", { "source": """ Object.defineProperty(navigator, 'webdriver', { get: () => undefined })""" }) return driver elif name in PAD_LIST: if driver_path is None: driver_path = CHROMEDRIVER options = CH_Options() options.add_experimental_option("mobileEmulation", {"deviceName": name}) driver = webdriver.Chrome(chrome_options=options, executable_path=driver_path, options=option) driver.set_window_size(width=1100, height=900) driver.execute_cdp_cmd( "Page.addScriptToEvaluateOnNewDocument", { "source": """ Object.defineProperty(navigator, 'webdriver', { get: () => undefined })""" }) return driver else: raise NameError( "Not found '{}' browser, See the help doc: https://github.com/SeldomQA/seldom/blob/master/docs/driver.md'." .format(name))
import os import unittest from time import sleep from selenium import webdriver from selenium.webdriver.common.keys import Keys from selenium.webdriver.chrome.options import Options as CH_Options from function import Excel url = 'https://www.sogou.com/' mobileEmulation = {'deviceName': 'iPhone X'} #options = webdriver.ChromeOptions() options = CH_Options() options.add_argument("--headless") options.add_argument('--disable-gpu') options.add_argument("--window-size=1920x1080") options.add_argument('--no-sandbox')# 在Ubuntu上执行要加上这句 options.add_experimental_option('mobileEmulation', mobileEmulation) chrome_driver_path = os.path.split(os.path.realpath(__file__))[0] + '/chromedriver' search_key = ['英荔', '英荔教育', '英荔商学院'] search_ele = [r'#mainBody > div.results > div:nth-child(1) > a > h3', r'#sogou_vr_30000909_2 > h3 > a', r'#sogou_vr_30000909_3 > h3 > a', r'#sogou_vr_30000909_5 > h3 > a', r'#sogou_vr_30000909_6 > h3 > a',] search_eles = [r'#mainBody > div.results > div:nth-child(1) > a > h3', r'#mainBody > div.results > div:nth-child(3) > a > h3', r'#sogou_vr_30000909_3 > h3 > a', r'#mainBody > div.results > div:nth-child(12) > a > h3',
def browser(): """ 全局定义浏览器驱动 :return: """ global driver global driver_type if driver_type == "chrome": # 本地chrome浏览器 driver = webdriver.Chrome() driver.set_window_size(1920, 1080) elif driver_type == "chrome_phone": # 学生按分辨率启动 WIDTH = 960 HEIGHT = 600 PIXEL_RATIO = 3.0 UA = 'Mozilla/5.0 (Linux; Android 4.1.1; GT-N7100 Build/JRO03C) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/35.0.1916.138 Mobile Safari/537.36 T7/6.3' mobileEmulation = { "deviceMetrics": { "width": WIDTH, "height": HEIGHT, "pixelRatio": PIXEL_RATIO }, "userAgent": UA } options = webdriver.ChromeOptions() options.add_experimental_option('mobileEmulation', mobileEmulation) driver = webdriver.Chrome(executable_path='chromedriver.exe', chrome_options=options) driver.maximize_window() driver.implicitly_wait(10) elif driver_type == "firefox": # 本地firefox浏览器 driver = webdriver.Firefox() driver.set_window_size(1920, 1080) elif driver_type == "chrome-headless": # chrome headless模式 chrome_options = CH_Options() chrome_options.add_argument("--headless") chrome_options.add_argument('--disable-gpu') chrome_options.add_argument("--window-size=1920x1080") driver = webdriver.Chrome(options=chrome_options) elif driver_type == "firefox-headless": # firefox headless模式 firefox_options = FF_Options() firefox_options.headless = True driver = webdriver.Firefox(firefox_options=firefox_options) elif driver_type == "grid": # 通过远程节点运行 driver = Remote(command_executor='http://10.2.16.182:4444/wd/hub', desired_capabilities={ "browserName": "chrome", }) driver.set_window_size(1920, 1080) else: raise NameError("driver驱动类型定义错误!") return driver
def Browser(name=None, driver_path=None, grid_url=None, **kwargs): """ Run class initialization method, the default is proper to drive the Firefox browser. Of course, you can also pass parameter for other browser, Chrome browser for the "Chrome", the Internet Explorer browser for "internet explorer" or "ie". :param name: Browser name :param driver_path: Browser driver path :param grid_url: Either a string representing URL of the remote server or a custom remote_connection.RemoteConnection object. :return: """ if name is None: name = "chrome" if name in ["firefox", "ff"]: if driver_path is not None: return webdriver.Firefox(executable_path=driver_path, proxy=BrowserConfig.proxy) if grid_url is not None: return webdriver.Remote( command_executor=grid_url, desired_capabilities=DesiredCapabilities.FIREFOX.copy(), proxy=BrowserConfig.proxy) return webdriver.Firefox() elif name in ["chrome", "google chrome", "gc"]: if driver_path is not None: return webdriver.Chrome(executable_path=driver_path) if grid_url is not None: return webdriver.Remote( command_executor=grid_url, desired_capabilities=DesiredCapabilities.CHROME.copy()) return webdriver.Chrome() # elif name == ["internet explorer", "ie", "IE"]: # return webdriver.Ie() # elif name == "opera": # return webdriver.Opera() elif name == "chrome_headless": chrome_options = CH_Options() chrome_options.add_argument('--headless') if driver_path is not None: return webdriver.Chrome(chrome_options=chrome_options, executable_path=driver_path) return webdriver.Chrome(chrome_options=chrome_options) elif name == "firefox_headless": firefox_options = FF_Options() firefox_options.headless = True if driver_path is not None: return webdriver.Firefox(firefox_options=firefox_options, executable_path=driver_path) return webdriver.Firefox(firefox_options=firefox_options) elif name == "Android": if "config" not in kwargs.keys(): log.debug("请填写android 机的配置信息") return 0 else: try: with open(kwargs.get("config", "resource/Android_Config.yaml"), "r", encoding="utf-8") as f: config = yaml.safe_load(f) except FileNotFoundError as e: log.error(e) return 0 aconfig = AndroidConfig() RemoteUrl = config.get("remote") if RemoteUrl is None: log.error("请检查配置文件") return 0 else: config.pop("remote") for i in config: setattr(aconfig, i, config[i]) return androiddriver.Remote(RemoteUrl, config) # elif name == 'edge': # return webdriver.Edge() # elif name == 'safari': # return webdriver.Safari() # elif name in PHONE_LIST: # options = CH_Options() # options.add_experimental_option("mobileEmulation", {"deviceName": name}) # driver = webdriver.Chrome(chrome_options=options, executable_path=driver_path) # driver.set_window_size(width=480, height=900) # return driver # elif name in PAD_LIST: # options = CH_Options() # options.add_experimental_option("mobileEmulation", {"deviceName": name}) # driver = webdriver.Chrome(chrome_options=options, executable_path=driver_path) # driver.set_window_size(width=1100, height=900) # return driver else: raise NameError("Not found '{}' browser.".format(name))
def browser(name=None, driver_path=None, grid_url=None): """ Run class initialization method, the default is proper to drive the Firefox browser. Of course, you can also pass parameter for other browser, Chrome browser for the "Chrome", the Internet Explorer browser for "internet explorer" or "ie". :param name: Browser name :param driver_path: Browser driver path :param grid_url: Either a string representing URL of the remote server or a custom remote_connection.RemoteConnection object. :return: """ if name is None: name = "chrome" if name == "firefox" or name == "ff": if driver_path is not None: return webdriver.Firefox(executable_path=driver_path) if grid_url is not None: webdriver.Remote( command_executor=grid_url, desired_capabilities=DesiredCapabilities.FIREFOX.copy()) return webdriver.Firefox() elif name == "chrome": if driver_path is not None: return webdriver.Chrome(executable_path=driver_path) if grid_url is not None: webdriver.Remote( command_executor=grid_url, desired_capabilities=DesiredCapabilities.CHROME.copy()) return webdriver.Chrome() elif name == "internet explorer" or name == "ie": return webdriver.Ie() elif name == "opera": return webdriver.Opera() elif name == "chrome_headless": chrome_options = CH_Options() chrome_options.add_argument('--headless') if driver_path is not None: return webdriver.Chrome(chrome_options=chrome_options, executable_path=driver_path) return webdriver.Chrome(chrome_options=chrome_options) elif name == "firefox_headless": firefox_options = FF_Options() firefox_options.headless = True if driver_path is not None: return webdriver.Firefox(firefox_options=firefox_options, executable_path=driver_path) return webdriver.Firefox(firefox_options=firefox_options) elif name == 'edge': return webdriver.Edge() elif name == 'safari': return webdriver.Safari() elif name in PHONE_LIST: options = CH_Options() options.add_experimental_option("mobileEmulation", {"deviceName": name}) driver = webdriver.Chrome(chrome_options=options, executable_path=driver_path) driver.set_window_size(width=480, height=900) return driver elif name in PAD_LIST: options = CH_Options() options.add_experimental_option("mobileEmulation", {"deviceName": name}) driver = webdriver.Chrome(chrome_options=options, executable_path=driver_path) driver.set_window_size(width=1100, height=900) return driver else: raise NameError( "Not found '{}' browser, See the help doc: https://github.com/SeldomQA/seldom/blob/master/docs/driver.md'." .format(name))