Python+selenium automation eight elements positioning methods and examples


1. The find_element_by_id method in the selenium module cannot be used

Two, Python+selenium automation eight elements positioning method

scenes to be used:

1. Locating by id attribute: driver.find_element(By.ID,"value")
2. Locating by name attribute: driver.find_element(By.NAME,"value")
3. Locating by class attribute: driver.find_element(By .CLASS_NAME,"value")
4. Locating through the tag_name attribute: driver.find_element(By.TAG_NAME,"input")
5. Locating through the link_text attribute: driver.find_element(By.LINK_TEXT,"value")
6. Using the partial_link attribute Locating: driver.find_element(By.PARTIAL_LINK_TEXT,"value")
7. Locating by xpath attribute: find_element_by_xpath()
8. Locating by css attribute: find_element_by_css_selector("css")


1. The find_element_by_id method in the selenium module cannot be used

When I was studying before, I used to watch the videos of Bilibili University for free. Now that it is standardized, the previous learning method will report an error.
For example: the find_element_by_id method in the selenium module cannot be used, and an error will be reported:
AttributeError: 'WebDriver' object has no attribute 'find_element_by_id'
use driver.find_element(By.ID,"value") instead, and import from selenium.webdriver.common. by import By is fine.
Other element positioning methods can be modified in the same way.

Two, Python+selenium automation eight elements positioning method

Usage scenarios:
① Use id and name first (to ensure uniqueness)
② Whether it is a hyperlink or not, use link_text, partial_link_text for hyperlinks, otherwise use css_selector, xpath
③ class_name, tag_name are rarely used, because it is difficult to guarantee that these two are defined as elements or attribute uniqueness

1、通过id属性定位:driver.find_element(By.ID,"value")
driver.find_element(By.ID,"stu_username_hide").send_keys("1234567879")
driver.find_element(By.ID,"stu_password_hide").send_keys("123456")

2、通过name属性定位:driver.find_element(By.NAME,"value")
driver.find_element(By.NAME,"username").send_keys("1234567879")
driver.find_element(By.NAME,"password").send_keys("123456")

3. Position by class attribute: driver.find_element(By.CLASS_NAME,"value")
driver.find_element(By.CLASS_NAME,"Validform_error")

4. Position by tag_name attribute: driver.find_element(By.TAG_NAME,"input")
driver.find_element(By.TAG_NAME,"input")

5. Locate through the link_text attribute: driver.find_element(By.LINK_TEXT,"value")
driver.find_element(By.LINK_TEXT,"login")//click to log in

6. Locating through the partial_link attribute: driver.find_element(By.PARTIAL_LINK_TEXT,"value")
driver.find_element(By.PARTIAL_LINK_TEXT,"Login")//Fuzzy matching links with the word "Login"

7. Locating by xpath attribute: find_element_by_xpath()
driver.find_element(By.XPATH,'/html/body/div/p[1]')//Search according to the path of the element
(1) Absolute path: from the outermost element to the specified element, all paths through the element level, starting with /, from the source tag Start to find the required elements from top to bottom
For example: /html/body/div/p[1]
(2) Relative path: start from the first element that meets the conditions (generally distinguished by attributes), start with //, followed by It must be followed by the tag name or * (* means all tags)
A few examples:
①Relative path + index positioning
//Tag name/Tag name[When there is a tag with the same name, the serial number of the required tag]/Tag nameExample
: //form/ span[1]/input
②Relative path + attribute positioning
Search tags whose attribute under the tag name is attribute name 1='attribute value 1': //Tag name[@attribute name 1='attribute value 1']Example:
// input[@autocomplete='off']
③Relative path + wildcard positioning
Search the entire webpage to find the tag with attribute name 1='attribute value 1' in the tag: //tag name*[@attribute name 1='attribute value 1' ]
Example: //input[@autocomplete='off']
Search the entire webpage and find a tag with an attribute value of 'attribute value 1': //Tag name *[@*='attribute value 1']
Example: / /input[@*='off']
④Relative path + partial attribute value positioning
Search tags whose attribute value starts with x: //*[starts-with(@attribute name,'x')]'x')]'x')]
Example: //*[starts-with(@autocomplete,'of')]
search tags whose attribute value ends with x: //*[substring(@attribute name.a,'x')]
Example: //* [substring(@autocomplete,'ff')]
Search tags whose attribute value contains x: //*[contains(@attribute name,'x')]
example: //*[contains(@autocomplete,'ff') ]
⑤ relative path + text positioning
// label name [text()='x')] or //*[text()="xxx"]
example: //span[text()='search by image') ]

8. Locating through css attributes: find_element_by_css_selector("css")

