The positioning of eight elements of web automation testing

Premise: The element or attribute to be positioned must be unique.

id,name,class_name,xpath,css,tag_name,link_text,partial_link_text

Case: Baidu, you will know (baidu.com)

id positioning:

driver.find_element(By.ID, "kw").send_keys("")

name positioning:

driver.find_element(By.NAME, "wd").send_keys("")

link_text positioning (link text):

driver.find_element(By.LINK_TEXT, "新闻").click()

partial_link_text positioning (partial link text):

driver.find_element(By.PARTIAL_LINK_TEXT, "新闻").click()

xpath location:

Absolute path: /

copy the full xpath

/html/body/div[1]/div[2]/div[5]/div[1]/div/form/span[1]/input

relative path://

Press Ctrl+F to enable search

//input

1. Relative path + index positioning

//form/span[1]/input

2. Relative path + attribute positioning

//input[@autocomplete="off"]

3. Relative path + wildcard positioning

//[@*="off]

//*[@id="kw"]

Copying xpath often makes mistakes, not a panacea

4. Relative path + partial attribute value positioning

Starts with: //input[starts-with(@autocomplete,'of')]

//*[starts-with(@autocomplete,'of')]

ends with: //input[substring(@autocomplete,2)='ff']

Contains: //*[contains(@autocomplete,'ff')]

5. Relative path + text positioning

//span[text()='Search by image']

CSS positioning: not commonly used

Guess you like

Origin blog.csdn.net/xiaoxiaoTeddy/article/details/124136659