我们从Python开源项目中,提取了以下7个代码示例,用于说明如何使用selenium.webdriver.common.desired_capabilities.DesiredCapabilities.CHROME。
def _get_desired_capabilities(): """ Return a dictionary of browser settings for a Selenium web driver. """ platform = _TEST_SETTINGS['PLATFORM'] browser = _TEST_SETTINGS['BROWSER'] version = _TEST_SETTINGS['VERSION'] if platform and browser: capabilities = { 'platform': platform, 'browserName': browser, 'version': version, } elif browser.lower() == 'firefox': capabilities = DesiredCapabilities.FIREFOX else: capabilities = DesiredCapabilities.CHROME return _add_travis_info(capabilities)
def __init__(self): try: self.driver = webdriver.Remote(command_executor='http://chrome:4444/wd/hub', desired_capabilities=DesiredCapabilities.CHROME) except URLError as e: print("Failed to connect!") raise e
def __init__(self): self.remote = "http://selenium:4444/wd/hub" self.browser_type = DesiredCapabilities.CHROME self.browser = None
def connect_to_selenium(port, t=30, c=1): global Browser if c >= t: return False try: if verbose: print ('Connecting to docker selenium server ' 'http://127.0.0.1:%s/wd/hub [%s/%s]...') % (str(port), str(c), str(t)) if driver == 'chrome': desired_caps = DesiredCapabilities.CHROME elif driver == 'firefox': desired_caps = DesiredCapabilities.FIREFOX Browser = webdriver.Remote( command_executor='http://127.0.0.1:%s/wd/hub' % str(port), desired_capabilities=desired_caps ) except: c += 1 sleep(1) connect_to_selenium(port, t, c) return True
def test_page(): driver = webdriver.Remote( desired_capabilities=DesiredCapabilities.CHROME, command_executor="http://%s:4444" % host ) driver.get('http://nginx:8000') print driver.title driver.get_screenshot_as_file('screenshot.png') driver.quit()
def setUpClass(cls): super(SeleniumTestCase, cls).setUpClass() cls.driver = webdriver.Remote( command_executor='http://127.0.0.1:4444/wd/hub', desired_capabilities=DesiredCapabilities.CHROME) cls.driver.implicitly_wait(10)
def load_page(page_url, remote_server, max_load_seconds, \ screenshot_dir, should_save_screenshot): load_timeout = 120 # seconds is_ok = True driver = webdriver.Remote(command_executor = remote_server, \ desired_capabilities=DesiredCapabilities.CHROME) try: # Cleanup cache driver.delete_all_cookies() # driver.set_page_load_timeout(load_timeout) print("Open page: %s" % (page_url)) start_clock = time.clock() end_clock = time.clock() elapsed_seconds = ((end_clock - start_clock) * 1000 - sleep_delay) if elapsed_seconds > max_load_seconds: print("ERROR: page load is too slow. It took %s seconds, more than %d seconds." \ % ("{:.2f}".format(elapsed_seconds), max_load_seconds)) is_ok = False else: print("Page load took: %s seconds." % ("{:.2f}".format(elapsed_seconds))) all_warnings = driver.get_log('browser') critical_errors = [] for warning in all_warnings: if warning['level'] == 'SEVERE': has_error = True for ignore_err in IGNORE_ERROR_LIST: if ignore_err in warning['message']: has_error = False break if has_error is True: critical_errors.append(warning) if len(critical_errors) != 0: print("ERROR: severe errors have happened when loading the page. Details:\n\t%s" \ % "\n\t".join([str(error) for error in critical_errors])) is_ok = False save_screenshot_filepath = "%s/%s-%s.png" % \ (screenshot_dir, datetime.now().strftime('%Y-%m-%d_%H%M%S'), \ page_url.rstrip("/").split("/")[-1]) if should_save_screenshot is True: print("Save screenshot to %s" % (save_screenshot_filepath)) driver.get_screenshot_as_file(save_screenshot_filepath) except Exception as e: print("ERROR: get exception: %s" % (e)) is_ok = False finally: driver.close() # quit session driver.quit() return is_ok