selenium python by a simple operation and positioning elements

Simple browser operating

#
import webdriver module # to create a driver object that specifies the Chrome browser driver = webdriver.Chrome () # window is maximized driver.maximize_window () # access Baidu driver.get ( " http://baidu.com " ) driver.get ( " http://sina.com " ) # Back driver.back () # forward driver.forward () # refresh driver.refresh () # Close driver.close () # close the session, close your browser , a close chromedriver driver.quit ()

 

selenium eight kinds of targeting

6 kinds of features come by a single element (id, calss_name, tag_name, name, link_text (2))

Combinations of various features and elements come relationships (xpath, css)

1.id positioning: unique

 find_element_by_id()

2.name Positioning: not unique

find_element_by_name()

find_elements_by_name()

3.class Positioning: not unique

find_element_by_class()

4.tag_name Positioning: not unique

find_element_by_tag_name () # singular, the DOM page, to the first matching element

find_elements_by_tag_name () # plural, returns a list of elements webElement objects, all matching elements

5. Text Match: / exact match / partial match

find_element_by_link_text()

find_element_by_partial_link_text()

 

Xpath positioning:

1. By positioning yourself:

Syntax: // tag name [@ attribute name = value]

E.g:

// * [@ id = "mCon"] / span # * matches all elements

//*[@id="kw"]

2. locating via text:

Syntax: // tag name [text () = "value"]

E.g:

// h1 [(text () = "on 20")] # exact match

// [contains (text (), "of 20")] # matching section h1, comprising

3. Level positioning:

If the element is found in two or more identical elements, then by their different parent parent or parent to locate

/ Absolute positioning, can only write single slash child can not skip writing

// relative positioning, you can write double slash child, child's children, and so on (recommended)

for example:

l enter the account number

//div//input[@name="account"]

// div [@ class = "padding-cont pt-login"] // input [@ placeholder = "E-mail / account / phone number"]

l password

//div[@class="padding-cont pt-login"]//input[@name="pass"]

//div[@class="padding-cont pt-login"]//input[@type="password"]

l Remember Login

// div [@ class = "padding-cont pt-login"] // a [text () = "Remember Login"]

//div[@class="padding-cont pt-login"]//a[@class="auto-login fl"]

l forgot password

//div[@class="padding-cont pt-login"]//a[@class="forget fr"]

// div [@ class = "padding-cont pt-login"] // a [text () = "Forgot Password?"]

l login button

//div[@class="padding-cont pt-login"]//a[@class="btn-btn"]

//div[@class="padding-cont pt-login"]//a[text()="登录"]

 Xpath axis positioning:

Meaning: to locate elements with the same level directory, called axis positioning

Axis operation:

ancestor: ancestor node, including the parent node

parent: parent

preceding-sibling: the current element node labels of all previous siblings

following-sibling: all siblings after the current element node label

preceding: all current node before the element node label (HTML page order)

following: All current node (HTML page order) after the element node label

Axis positioning syntax:

/ Label Name :: axis name [@ attribute name = value]

Example: Example: // div // table // td // preceding :: td

Scenario:

Page is displayed as a table style data column, by a combination of elements you need to locate

xpath axis expansion information: https://www.w3school.com.cn/xpath/xpath_axes.asp

 

Guess you like

Origin www.cnblogs.com/xingyunqiu/p/11495267.html