Selenium - WebDriver API

Official API document: https://seleniumhq.github.io/selenium/docs/api/py/api.html

Recommanded Import Style

from selenium import webdriver

Then, you can access the classes like this:

webdriver.Firefox
webdriver.FirefoxProfile
webdriver.Chrome
webdriver.ChromeOptions
webdriver.Ie
webdriver.Opera
webdriver.PhantomJS
webdriver.Remote
webdriver.DesiredCapabilities
webdriver.ActionChains
webdriver.TouchActions
webdriver.Proxy

Other import:

from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import [TheNameOfTheExceptionClass]

Conventions used in the API:

driver.current_url

driver.close()

Exceptions:

  • selenium.common.exceptions.ElementNotSelectableException
  • selenium.common.exceptions.ElementNotVisibleException
  • selenium.common.exceptions.ErrorInResponseException
  • selenium.common.exceptions.ImeActivationFailedException
  • selenium.common.exceptions.ImeNotAvailableException
  • selenium.common.exceptions.InvalidCookieDomainException
  • selenium.common.exceptions.InvalidElementStateException
  • selenium.common.exceptions.InvalidSelectorException
  • selenium.common.exceptions.InvalidSwitchToTargetException
  • selenium.common.exceptions.MoveTargetOutOfBoundsException
  • selenium.common.exceptions.NoAlertPresentException
  • selenium.common.exceptions.NoSuchAttributeException
  • selenium.common.exceptions.NoSuchElementException
  • selenium.common.exceptions.NoSuchFrameException
  • selenium.common.exceptions.NoSuchWindowException
  • selenium.common.exceptions.RemoteDriverServerException
  • selenium.common.exceptions.StaleElementReferenceException
  • selenium.common.exceptions.TimeoutException
  • selenium.common.exceptions.UnableToSetCookieException
  • selenium.common.exceptions.UnexpectedAlertPresentException
  • selenium.common.exceptions.UnexpectedTagNameException
  • selenium.common.exceptions.WebDriverException

Action Chains

class selenium.webdriver.common.action_chains.ActionChains(driver)

ActionChains are a way to automate low level interactions such as mouse movements, mouse button actions, key press, and context menu interactions. This is useful for doing more complex actions like hover over and drag and drop.

Generate user actions.
When you call methods for actions on the ActionChains object, the actions are stored in a queue in the ActionChains object. When you call perform(), the events are fired in the order they are queued up.

ActionChains can be used in a chain pattern:

menu = driver.find_element_by_css_selector(".nav")
hidden_submenu = driver.find_element_by_css_selector(".nav #submenu1")

ActionChains(driver).move_to_element(menu).click(hidden_submenu).perform()

Or actions can be queued up one by one, then performed.:

menu = driver.find_element_by_css_selector(".nav")
hidden_submenu = driver.find_element_by_css_selector(".nav #submenu1")

actions = ActionChains(driver)
actions.move_to_element(menu)
actions.click(hidden_submenu)
actions.perform()

Alerts

class selenium.webdriver.common.alert.Alert(driver)

Alert(driver).accept()
Alert(driver).dismiss()

Special Keys

class selenium.webdriver.common.keys.Keys

