我们从Python开源项目中,提取了以下8个代码示例,用于说明如何使用selenium.webdriver.Edge()。
def get_driver(self, task): """Get the webdriver instance""" from selenium import webdriver capabilities = webdriver.DesiredCapabilities.EDGE.copy() extension_src = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'support', 'edge', 'extension') extension_dir = os.path.join(os.environ.get('LOCALAPPDATA'), 'Packages', 'Microsoft.MicrosoftEdge_8wekyb3d8bbwe', 'LocalState', 'wptagent') if not os.path.isdir(extension_dir): os.makedirs(extension_dir) files = os.listdir(extension_src) for file_name in files: try: src = os.path.join(extension_src, file_name) if os.path.isfile(src): shutil.copy(src, extension_dir) except Exception: pass capabilities['extensionPaths'] = [extension_dir] driver = webdriver.Edge(executable_path=self.path, capabilities=capabilities) return driver
def launch(self, _job, task): """Launch the browser""" if self.job['message_server'] is not None: self.job['message_server'].flush_messages() try: logging.debug('Launching Edge : %s', self.path) self.driver = self.get_driver(task) self.driver.set_page_load_timeout(task['time_limit']) if 'browserVersion' in self.driver.capabilities: self.browser_version = self.driver.capabilities['browserVersion'] self.driver.get(self.start_page) logging.debug('Resizing browser to %dx%d', task['width'], task['height']) self.driver.set_window_position(0, 0) self.driver.set_window_size(task['width'], task['height']) # Start the relay agent to capture ETW events wpt_etw_path = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'support', 'edge', 'wpt-etw', 'wpt-etw.exe') if os.path.isfile(self.wpt_etw_done): try: os.remove(self.wpt_etw_done) except Exception: pass from .os_util import run_elevated self.wpt_etw_proc = run_elevated(wpt_etw_path, '--bodies "{0}"'.format(self.bodies_path), wait=False) self.wait_for_extension() if self.extension_loaded: DesktopBrowser.wait_for_idle(self) else: task['error'] = 'Error waiting for wpt-etw to start. Make sure .net is installed' except Exception as err: task['error'] = 'Error starting Microsoft Edge: {0}'.format(err.__str__())
def navigate(self, url): """Navigate to the given URL""" if self.driver is not None: try: self.driver.get(url) except Exception as err: logging.debug("Error navigating Edge: %s", str(err))
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 create_driver(): try: web = webdriver.Firefox() print " [*] Opening Mozila FireFox..." return web except: try: web = webdriver.Chrome() print " [*] We got some errors running Firefox, Opening Google Chrome instead..." return web except: try: web = webdriver.Opera() print " [*] We got some errors running Chrome, Opening Opera instead..." return web except: try: web = webdriver.Edge() print " [*] We got some errors running Opera, Opening Edge instead..." return web except: try: web = webdriver.Ie() print " [*] We got some errors running Edge, Opening Internet Explorer instead..." return web except: print " Error: \n Can not call any WebBrowsers\n Check your Installed Browsers!" exit() #Stolen from stackoverflow :D
def test_edge_manager_with_selenium(path): delete_old_install(path) driver_path = EdgeDriverManager().install(path) dr = webdriver.Edge(driver_path) dr.quit()
def test_edge_manager_with_selenium_cache(path): driver_path = EdgeDriverManager().install(path) dr = webdriver.Edge(driver_path) dr.quit()
def mode_local(self): """Open new brower on local mode""" browser_name = self.bot_config.config['browser'] if browser_name == "chrome": self.curr_caps = DesiredCapabilities.CHROME.copy() self.curr_driver = WebDriver.Chrome( executable_path=self.curr_driver_path, desired_capabilities=self.curr_caps ) elif browser_name == "firefox": self.curr_caps = DesiredCapabilities.FIREFOX.copy() self.curr_driver = WebDriver.Firefox( executable_path=self.curr_driver_path, capabilities=self.curr_caps ) elif browser_name == "iexplorer": self.curr_caps = DesiredCapabilities.INTERNETEXPLORER.copy() self.curr_driver = WebDriver.Ie( executable_path=self.curr_driver_path, capabilities=self.curr_caps ) elif browser_name == "edge": self.curr_caps = DesiredCapabilities.EDGE.copy() self.curr_driver = WebDriver.Edge( executable_path=self.curr_driver_path, capabilities=self.curr_caps ) elif browser_name == "phantomjs": self.curr_caps = DesiredCapabilities.PHANTOMJS.copy() self.curr_driver = WebDriver.PhantomJS( executable_path=self.curr_driver_path, desired_capabilities=self.curr_caps ) elif browser_name == "opera": self.curr_caps = DesiredCapabilities.OPERA.copy() self.curr_driver = WebDriver.Opera( executable_path=self.curr_driver_path, desired_capabilities=self.curr_caps ) else: raise CoreException( message=("config file error, SECTION=bot, KEY=browser isn't " "valid value: {}".format(browser_name)), log=self.log )