Selenium- core WebDriver

1, identification and location page elements

Page elements from the tag pairs
tag has various attributes
can have between label text data
labels have a hierarchy

Provides eight elements Webdriver positioning method corresponding to a method in python is:

Id Location: find_element_by_id ()
the Name Location: find_element_by_name ()
the Tag Location: find_element_by_tag_name ()
Class Location: find_element_by_class_name ()
LINK_TEXT Location: find_element_by_link_text ()
the Xpath Location: find_element_by_xpath ()
Css_selector Location: find_element_by_css_selector
LINK_TEXT Location: find_element_by_link_text ()
the Partial Link Location: find_element_by_partial_link_text ()
Partial link targeting is a supplement to link positioning, and some text links long, this time we can take part of the text positioning, as long as it can uniquely identify the can. For example find_element_by_partial_link_text ( "Home")

 2, Xpath positioning

XPath location language elements in an XML document. But selenium users to use this powerful language targeting elements in a web application.
Xpath positioning  find_element_by_xpath ()
positioning Baidu input box and Baidu search button with Xpath
1, absolute path to locate (the main label name hierarchy)
Find_element_by_xpath ( "/ HTML / body / div / div [2] / div / div / div / form / span / INPUT ")
Find_element_by_xpath (" / HTML / body / div / div [2] / div / div / div / form / span [2] / INPUT ")

2, the positioning element attributes using
Find_element_by_xpath ( "// input [@ ID = 'kW']")
Find_element_by_xpath ( "// input [@ ID = 'SU']")
// input an input current page tab, [ @ id = 'kw'] represents the element id is kw
Find_element_by_xpath ( "// * [@ name = 'WD']")
Find_element_by_xpath ( "// * [@ class = 's_ipt']")
If you do not specify a label name may be (*) are replaced with asterisks
in any attribute element can be used, as long as a unique identification
Find_element_by_xpath ( "// input [@ tupe = 'submit']")

3, in conjunction with the attribute hierarchy (parent element below the search box of the sub-tab)
find_element_by_xpath ( "// span [@ class = 'BG s_ipt_wr quickdelete-wrap'] / INPUT")
. 4, logical operators (not a property The only to distinguish one element, it can be connected to a plurality of logical operators to find the attributes)
Find_element_by_xpath ( "// INPUT [@ ID = '@ kw'and class =' s_ipt ']")
And the condition is satisfied by both a

3, Css positioning

# Class by positioning
driver.find_element_by_css_selector (. "S_btn")
# id by positioning
driver.find_element_by_css_selector ( "# kw")
# tag name by positioning
driver.find_element_by_css_selector ( "the INPUT")
# by positioning the label hierarchy (parent element is selected span all input elements)
driver.find_element_by_css_selector ( "span> input")
# by property positioned
driver.find_element_by_css_selector ( "[type = 'Submit]")
driver.find_element_by_css_selector ( "[name =' kW]")
# Integrated positioning
driver .find_element_by_css_selector ( "form.fm> span> input.s_ipt")
driver.find_element_by_css_selector ( "form # form> span> the INPUT # kw")

 4, the browser basic operations

Controlling the size of the browser window #
# browser 400 high, 200 pixels wide
driver.set_window_size (400,200)
# browser maximized
driver.maximize_window ()
# browser back control proceeds
driver.back ()
driver.forward ()
# Analog browser refresh
driver.refresh ()
# close the current window
driver.close ()
# exit drive close all windows
driver.quit ()

5, mouse

# Mouse operation related method is encapsulated in ActionChains class
# ActionChains class introduced
from selenium.webdriver Import ActionChains
# context_click (): Right-click
# double_click (): Double-
# drag_and_drop (): Drag
# move_to_element (): Hover
# perform (): class execution behavior ActionChains stored
above = driver.find_element_by_link_text ( "settings")
# of positioning elements to perform mouse hovering operation
ActionChains (driver) .move_to_element (above) .perform ()

6, keyboard

# Send_keys (Keys.BACK_SPACE): Delete key
# send_keys (Keys.SPACE): Spacebar
# send_keys (Keys.TAB): Tabulator
# send_keys (Keys.ESCAPE): Backspace key
# send_keys (Keys.ENTER): ENTER
# send_keys (Keys.CONTROL, 'a' ): Select
# send_keys (Keys.CONTROL, 'c' ): copy
# send_keys (Keys.CONTROL, 'x' ): cut
# send_keys (Keys.CONTROL , 'v'): adhesive
# send_keys (Keys.F1): keyboard Fl
. driver.find_element_by_id ( "kW") send_keys ( "ceshi")
. driver.find_element_by_id ( "kW") send_keys (Keys.CONTROL, 'A ')

