Xpath positioning method
driver.find_element_by_xpath()
Use element attributes
Description: Quickly locate elements and use unique attributes of elements;
example: //*[@id='userA']
Combination of level and attribute
Description: The element you are looking for has no attributes, but its parent has;
example: //*[@id='p1']/input
Combination of attribute and logic
Description: Solve the problem of the same attribute name between elements
Example: //*[@id='telA' and @class='telA']
Xpath-extended
// [text()="xxx"] elements whose text content is xxx
// [starts-with(@attribute,'xxx')] elements whose attributes start with xxx
//*[contains(@attribute, 'Sxxx')] elements with xxx in the attribute
CSS positioning method
driver.find_element_by_css_selector()
id selector
Description: select according to the element id attribute
Format: #id such as: #userA <select all elements whose id attribute value is userA>
class selector
Description: select according to the class attribute of the element
Format: .class such as: .telA <select all elements whose class attribute value is telA>
Element selector
Description: Select according to the tag name of the
element Format: element such as: input <select all input elements>
Attribute selector
Description: Choose according to the attribute name and value of the element
Format: [attribute=value] Such as: [type="password"] <Select all the values whose type attribute value is password>
Hierarchical selector
Description: Choose according to the parent-child relationship of the
element. Format: element>element such as: p>input <return all input elements under all p elements>
Tip:> You can use spaces instead of such as: p input or p [type=' password']
CSS extension
- input[type^='p'] Description: elements whose type attribute starts with the letter p
- input[type$='d'] Description: elements whose type attribute ends with the letter d
- input[type*='w'] Description: elements whose type attribute contains the letter w
Targeting | xpath | css |
---|---|---|
Element name | //input | input |
id positioning | //input[@id=‘userA’] | #userA |
class | /*[@class=‘telA’] | .screen |
Attributes | //*[text()=“xxx”] | input[type$=‘d’] |
Attributes | //*[starts-with(@attribute,‘xxx’)] | input[type^=‘p’] |
Attributes | //*contains(@attribute,‘xxx’)] | input[type*=‘w’] |
By class method
from selenium.webdriver.common.by import By
Method: find_element( By.ID ,"userA")
Remarks: Two parameters are required. The first parameter is the type of positioning provided by By, and the second parameter is the specific method of positioning.
Example:
1. driver.find_element(By. CSS_SELECTOR,'#emailA').send_keys("[email protected]")
2. driver.find_element(By.XPATH,'//*[@id=“emailA”]').send_keys('asdasd@asd. com')
3. driver.find_element( By.ID ,“userA”).send_keys(“admin”)
4. driver.find_element( By.NAME ,“passwordA”).send_keys(“123456”)
5. driver.find_element (By.CLASS_NAME,"telA").send_keys("18611111111")
6. driver.find_element(By.TAG_NAME,'input').send_keys("123")
7. driver.find_element(By.LINK_TEXT,'Visit Sina Website').click()
8. driver.find_element(By.PARTIAL_LINK_TEXT,'Access').click()
Common operation methods of elements
- clear() clear text
- send_keys() analog input
- click() Click the element
WebDriver operating browser commonly used methods
- driver.maximize_window() Maximize --> Simulate browser maximize button
- driver.set_window_size(100,100) browser size --> set browser width and height (pixels)
- driver,set_window_position(300,200) browser position --> set browser position
- driver.back() Back --> Simulate the browser back button
- driver.forward() Forward --> Simulate the browser forward button
- driver.refresh() Refresh --> Simulate browser F5 refresh
- driver.close() Close --> Simulate the browser close button (close a single window)
- driver.quit() Close --> Close all windows started by WebDriver
Other common methods of WebDriver
- size returns the element size
- text Get the text of the element
- title Get the page title
- current_url Get the current page URL
- get_attribute("xxx") to obtain the attribute value; xxx: the attribute to be obtained
- is_display() determines whether the element is visible
- is_enabled() Determine whether the element is available
Tips:
1. size, text, title, current_url: are attributes, without parentheses when called; such as: xxx.size
2. title, current_url: use the browser instantiated object to call directly; such as: driver.title
WebDriver operation mouse method
Description: encapsulate the method of operating the mouse in the ActionChains class in WebDriver
- context_click() right click --> This method simulates the effect of a right mouse click
- double_click() Double-click --> This method simulates the double-label double-click effect
- drag_and_drop() Drag --> This method simulates the double-label drag effect
- move_to_element() hover --> This method simulates the effect of mouse hover
- perform() Execute --> This method is used to execute all the above mouse methods
Key point analysis of code implementation
- 导包:from selenium.webdriver.common.action_chains import ActionChains
- Instantiate the ActionChains object: Action=ActionChains(driver)
- Call the right-click method: element=Action.context_click(username)
- Implementation: element.perform()
Drag key point analysis
- Source element socure=driver.find_element_by_id(xxx)
- Target element target=driver.find_element_by_id(xxx)
- Call the method Action.drag_and_drop(source, target).perform()
Common keyboard operations
from selenium.webdriver.common.keys import Keys
- send_keys(Keys.BACK_SPACE)删除键(BackSpace)
- send_keys(Keys.SPACE)Space key(Space)
- send_keys(Keys.TAB) Tab key (Tab)
- send_keys(Keys.ESCAPE) Back key (Esc)
- send_keys(Keys.ENTER) Enter key (Enter)
- send_keys(Keys.CONTROL,‘a’) 全选(Ctrl+A)
- send_keys(Keys.CONTROL,'c') copy (Ctrl+C)