ADD = u'\ue025'
ALT = u'\ue00a'
ARROW_DOWN = u'\ue015'
ARROW_LEFT = u'\ue012'
ARROW_RIGHT = u'\ue014'
ARROW_UP = u'\ue013'
BACKSPACE = u'\ue003'
BACK_SPACE = u'\ue003'
CANCEL = u'\ue001'
CLEAR = u'\ue005'
COMMAND = u'\ue03d'
CONTROL = u'\ue009'
DECIMAL = u'\ue028'
DELETE = u'\ue017'
DIVIDE = u'\ue029'
DOWN = u'\ue015'
END = u'\ue010'
ENTER = u'\ue007'
EQUALS = u'\ue019'
ESCAPE = u'\ue00c'
F1 = u'\ue031'
F10 = u'\ue03a'
F11 = u'\ue03b'
F12 = u'\ue03c'
F2 = u'\ue032'
F3 = u'\ue033'
F4 = u'\ue034'
F5 = u'\ue035'
F6 = u'\ue036'
F7 = u'\ue037'
F8 = u'\ue038'
F9 = u'\ue039'
HELP = u'\ue002'
HOME = u'\ue011'
INSERT = u'\ue016'
LEFT = u'\ue012'
LEFT_ALT = u'\ue00a'
LEFT_CONTROL = u'\ue009'
LEFT_SHIFT = u'\ue008'
META = u'\ue03d'
MULTIPLY = u'\ue024'
NULL = u'\ue000'
NUMPAD0 = u'\ue01a'
NUMPAD1 = u'\ue01b'
NUMPAD2 = u'\ue01c'
NUMPAD3 = u'\ue01d'
NUMPAD4 = u'\ue01e'
NUMPAD5 = u'\ue01f'
NUMPAD6 = u'\ue020'
NUMPAD7 = u'\ue021'
NUMPAD8 = u'\ue022'
NUMPAD9 = u'\ue023'
PAGE_DOWN = u'\ue00f'
PAGE_UP = u'\ue00e'
PAUSE = u'\ue00b'
RETURN = u'\ue006'
RIGHT = u'\ue014'
SEMICOLON = u'\ue018'
SEPARATOR = u'\ue026'
SHIFT = u'\ue008'
SPACE = u'\ue00d'
SUBTRACT = u'\ue027'
TAB = u'\ue004'
UP = u'\ue013'

Locate elements By

class selenium.webdriver.common.by.By

CLASS_NAME = 'class name'
CSS_SELECTOR = 'css selector'
ID = 'id'
LINK_TEXT = 'link text'
NAME = 'name'
PARTIAL_LINK_TEXT = 'partial link text'
TAG_NAME = 'tag name'
XPATH = 'xpath'

Desired Capabilities

class selenium.webdriver.common.desired_capabilities.DesiredCapabilities

#Usage Example:

from selenium import webdriver

selenium_grid_url = “http://198.0.0.1:4444/wd/hub“

# Create a desired capabilities object as a starting point. 
capabilities = DesiredCapabilities.FIREFOX.copy() 
capabilities[‘platform’] = “WINDOWS” 
capabilities[‘version’] = “10# Instantiate an instance of Remote WebDriver with the desired capabilities. 
driver = webdriver.Remote(desired_capabilities=capabilities,command_executor=selenium_grid_url)
#Note: Always use ‘.copy()’ on the DesiredCapabilities object to avoid the side effects of altering the Global class instance.

ANDROID = {'platform': 'ANDROID', 'browserName': 'android', 'version': '', 'javascriptEnabled': True}
CHROME = {'platform': 'ANY', 'browserName': 'chrome', 'version': '', 'javascriptEnabled': True}
EDGE = {'platform': 'WINDOWS', 'browserName': 'MicrosoftEdge', 'version': ''}
FIREFOX = {'platform': 'ANY', 'browserName': 'firefox', 'version': '', 'marionette': False, 'javascriptEnabled': True}
HTMLUNIT = {'platform': 'ANY', 'browserName': 'htmlunit', 'version': ''}
HTMLUNITWITHJS = {'platform': 'ANY', 'browserName': 'htmlunit', 'version': 'firefox', 'javascriptEnabled': True}
INTERNETEXPLORER = {'platform': 'WINDOWS', 'browserName': 'internet explorer', 'version': '', 'javascriptEnabled': True}
IPAD = {'platform': 'MAC', 'browserName': 'iPad', 'version': '', 'javascriptEnabled': True}
IPHONE = {'platform': 'MAC', 'browserName': 'iPhone', 'version': '', 'javascriptEnabled': True}
OPERA = {'platform': 'ANY', 'browserName': 'opera', 'version': '', 'javascriptEnabled': True}
PHANTOMJS = {'platform': 'ANY', 'browserName': 'phantomjs', 'version': '', 'javascriptEnabled': True}
SAFARI = {'platform': 'MAC', 'browserName': 'safari', 'version': '', 'javascriptEnabled': True}

