我们从Python开源项目中,提取了以下10个代码示例,用于说明如何使用selenium.webdriver.Safari()。
def give_me_the_page(n, user_name, password, broswer, pt = None): if not pt: if broswer=='Chrome': pt = webdriver.Chrome() elif broswer=='Safari': pt = webdriver.Safari() else: pt = webdriver.PhantomJS() pt.get('http://electsys.sjtu.edu.cn/edu/login.aspx') time.sleep(1) pt.execute_script("""var img=document.getElementById('form-input').getElementsByTagName('div')[2].getElementsByTagName('img')[0]; var d=document.createElement('CANVAS'); var cxt=d.getContext('2d'); d.width=img.width; d.height=img.height; cxt.drawImage(img,0,0); img.src=d.toDataURL('png');""")
def __init__(self, base_url=None, pagetitle=None): self.base_url = base_url self.pagetitle = pagetitle # self.driver = webdriver.Firefox() # self.driver.implicitly_wait(30) # self.driver = driver """ ??????????? # self.browser = "Firefox" #??????? # if Action.driver == None: # if self.browser.upper() == 'IE': Action.driver = webdriver.Ie() # elif self.browser.upper() == 'CHROME': Action.driver = webdriver.Chrome() # elif self.browser.upper() == 'FIREFOX': Action.driver = webdriver.Firefox() # elif self.browser.upper() == 'SAFARI': Action.driver = webdriver.Safari() # else: Action.driver = webdriver.Ie() # Action.driver.maximize_window() # #pass # #print u"?????????!" # self.driver = Action.driver self.verificationErrors = [] """ #?????????????????
def setUpClass(cls): super(SeleniumTestCase, cls).setUpClass() # load the webdriver setting as late as possible # this is needed when no web driver is specified and no functional tests should be run from common.settings.webdriver import WEBDRIVER if WEBDRIVER == "firefox": cls.selenium = webdriver.Firefox() elif WEBDRIVER == "chrome": cls.selenium = webdriver.Chrome() elif WEBDRIVER == "safari": cls.selenium = webdriver.Safari() else: raise Exception("Webdriver not configured probably!") cls.selenium.implicitly_wait(10) # wrap also the find_element(s)_by methods of the webdriver and webelement classes decorate(cls.selenium.__class__, wait_for, "find_element_by_") decorate(cls.selenium.__class__, wait_for, "find_elements_by_") decorate(WebElement, wait_for, "find_element_by_") decorate(WebElement, wait_for, "find_elements_by_")
def driver(base_url, osinfo, browserinfo): ostype, os_version = osinfo browser, browser_version = browserinfo buildid = '{0}_{1}_{2}_{3}'.format(ostype.lower().replace(' ', '_'), os_version.lower().replace(' ', '_'), browser, browser_version) # skip some combinations if os_version == 'Sierra' and browser == 'safari' and browser_version == '9.1': pytest.skip('cannot have Sierra running safari 9.1') elif os_version == 'El Capitan' and browser == 'safari' and browser_version == '10': pytest.skip('cannot have El Capitan running safari 10') # set driver if browserstack: username = os.environ.get('BROWSERSTACK_USER', None) access = os.environ.get('BROWSERSTACK_ACCESS_KEY', None) desired_cap = {'os': ostype, 'os_version': os_version, 'browser': browser, 'build': buildid, 'browser_version': browser_version, 'project': 'marvin', 'resolution': '1920x1080'} desired_cap['browserstack.local'] = True desired_cap['browserstack.debug'] = True desired_cap['browserstack.localIdentifier'] = os.environ['BROWSERSTACK_LOCAL_IDENTIFIER'] driver = webdriver.Remote( command_executor='http://{0}:{1}@hub.browserstack.com:80/wd/hub'.format(username, access), desired_capabilities=desired_cap) else: if browser == 'chrome': driver = webdriver.Chrome() elif browser == 'firefox': driver = webdriver.Firefox() elif browser == 'safari': driver = webdriver.Safari() driver.get(base_url) yield driver # teardown driver.quit()
def start_driver_session(context: Context, session_name: str): desired_capabilities = context.desired_capabilities desired_capabilities["name"] = session_name if CONFIG["hub_url"]: context.driver = webdriver.Remote( desired_capabilities=desired_capabilities, command_executor=CONFIG["hub_url"]) else: browser_name = CONFIG["environments"][0]["browser"] drivers = { "chrome": webdriver.Chrome, "edge": webdriver.Edge, "firefox": webdriver.Firefox, "ie": webdriver.Ie, "phantomjs": webdriver.PhantomJS, "safari": webdriver.Safari, } # start the browser context.driver = drivers[browser_name.lower()]() context.driver.set_page_load_timeout(time_to_wait=27) try: context.driver.maximize_window() logging.debug("Maximized the window.") except WebDriverException: logging.debug("Failed to maximize the window.") try: context.driver.set_window_size(1600, 1200) logging.warning("Set window size to 1600x1200") except WebDriverException: logging.warning("Failed to set window size, will continue as is") logging.debug("Browser Capabilities: %s", context.driver.capabilities)
def _make_safari(self , remote , desired_capabilities , profile_dir): return self._generic_make_browser(webdriver.Safari, webdriver.DesiredCapabilities.SAFARI, remote, desired_capabilities)
def run_local(self,os_name,os_version,browser,browser_version): "Return the local driver" local_driver = None if browser.lower() == "ff" or browser.lower() == 'firefox': local_driver = webdriver.Firefox() elif browser.lower() == "ie": local_driver = webdriver.Ie() elif browser.lower() == "chrome": local_driver = webdriver.Chrome() elif browser.lower() == "opera": local_driver = webdriver.Opera() elif browser.lower() == "safari": local_driver = webdriver.Safari() return local_driver
def initWork(): driver = webdriver.Safari() # Get local session of firefoxromedriver return driver # ????
def env_driver(browser): """ Return the web driver. Args: browser: string Returns: selenium WebDriver """ from selenium import webdriver the_driver = False if browser == 'chrome': the_driver = webdriver.Chrome elif browser == 'firefox': the_driver = webdriver.Firefox elif browser == 'safari': the_driver = webdriver.Safari elif browser == 'phantomjs': the_driver = webdriver.PhantomJS elif browser == 'opera': the_driver = webdriver.Opera return the_driver
def create_webdriver(self, driver_name, alias=None, kwargs={}, **init_kwargs): """Creates an instance of a WebDriver. Like `Open Browser`, but allows passing arguments to a WebDriver's __init__. _Open Browser_ is preferred over _Create Webdriver_ when feasible. Returns the index of this browser instance which can be used later to switch back to it. Index starts from 1 and is reset back to it when `Close All Browsers` keyword is used. See `Switch Browser` for example. `driver_name` must be the exact name of a WebDriver in _selenium.webdriver_ to use. WebDriver names include: Firefox, Chrome, Ie, Opera, Safari, PhantomJS, and Remote. Use keyword arguments to specify the arguments you want to pass to the WebDriver's __init__. The values of the arguments are not processed in any way before being passed on. For Robot Framework < 2.8, which does not support keyword arguments, create a keyword dictionary and pass it in as argument `kwargs`. See the [http://selenium.googlecode.com/git/docs/api/py/api.html|Selenium API Documentation] for information about argument names and appropriate argument values. Examples: | # use proxy for Firefox | | | | | ${proxy}= | Evaluate | sys.modules['selenium.webdriver'].Proxy() | sys, selenium.webdriver | | ${proxy.http_proxy}= | Set Variable | localhost:8888 | | | Create Webdriver | Firefox | proxy=${proxy} | | | # use a proxy for PhantomJS | | | | | ${service args}= | Create List | --proxy=192.168.132.104:8888 | | | Create Webdriver | PhantomJS | service_args=${service args} | | Example for Robot Framework < 2.8: | # debug IE driver | | | | | ${kwargs}= | Create Dictionary | log_level=DEBUG | log_file=%{HOMEPATH}${/}ie.log | | Create Webdriver | Ie | kwargs=${kwargs} | | """ if not isinstance(kwargs, dict): raise RuntimeError("kwargs must be a dictionary.") for arg_name in kwargs: if arg_name in init_kwargs: raise RuntimeError("Got multiple values for argument '%s'." % arg_name) init_kwargs[arg_name] = kwargs[arg_name] driver_name = driver_name.strip() try: creation_func = getattr(webdriver, driver_name) except AttributeError: raise RuntimeError("'%s' is not a valid WebDriver name" % driver_name) self._info("Creating an instance of the %s WebDriver" % driver_name) driver = creation_func(**init_kwargs) self._debug("Created %s WebDriver instance with session id %s" % (driver_name, driver.session_id)) return self._cache.register(driver, alias)