Simple operation of the browser # Import the webdriver module # Create driver object specifying Chrome browser driver = () # Maximize the window driver.maximize_window() # Visit Baidu ("") ("") # Back up () # Forward () # Refresh () # Close the window () # Close session, close browser, close chromedriver ()
8 ways to locate selenium
6 find elements by a single feature (id, calss_name, tag_name, name, link_text(2))
Combine various features and relationships to find elements (xpath, css)
Positioning: Unique
find_element_by_id()
Positioning: not unique
find_element_by_name()
find_elements_by_name()
Positioning: not unique
find_element_by_class()
4. tag_name location: not unique
find_element_by_tag_name()# singular, the first element matched in the DOM page
find_elements_by_tag_name()# plural, returns a list with elements as webElement objects, all matching elements
5. Text Match:/Exact Match/Partial Match
find_element_by_link_text()
find_element_by_partial_link_text()
Xpath localization:
1. Position yourself through yourself:
Syntax: // tag name [@attribute name=value]
Example:
//*[@]/span# *Match all elements
//*[@]
2. Orientation through text:
Syntax:// tag name [text()="value"]
Example:
//h1[(text()="Issue 20")]# exact match
//h1[contains(text(), "issue 20")]# partial match, contains
3. Hierarchical positioning:
If the found element has two or more identical elements, then locate them by their different parents or parents of parents
/ Absolute positioning, single slash can only write sub-levels, not skip levels
// Relative positioning, double slashes can write sub-levels, sub-levels of sub-levels, etc. (recommended)
Examples:
l Enter account number //div//input[@name="account"] //div[@class="padding-cont pt-login"]//input[@placeholder="Email/account/cell phone number"] l enter a password //div[@class="padding-cont pt-login"]//input[@name="pass"] //div[@class="padding-cont pt-login"]//input[@type="password"] l Auto-Login Next Time //div[@class="padding-cont pt-login"]//a[text()="Log in automatically next time"] //div[@class="padding-cont pt-login"]//a[@class="auto-login fl"] l forgotten password? //div[@class="padding-cont pt-login"]//a[@class="forget fr"] //div[@class="padding-cont pt-login"]//a[text()="Forgot your password?"] l Login Button //div[@class="padding-cont pt-login"]//a[@class="btn-btn"] //div[@class="padding-cont pt-login"]//a[text()="login"]
Xpath axis positioning:
Meaning: Positioning an element through a sibling directory is called axis positioning.
Axis arithmetic:
ancestor: ancestor nodes, including parent nodes
parent: parent node
preceding-sibling: all sibling nodes before the label of the current element node
following-sibling: all sibling nodes after the node label of the current element
preceding: all nodes before the node tag of the current element (HTML page order)
following: all nodes after the node tag of the current element (HTML page order)
Axis positioning syntax:
/axis name::label name [@attribute name=value]
Example: Example: //div//table//td//preceding::td
Application Scenarios:
The page is displayed as a table-style column of data that requires a combination to position the elements
This is the entire knowledge content of this presentation, thank you for your support.