Utilities

selenium.webdriver.common.utils.find_connectable_ip(hostport=None)

selenium.webdriver.common.utils.free_port()

 

selenium.webdriver.common.utils.is_connectable(porthost='localhost')

selenium.webdriver.common.utils.is_url_connectable(port)

 

selenium.webdriver.common.utils.join_host_port(hostport)

selenium.webdriver.common.utils.keys_to_typing(value)

 

Firefox WebDriver

class selenium.webdriver.firefox.webdriver.WebDriver(firefox_profile=Nonefirefox_binary=None,timeout=30capabilities=Noneproxy=Noneexecutable_path='wires'firefox_options=None)

Chrome WebDriver

class selenium.webdriver.chrome.webdriver.WebDriver(executable_path='chromedriver'port=0,chrome_options=Noneservice_args=Nonedesired_capabilities=Noneservice_log_path=None)

Remote WebDriver

class selenium.webdriver.remote.webdriver.WebDriver(command_executor='http://127.0.0.1:4444/wd/hub',desired_capabilities=Nonebrowser_profile=Noneproxy=Nonekeep_alive=Falsefile_detector=None)

WebElement

class selenium.webdriver.remote.webelement.WebElement(parentid_w3c=False)

Represents a DOM element.

Generally, all interesting operations that interact with a document will be performed through this interface.

All method calls will do a freshness check to ensure that the element reference is still valid. This essentially determines whether or not the element is still attached to the DOM. If this test fails, then an StaleElementReferenceException is thrown, and all future calls to this instance will fail.

UI Support

class selenium.webdriver.support.select.Select(webelement)

class selenium.webdriver.support.wait.WebDriverWait(drivertimeoutpoll_frequency=0.5,ignored_exceptions=None)

Color Support

class selenium.webdriver.support.color.Color(redgreenbluealpha=1)

from selenium.webdriver.support.color import Color

print(Color.from_string('#00ff33').rgba)
print(Color.from_string('rgb(1, 255, 3)').hex)
print(Color.from_string('blue').rgba)

Expected conditions Support

class selenium.webdriver.support.expected_conditions.alert_is_present

class selenium.webdriver.support.expected_conditions.element_located_selection_state_to_be(locator,is_selected)

class selenium.webdriver.support.expected_conditions.element_located_to_be_selected(locator)

class selenium.webdriver.support.expected_conditions.element_selection_state_to_be(element,is_selected)

class selenium.webdriver.support.expected_conditions.element_to_be_clickable(locator)

class selenium.webdriver.support.expected_conditions.element_to_be_selected(element)

class selenium.webdriver.support.expected_conditions.frame_to_be_available_and_switch_to_it(locator)

class selenium.webdriver.support.expected_conditions.invisibility_of_element_located(locator)

class selenium.webdriver.support.expected_conditions.presence_of_all_elements_located(locator)

class selenium.webdriver.support.expected_conditions.presence_of_element_located(locator)

class selenium.webdriver.support.expected_conditions.text_to_be_present_in_element(locator,text_)

class selenium.webdriver.support.expected_conditions.staleness_of(element)

class selenium.webdriver.support.expected_conditions.text_to_be_present_in_element(locator,text_)

class selenium.webdriver.support.expected_conditions.text_to_be_present_in_element_value(locator,text_)

class selenium.webdriver.support.expected_conditions.title_contains(title)

class selenium.webdriver.support.expected_conditions.title_is(title)

class selenium.webdriver.support.expected_conditions.visibility_of(element)

class selenium.webdriver.support.expected_conditions.visibility_of_any_elements_located(locator)

class selenium.webdriver.support.expected_conditions.visibility_of_element_located(locator)

 

http://selenium-python.readthedocs.io/index.html

posted @ 2016-09-08 16:06  sufei  阅读(1634)  评论(0编辑  收藏  举报