driver.find_element(By.CSS_SELECTOR, '#id')//Find by id
Tip: css positioning is highly recommended in selenium, because it is faster than XPath positioning; css selector syntax is very powerful.

(1) id selector
Use # to indicate the id attribute, such as: driver.find_element(By.CSS_SELECTOR, '#user')
(2) Class selector
uses . to indicate the class attribute, such as: driver.find_element(By.CSS_SELECTOR, '.tel')
(3) Locating by tag name
Format: element, such as: driver.find_element(By.CSS_SELECTOR, 'input')
(4) Locating by attribute or part of attribute
Format: ["attribute=value"]
in characters ^ indicates matching from the beginning of the string, character * indicates that fuzzy query is required, and character $ indicates matching at the end of the string.
For example:
driver.find_element(By.CSS_SELECTOR,"[autocomplete='off']")//find elements with attribute autocomplete='off'
driver.find_element(By.CSS_SELECTOR,"[autocomplete='off'][name ='user']")//Find elements with attributes autocomplete='off' and name='user'
//Locate
driver.find_elemen(By.CSS_SELECTOR,
driver.find_elemen(By.CSS_SELECTOR,"[autocomplete*='f']")//Find elements whose autocomplete attribute value contains f
driver.find_elemen(By.CSS_SELECTOR,"[autocomplete$='f']")// Find elements whose autocomplete attribute value ends with f
(5) The hierarchical selector
selects according to the parent-child relationship of the element. It is generally difficult to uniquely locate the element in hierarchical positioning, so in general, the hierarchical is combined with id/class/attribute or some attribute values position.
Format: element>element, > can be replaced by spaces
Example:
driver.find_element(By.CSS_SELECTOR,"form>span>input")
driver.find_element(By.CSS_SELECTOR,"form span input")
driver.find_element(By. CSS_SELECTOR,"form>span>input>[type='password']")
driver.find_element(By.CSS_SELECTOR,
"form span input [type='password']") (6) Locating through sibling nodes (locating through the parallel index of elements)
: sibling nodes are under the same parent element, there are multiple identical sub-labels, then these sub-elements It is a brother node.
For example:
there are multiple parallel <a></a> tags
 to locate child elements by index a:nth-child(1)
//Select the first <a> element
driver.find_element(By.CSS_SELECTOR,"div#s-top-left>a:nth-child(1)")
//Select the second <a> element
driver .find_element(By.CSS_SELECTOR,"div#s-top-left>a:nth-child(2)")
//Select the element in the nth <a>
driver.find_element(By.CSS_SELECTOR,"div#s- top-left>a:nth-child(n)")

Supplementary operation

1. send_keys function: simulate input parameters
2. clear function: clear data, no parameter
3. click function: click element, no parameter
#Input "admin" where element id=user
driver.find_element(By.CSS_SELECTOR, '# user').send_key("admin")
#Clear the original content of the element id=user
driver.find_element(By.CSS_SELECTOR, '#user').clear()  
#Click on the place where the element id=user
driver.find_element(By. CSS_SELECTOR, '#user').click()  
4. maximize_window() simulates the browser maximize button
5. set_window_size(100, 100) sets the browser size
6. set_window_position(300,200) sets the browser position
7. back() control Browser back
8, forward() Control the browser forward
9, refresh() Refresh the current page
10, close() Close the current window
11, quit() Close all the windows started by WebDriver

3. Examples

Example one

Requirements: 1) Open the login A.html page, use id to locate, and automatically fill in account number: admin, password: 123456;
        2) Click the login button
        3) Close the browser window after 3 seconds of successful login

步骤:1、导包
from time import sleep
from selenium import webdriver
from selenium.webdriver.common.by import By

2. Instantiate the browser object
driver = webdriver.Chrome()

3. Open the project
driver.get(url)

4. Find the element and fill in
driver.find_element(By.ID,"username").send_keys("admin") driver.find_element(By.ID,"password").send_keys("123456")

5. Click the login button
driver.find_element(By.LINK_TEXT,"login").click()

6. Close the browser after 3 seconds
sleep(3)
driver.quit()

accomplish:

#1. Guide package
from time import sleep
from selenium import webdriver
from selenium.webdriver.common.by import By
 
#2. Instantiate browser object
driver = webdriver.Chrome()
 
#3. Open url
driver.get("index. html")
 
#4. Find operation element
driver.find_element(By.ID,"stu_username_hide").send_keys("1234567879")
driver.find_element(By.ID,"stu_password_hide").send_keys("123456")
 
#5、 close browser
sleep(3)
driver.quit()

Guess you like

Origin blog.csdn.net/qq_30273575/article/details/131823748