Python+selenium之Xpath

1、什么是自动化

通过机器去完成手工测试的过程

2、web自动化测试元素定位方法有哪些?
1、id
2、name
3、class
4、tagname
5、linktext
6、patiallinktext
7、xpath
8、cssselector

3、xpath定位的方法

1、xpath之绝对路径定位

绝对路径:从根目录(/)开始,一级一级找到目标路径

例如:driver.find_element_by_xpath("/html/body/div[1]/div[1]/div/div[1]/div/form/span[1]/input")

driver.find_element_by_xpath("/html/body/div[1]/div[1]/div/div[1]/div/form/span[1]/input").send_keys("自动化测试")

2、相对路径定位

相对路径:从//开始,从选择的元素集去找到目标元素

driver.find_element_by_xpath("//form/span/input").send_keys("自动化测试")


3、通过索引定位

标签[N]

例如:driver.find_element_by_xpath("//form/span[1]/input").send_keys("自动化测试")

4、通过属性定位

1)通过唯一属性值定位

driver.find_element_by_xpath("//input[@id='kw']").send_keys("自动化测试")

2)通过同时匹配多个属性值定位 多个属性值之间用and/or

driver.find_element_by_xpath("//input[@id='kw' and @name='wd']").send_keys("自动化测试")

5、通过部分属性值定位

函数:
1)contains(@属性名,属性值) 模糊匹配属性值

2)substring(@属性,n) n指的是开始截取的下标 截取属性值

3)starts-with(@属性名,属性值) 属性值以什么开头

#通过部分属性值匹配

driver.find_element_by_xpath("//input[contains(@autocomplete,'ff')]").send_keys("自动化测试")

driver.find_element_by_xpath("//input[substring(@class,3)='ipt']").send_keys("自动化测试")

driver.find_element_by_xpath("//input[starts-with(@class,'s_')]").send_keys("自动化测试")

6、通过文本进行定位

函数:text()

#通过文本进行定位

driver.find_element_by_xpath("//div[@id='u1']/a[text()='新闻']").click()


原文地址:https://www.cnblogs.com/Magic_liao/p/13449559.html