Selenum eight common positioning (case analysis)

Table of contents

1. ID positioning

2. name positioning

3. class_name positioning

5. Partial link positioning

6. html tag name positioning

7. Xpath positioning

 8. CSS positioning


Selenium is a highly respected tool. It has rich functions that allow us to interact with web pages and perform various tasks, which can provide great convenience for test engineers and developers.

To get the most out of Selenium, you need to understand how to properly position elements on a web page.

Next, I will take you to discuss eight commonly used positioning methods in Selenium, and through case analysis, help you better understand how to use them in automated testing and web crawlers.

1. ID positioning

driver.find_element_by_name("username").send_keys("admin")

2. name positioning

driver.find_element_by_name("username").send_keys("admin")

3. class_name positioning

# 可以根据class属性值来查找一个或者一组显示效果相同的页面元素

driver.find_element_by_class_name("spread").send_keys("admin")

driver.find_element_by_link_text("baidu 搜索").click()

5. Partial link positioning

# 使用此方法定位页面链接只需要模糊匹配链接文字即可,常用一匹配页面链接文字不定期发生少量变化的情况

driver.find_element_by_partial_link_text("baidu").click()

6. html tag name positioning

# HTML标签名称的定位方式主要用于匹配多个页面元素的情况,将查找到的网页元素对象计数、遍历、修改属性等操作
driver.find_element_by_tag_name("input").send_keys("123")

7. Xpath positioning

# 绝对路径定位元素
driver.find_element_by_xpath("/html/body/div/input['@value='查询']")
# 相对路径定位元素
driver.find_element_by_xpath("//input['@value='查询']")
# 索引号定位元素
driver.find_element_by_xpath("//input[2]")
# 使用页面元素的属性值定位元素
driver.find_element_by_xpath('//img[@alt="div1-img1"]')
# 模糊属性值定位元素
driver.find_element_by_xpath('//img[contains(@alt,"img")]')

 8. CSS positioning

#CSS:层叠样式表,主要是用于描述页面元素的展现和样式的定义

#1.使用绝对路径来定位元素
driver.find_element_by_css_selector('html>body>div>input[value="查询"]')
#2.使用相对路径来定位元素
driver.find_element_by_css_selector('input[value="查询"]')
#3.使用class名称来定位元素
driver.find_element_by_css_selector('input.spreed')
#4使用ID属性值来定位元素
driver.find_element_by_css_selector('input#div1input')
#5.使用页面其他属性值来定位元素
driver.find_element_by_css_selector('img[art="div1-img1"]')
driver.find_element_by_css_selector('img[art="div1-img1"][href="http://www.sogou.com"]')
#6.使用属性值的一部分内容来定位元素
    # ^表示从字符串的开始匹配
    # $表示从字符串的结尾匹配
    # *表示从字符串的模糊匹配
driver.find_element_by_css_selector('a[href^="http://www.so"]')
driver.find_element_by_css_selector('a[href$="gou.com"]')
driver.find_element_by_css_selector('a[href*="so"]')
#7.使用页面元素进行子页面元素的查找
driver.find_element_by_css_selector('div#div1>input#div1input')
driver.find_element_by_css_selector('div input')

The following are relatively good learning tutorial resources that I have collected. Although they are not very valuable, if you happen to need them, you can leave a message in the comment area [777] and just take them away.

Friends who want to get information, please like + comment + collect , three times in a row!

After three consecutive rounds , I will send you private messages one by one in the comment area~

 

Guess you like

Origin blog.csdn.net/m0_70618214/article/details/132900973