自动化测试之定位元素的多种方式

1.xpath

2.css_selector

3.name

4.ID

5.1通过F12中的console,输入$('元素的位置')来确认优化元素css seletor的位置:

div  :class="notice-box" 定位格式 .notice-box

有些类中间只有空格,在console中找不到的可以使用.代替查找试试

p标签 :p:nth-child(3) 没有点

input(上传文件): '.up-id-item:nth-child(1) [type="file"]')

5.2 通过F12中的console,输入$x('元素的位置')来确认优化元素xpath的位置:

http://www.cnblogs.com/unknows/p/7684331.html

1、id  获取id 的属性值

2、starts-with 顾名思义,匹配一个属性开始位置的关键字  -- 模糊定位

3、contains 匹配一个属性值中包含的字符串  -- 模糊定位

4、text()  函数文本定位

5、last()  函数位置定位

1、XPATH使用方法
使用XPATH有如下几种方法定位元素(相比CSS选择器,方法稍微多一点):
a、通过绝对路径定位元素(不推荐!)
WebElement ele = driver.findElement(By.xpath("html/body/div/form/input"));
b、通过相对路径定位元素
WebElement ele = driver.findElement(By.xpath("//input"));
c、使用索引定位元素
WebElement ele = driver.findElement(By.xpath("//input[4]"));
d、使用XPATH及属性值定位元素
WebElement ele = driver.findElement(By.xpath("//input[@id='fuck']"));
//其他方法(看字面意思应该能理解吧)
WebElement ele = driver.findElement(By.xpath("//input[@type='submit'][@name='fuck']"));
WebElement ele = driver.findElement(By.xpath("//input[@type='submit' and @name='fuck']"));
WebElement ele = driver.findElement(By.xpath("//input[@type='submit' or @name='fuck']"));
e、使用XPATH及属性名称定位元素
元素属性类型:@id 、@name、@type、@class、@tittle
//查找所有input标签中含有type属性的元素 WebElement ele = driver.findElement(By.xpath("//input[@type]")); f、部分属性值匹配 WebElement ele = driver.findElement(By.xpath("//input[start-with(@id,'fuck')]"));//匹配id以fuck开头的元素,id='fuckyou' WebElement ele = driver.findElement(By.xpath("//input[ends-with(@id,'fuck')]"));//匹配id以fuck结尾的元素,id='youfuck' WebElement ele = driver.findElement(By.xpath("//input[contains(@id,'fuck')]"));//匹配id中含有fuck的元素,id='youfuckyou' g、使用任意值来匹配属性及元素 WebElement ele = driver.findElement(By.xpath("//input[@*='fuck']"));//匹配所有input元素中含有属性的值为fuck的元素 元素定位总结

6.在页面往往有滚动的情况

.这个时候我们可以先让页面直接跳到元素出现的位置,然后就可以操作了。同样需要借助JS去实现。 

元素聚焦:

target = driver.find_element_by_xxxx()
driver.execute_script("arguments[0].scrollIntoView();", target)

 



猜你喜欢

转载自blog.csdn.net/lx5090110/article/details/79467458
今日推荐