One 、 location
The old version is used
driver.find_element_by_xxx()
The new version is
from selenium.webdriver.common.by import By
driver.find_element(By.XXX, “selector”)
If you match more than one , Returns the first match .
If it doesn't match , Throw out NoSuchElementException abnormal ( Report errors ).
What is mainly written here is the new version
1、 By element id Attribute to locate the element
ele = driver.find_element(By.ID, "IamID") # The type is WebElement class
2、 By element name Attribute to locate the element
driver.find_element(By.NAME, "first").send_keys(" By element name Attribute to locate the element ")
3、 By element class Attribute to locate the element
driver.find_element(By.CLASS_NAME, 'poem').send_keys(" By element class Attribute to locate the element ")
4、 Locate the element by the tag name of the tag
print (driver.find_element(By.TAG_NAME, 'form').text)
5、 according to Hyperlink label To locate the element... Based on the text content in the
print (driver.find_element(By.LINK_TEXT, ' A round in the sky ,').text)
6、 according to Hyperlink label Medium Part of the text content to locate the element
print (driver.find_element(By.PARTIAL_LINK_TEXT, " Round ").text)
7、 according to css Selector to locate the element
print (driver.find_element(By.CSS_SELECTOR, '#tianshang').text)
8、 according to xpath Expression to locate the element
print (driver.find_element(By.XPATH, '//*[@id="tianshang"]').text)
Example
Two 、 operation
1、send_keys function : Parameters are the characters to be entered
driver.find_element(By.CSS_SELECTOR, '#tianshang').send_key("admin")
2、clear function : Empty data , No arguments
driver.find_element(By.CSS_SELECTOR, '#tianshang').clear()
3、click function : Click on the element , No arguments
driver.find_element(By.CSS_SELECTOR, '#tianshang').click()
4、set_window_size() Set the size of the browser
5、back() Control the browser back
6、forward() Control browser progress
7、refresh() Refresh current page
8、submit() For submitting forms
9、get_attribute(name) Get element attribute value
10、is_displayed() Set whether the element is visible to the user
11、size Returns the size of the element
12、text Get the text of the element