在使用selenium webdriver进行元素定位时,有8种基本元素定位方法(注意:并不是只有8种,总共来讲,有16种)。
分别介绍以下:
1.name定位
(注意:必须确保name属性值在当前html页面中是惟一的)
页面源码以下:
"<button name="btnK">Google Search</button>"
当咱们要用name属性来定位这个按钮元素并点击它时,代码以下:
driver.find_element_by_name('btnK').click()
2.id定位
(注意:必须确保id属性值在当前html页面中是惟一的)
页面源码以下:
"<button id="gbqfba">Google Search</button>"
当咱们要用id属性来定位这个按钮元素并点击它时,代码以下:
driver.find_element_by_id('gbqfba').click()
3.tagNamed定位
(注意:必须确保标签在当前html页面中是惟一的,标签名在html页面中通常不惟一,因此定位单个元素时不多用,定位一组元素时用)
页面源码以下:
"<button>Google Search</button>"
当咱们要用tagName属性来定位这个按钮元素并点击它时,代码以下:
driver.find_element_by_tag_name('button').click()
4.className定位
(注意:必须确保className属性值在当前html页面中是惟一的)
一:class属性值只有一个
页面源码以下:
"<button class="gbqfba">Google Search</button>"
当咱们要用className属性来定位这个按钮元素并点击它时,代码以下:
driver.find_element_by_class_name('gbqfba').click()
二:class属性值存在多个
注意:使用className来进行元素定位时,有时会碰到一个元素指定了若干个class属性值的“复合样式”的状况
页面源码以下:
<button class ="btn btn_big btn_submit">登陆</button>
这个元素的class有三个不一样的值,咱们只须要选择其中一个值就行了,代码以下:
driver.find_element_by_class_name('btn_submit').click()
5.linkText定位
页面源码以下:
<a href="/intl/en/about.html">个人名字叫作Kevin</a>
当咱们要用linkText属性来定位这个按钮元素并点击它时,代码以下:
driver.find_element_by_link_text('个人名字叫作Kevin').click()
6.partialLinkText定位
(注意:这个方法是上一个方法的扩展。当你不能准确知道超连接上的文本信息或者只想经过一些关键字进行匹配时,可使用这个方法来经过部分连接文字进行匹配。)
页面源码以下:
<a href="/intl/en/about.html">个人名字叫作Kevin</a>
当咱们要用linkText属性来定位这个按钮元素并点击它时,代码以下:
driver.find_element_by_partial_link_text('Kevin').click()
7.xpath定位
(这个方法是很是强大的元素查找方式,使用这种方法几乎能够定位到页面上的任意元素。)
查找页面根元素: //
查找页面上全部的input标签的元素: //input
代码:driver.find_elements_by_xpath('//input[1]') #注意:加个s表明定位多个元素
查找页面上第一个input标签的元素: //input[1]
代码:driver.find_element_by_xpath('//input[1]')
查找页面上标签为form且id为loginForm元素: //form[@id="loginForm"]
代码:driver.find_element_by_xpath('//form[@id="loginForm"]')
查找页面上具备name属性为username的input标签的元素: //input[@name="username"]
代码:driver.find_element_by_xpath('//input[@name="username"]')
查找页面上id为loginForm的form元素下的第一个input元素: //form[@id="loginForm"]/input[1]
代码:driver.find_element_by_xpath('//form[@id="loginForm"]/input[1]')
查找页面具备name属性为contiune而且type属性为button的input元素: //input[@name="continue" and @type="button"]
代码:driver.find_element_by_xpath('///input[@name="continue" and @type="button"]')
查找页面上id为loginForm的form元素下第4个input元素: //form[@id="loginForm"]/input[4]
代码:driver.find_element_by_xpath('//form[@id="loginForm"]/input[4]')
前面讲的都是xpath中基于准确元素属性的定位,其实xpath做为定位神器也能够用于模糊匹配。
1.用contains关键字,定位代码以下:
driver.find_element_by_xpath("//a[contains(@href,‘logout’)]")
2.用start-with关键字,定位代码以下:
driver.find_element_by_xpath("//a[starts-with(@rel,"nofo")]");
3.用Text关键字,定位代码以下:
driver.find_element_by_xpath('//*[text()="退出"]');
4.用ends-with关键字,定位代码以下
driver.find_element_by_xpath("//a[ends-with(@rel,"nofo")]");
8.css定位
css定位元素方式跟xpath比较相似,但执行速度较快,并且各类浏览器对它的支持都至关到位,因此功能也是蛮强大的。
下面是一些常见的cssSelector的定位方式:
定位id为flrs的div元素能够写成:#flrs 注:至关于xpath语法的//div[@id=’flrs’]
定位id为flrs下的a元素,能够写成 #flrs>a 注:至关于xpath语法的//div[@id=’flrs’]/a
定位id为flrs下的href属性值为 /forexample/about.html的元素,能够写成: # flrs > a[href=”/forexample/about.html”]
若是须要指定多个属性值时,能够逐一加在后面,如 #flrs>input[name=”username”][type=”text”]
最后再总结一下,各类方式在选择的时候应该怎么选择:
1.当页面元素有id属性时,尽可能用id来定位。
2.xpath是屠龙刀,css是倚天剑,双剑合璧,才能够天下无敌,因此最好把这两种定位方式都掌握。
3.不少人说xpath定位比css慢,但我的感受速度上没什么区别,多是0.01和0.1的区别吧。