7, the waiting time

Reality: When the browser loads the page elements on the page and will not be loaded simultaneously, thus increasing the difficulty of locating elements (ElementNotVisibleException error).
WebDriver provides two types of waiting for
the display to wait (WebDriverWait class): the WebDriver waiting for some condition to continue the establishment, otherwise reach thrown beyond the maximum time a long time anomalies (TimeoutException)
implicit wait (implicitly_wait): through a certain when the long wait on a page element loading is complete. If you exceed the length of time set, the element has not yet been loaded, an exception is thrown NoSuchElementException

Display wait (WebDriverWait class): the WebDriver wait to continue if a condition exists, or else thrown in to achieve maximum long time beyond the time when abnormal (TimeoutException)
specific format WebDriver (Driver, timeout, poll_frequency = 0.5, ignored_exceptions = None)
• driver: driver browser
• timeout: longest timeout default in seconds
• Poll_frequency: (step) detection time interval, the default is 0.5s
• Ignored_exceptions: abnormality information after a timeout, by default throws NoSuchElementException
• WebDriverWait generally mate with an until () and until_not () method.

Implicit wait (implicitly_wait): done by a certain length of time waiting for pages to load elements. If you exceed the length of time set, the element has not yet been loaded, an exception is thrown NoSuchElementException
implicitly_wait in seconds, set 10 seconds, not a fixed waiting time does not affect the speed of execution of the script. Secondly waits for all elements on the page. If the first 6s found it to continue, more than 10 seconds did not find an exception is thrown.

8, file upload

File upload operation is also more common, upload operation webdriver does not provide a corresponding method.
Upload process generally open a window system window, from the window, select Add a local file (question: how to operate the local window window). Upload a file; just locate the upload button, through send_keys add local file path on it. Absolute and relative paths are available.
Upload File #
# If the element is uploaded <input type = "file" name = "file"> like this, it is possible to direct the path send_keys element
driver.get ( "file: /// D: / BestTesting / pyStudy /%E8%A2%AB%E6%B5%8Bdemo/uploadfile.html. ")
driver.find_element_by_name (" File ") send_keys (." C: \\ the Users \ Administrator \ Pictures \ katyperry.png ")

9, form processing

frame / iframe nesting, webdriver only to position elements on a page.
switch_to.frame () methods of switching.

# 表单切换
driver.get("file:///D:/BestTesting/pyStudy/%E8%A2%AB%E6%B5%8Bdemo/iframe.html")
iframe = driver.find_element_by_id("iname")
driver.switch_to.frame(iframe)
driver.find_element_by_id("sb_form_q").send_keys("selenium")
driver.find_element_by_id("sb_form_go").click()
# 跳到iframe上一级driver.switch_to.parent_frame()
driver.switch_to.parent_frame()
# 跳到最外层的页面driver.switch_to.default_content()
# driver.switch_to.default_content()
driver.find_element_by_link_text("baidu").click()

10, warning box

Alert processing javascript generated in the WebDriver, confirm, prompt very simple, method is to use switch_to.alert particular method.
= driver.switch_to.alert Alert
alert.accept () # equivalent to clicking OK
alert.dismiss () # equivalent to clicking Cancel
alert.text # get the pop-up text box
driver.switch_to_alert ()
driver.switch_to.alert
 selenium3.7.0 in switch_to_alert () method, although being "painted bars", but can be used normally!

11, drop-down box process

# Import select class
from the Select Import selenium.webdriver.support.select
# select_by_value () value of the positioning value by the drop-down box option
# select_by_visible_text () value of the positioning options via drop-down text
# select_by_index () to locate the drop-down Index. 0,1
# targeting dropdown box
driver.get ( "file: /// D: / BestTesting / pyStudy / measured Demo / select.html")
SEL = driver.find_element_by_name ( "SELECT")
the Select (SEL) .select_by_value ( "1")
# the Select (SEL) .select_by_visible_text ( "Apple")
# the Select (SEL) .select_by_index ( "1")

12, window screenshot

# Screen shot
driver.get ( "http://www.baidu.com")
# interception of the current window, specify the image save location
driver.save_screenshot ( "C: \\ Users \\ Administrator \\ Pictures \\ test.png" )

13, operating cookies

 14, call JS scroll bar

Guess you like

Origin www.cnblogs.com/katyhudson/p/12463118.html