Selenum八种常用定位

  1. ID定位

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

  2. name定位

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

  3. class_name定位

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

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

  4. link_text定位

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

  5. 部分链接定位

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

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

  6. html标签名定位

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

  7. Xpath定位

# 绝对路径定位元素
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定位

#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')

 

posted @ 2022-10-24 18:57  夜渐凉  阅读(70)  评论(0)    收藏  举报