Selenium - WebDriver Advanced Usage

來源:互聯網
上載者:User

標籤:

Explicit Waits
# Pythonfrom selenium import webdriverfrom selenium.webdriver.common.by import Byfrom selenium.webdriver.support.ui import WebDriverWait # available since 2.4.0from selenium.webdriver.support import expected_conditions as EC # available since 2.26.0ff = webdriver.Firefox()ff.get("http://somedomain/url_that_delays_loading")try:    element = WebDriverWait(ff, 10).until(EC.presence_of_element_located((By.ID, "myDynamicElement")))finally:    ff.quit()
  • Expected Conditions
# Pythonfrom selenium.webdriver.support import expected_conditions as ECwait = WebDriverWait(driver, 10)element = wait.until(EC.element_to_be_clickable((By.ID,‘someid‘)))
Implicit Waits
# Pythonfrom selenium import webdriverff = webdriver.Firefox()ff.implicitly_wait(10) # secondsff.get("http://somedomain/url_that_delays_loading")myDynamicElement = ff.find_element_by_id("myDynamicElement")
Remote WebDriver
  • Taking a Screenshot
# Pythonfrom selenium import webdriverdriver = webdriver.Remote("http://localhost:4444/wd/hub", webdriver.DesiredCapabilities.FIREFOX.copy())driver.get("http://www.google.com")driver.get_screenshot_as_file(‘/Screenshots/google.png‘)
  • Using a FirefoxProfile
# Pythonfrom selenium import webdriverfp = webdriver.FirefoxProfile()# set something on the profile...driver = webdriver.Remote(desired_capabilities=webdriver.DesiredCapabilities.FIREFOX, browser_profile=fp)
  • Using ChromeOptions
# Pythonfrom selenium import webdriveroptions = webdriver.ChromeOptions()# set some optionsdriver = webdriver.Remote(desired_capabilities=options.to_capabilities())
Using a Proxy
  • Internet Explorer
# Pythonfrom selenium import webdriverPROXY = "localhost:8080"# Create a copy of desired capabilities object.desired_capabilities = webdriver.DesiredCapabilities.INTERNETEXPLORER.copy()# Change the proxy properties of that copy.desired_capabilities[‘proxy‘] = {    "httpProxy":PROXY,    "ftpProxy":PROXY,    "sslProxy":PROXY,    "noProxy":None,    "proxyType":"MANUAL",    "class":"org.openqa.selenium.Proxy",    "autodetect":False}# you have to use remote, otherwise you‘ll have to code it yourself in python to # dynamically changing the system proxy preferencesdriver = webdriver.Remote("http://localhost:4444/wd/hub", desired_capabilities)
  • Chrome

  Is basically the same as internet explorer. It uses the same configuration on the machine as IE does (on windows). On Mac it uses the System Preference -> Network settings. On Linux it uses (on Ubuntu) System > Preferences > Network Proxy Preferences (Alternatively in “/etc/environment” set http_proxy). As of this writing it is unknown how to set the proxy programmatically.

  • Firefox
# Pythonfrom selenium import webdriverfrom selenium.webdriver.common.proxy import *myProxy = "host:8080"proxy = Proxy({    ‘proxyType‘: ProxyType.MANUAL,    ‘httpProxy‘: myProxy,    ‘ftpProxy‘: myProxy,    ‘sslProxy‘: myProxy,    ‘noProxy‘: ‘‘ # set this value as desired    })driver = webdriver.Firefox(proxy=proxy)# for remotecaps = webdriver.DesiredCapabilities.FIREFOX.copy()proxy.add_to_capabilities(caps)driver = webdriver.Remote(desired_capabilities=caps)
Data Driven Testing
# Python# Collection of String valuessource = open("input_file.txt", "r")values = source.readlines()source.close()# Execute For loop for each String in the values arrayfor search in values:    driver.get(‘http://www.google.com‘)    driver.find_element_by_name("q").send_keys(search)    driver.find_element_by_id("btnG").click()    element = WebDriverWait(driver, 5).until(ExpectedConditions.presence_of_element_located((By.XPATH, "//*[contains(., ‘Results‘)]"))    assert search in element.text

 

Selenium - WebDriver Advanced Usage

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.