Xpath CSS positioning in Selenium

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

  1. input[type^='p'] Description: elements whose type attribute starts with the letter p
  2. input[type$='d'] Description: elements whose type attribute ends with the letter d
  3. 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

  1. clear() clear text
  2. send_keys() analog input
  3. click() Click the element

WebDriver operating browser commonly used methods

  1. driver.maximize_window() Maximize --> Simulate browser maximize button
  2. driver.set_window_size(100,100) browser size --> set browser width and height (pixels)
  3. driver,set_window_position(300,200) browser position --> set browser position
  4. driver.back() Back --> Simulate the browser back button
  5. driver.forward() Forward --> Simulate the browser forward button
  6. driver.refresh() Refresh --> Simulate browser F5 refresh
  7. driver.close() Close --> Simulate the browser close button (close a single window)
  8. driver.quit() Close --> Close all windows started by WebDriver

Other common methods of WebDriver

  1. size returns the element size
  2. text Get the text of the element
  3. title Get the page title
  4. current_url Get the current page URL
  5. get_attribute("xxx") to obtain the attribute value; xxx: the attribute to be obtained
  6. is_display() determines whether the element is visible
  7. 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

  1. context_click() right click --> This method simulates the effect of a right mouse click
  2. double_click() Double-click --> This method simulates the double-label double-click effect
  3. drag_and_drop() Drag --> This method simulates the double-label drag effect
  4. move_to_element() hover --> This method simulates the effect of mouse hover
  5. perform() Execute --> This method is used to execute all the above mouse methods

Key point analysis of code implementation

  1. 导包:from selenium.webdriver.common.action_chains import ActionChains
  2. Instantiate the ActionChains object: Action=ActionChains(driver)
  3. Call the right-click method: element=Action.context_click(username)
  4. Implementation: element.perform()

Drag key point analysis

  1. Source element socure=driver.find_element_by_id(xxx)
  2. Target element target=driver.find_element_by_id(xxx)
  3. Call the method Action.drag_and_drop(source, target).perform()

Common keyboard operations
from selenium.webdriver.common.keys import Keys

  1. send_keys(Keys.BACK_SPACE)删除键(BackSpace)
  2. send_keys(Keys.SPACE)Space key(Space)
  3. send_keys(Keys.TAB) Tab key (Tab)
  4. send_keys(Keys.ESCAPE) Back key (Esc)
  5. send_keys(Keys.ENTER) Enter key (Enter)
  6. send_keys(Keys.CONTROL,‘a’) 全选(Ctrl+A)
  7. send_keys(Keys.CONTROL,'c') copy (Ctrl+C)

Guess you like

Origin blog.csdn.net/qq_34237321/article/details/97885927
Recommended