例:<input id="kw" name="wd" class="s_ipt" value="" maxlength="255" autocomplete="off">css
element = driver.find_element_by_id("kw") html
例:<input name="cheese" type="text"/>post
cheese = driver.find_element_by_name("cheese")spa
3、经过classname查找htm
例:<div class="cheese"><span>Cheddar</span></div>索引
cheeses = driver.find_elements_by_class_name("cheese")ip
4、经过标签名查找ci
例:<iframe src="..."></iframe>element
frame = driver.find_element_by_tag_name("iframe")文档
5、经过连接文本查找
例:<a href="http://www.baidu.com">转到百度</a>
ele = driver.find_element_by_link_text("转到百度")
注:有的时候,连接的文本很长,咱们甚至只须要经过部分文本去找到该连接元素
只要这个连接文本是惟一的就行
6、经过CSS选择器查找
eles = driver.find_element_by_css_selector('#choose_car option')
基本用法:
方式 |
用法 |
举例 |
描述 |
根据class查找 |
.class |
.intro |
查找class=”intro”元素 |
根据id查找 |
#id |
#firstname |
查找id=”firstname”的元素 |
根据标签名查找 |
tagname |
div |
查找<div>元素 |
根据属性查找 |
[attribute] |
[target] |
查找具备”target”属性的元素 |
[attribute=value] |
[target=_blank] |
查找包含target=”_blank”的元素 |
|
[attribute^=value] |
[href^=”https”] |
查找包含href属性,且该属性的值以”https”开头的元素 |
|
[attribute$=value] |
[href$=”.pdf”] |
查找包含href属性,且该属性的值以”.pdf”结尾的元素 |
|
[attribute*=value] |
[href*=”abc”] |
查找包含href属性,且该属性的值包含“abc”的元素 |
高级用法:
用法 |
举例 |
描述 |
后代元素选择器 |
div p |
选择全部在<div>里面的<p> |
子元素选择器 |
div>p |
选择全部<div>的<p>子元素 |
组选择器,同时选择多个元素 |
<div>,<p> |
同时选择全部的<div>元素和<p>元素 |
相邻兄弟元素 |
<div>+<p> |
选择全部<div>后面紧跟的<p>元素 |
兄弟元素 |
<div>~<p> |
选择全部<div>元素后面的<P>元素(不必定要紧跟) |
:empty |
p:empty |
选择没有子节点(包括文本)的<p>元素 |
:first-child |
p:first-child |
选择全部是 父元素第一个元素的<p>元素 |
:first-of-type |
p:first-of-type |
选择全部是 父元素第一个<p>元素的<p>元素 |
:last-child |
p:last-child |
选择全部是其父元素最后一个元素的<p>元素 |
:last-of-type |
p:last-of-type |
选择全部是其父元素最后一个<p>元素的<p>元素 |
:nth-child(n) |
p:nth-child(2) |
选择全部是其父元素第二个元素的<p>元素 |
:nth-of-type(n) |
p:nth-of-type(2) |
选择全部是其父元素的第二个<p>元素的<p>元素 |
:nth-last-child(n) |
p:nth-last-child(2) |
选择全部是其父元素倒数第二个元素的<p>元素 |
:nth-last-of-type(n) |
p:nth-last-of-type |
选择全部是其父元素倒数第二个<p>元素的<p>元素 |
:only-child |
p:only-child |
选择全部是其父元素惟一一个子元素的<p>元素 |
:only-of-type |
p:only-of-type |
选择全部是其父元素惟一一个<P>子元素的<p>元素 |
:not(selector) |
:not(p) |
选择全部不是<p>元素的元素 |
7、经过Xpath查找
eles = food.find_elements_by_xpath('./p')
基本用法:
用法 |
举例 |
描述 |
绝对路径(/) |
/html/body/div/p |
表示html文档中的p节点,xpath路径表示了元素的位置 |
相对路径(//) |
//footer//p |
表示footer元素中全部的后代P类型元素 |
混合使用 |
//footer/div/p |
表示html文档中footer元素下的div元素下的p元素 |
根据属性选择 |
//*[@style] |
表示选择HTML文档下全部包含style属性的元素 |
//p[@spec='len2'] |
选择全部具备spec 属性且值为“len2” 的p元素 |
|
//a[contains(@href,'51job.com')]
|
选择全部具备href属性,且该属性的值包含“51job.com”的a元素 |
|
//a[starts-with(@href,'http://big5.51job')] |
表示属性href以“http://big5.51job”开头 |
高级用法:
方式 |
用法 |
举例 |
描述 |
根据文本定位元素
|
所有文字 |
//*[text()='花呗套现'] |
文本等于“花呗套现”的元素 |
部分文字 | //*[contains(text(),'花呗') | 文本包含"花呗”的元素 | |
子元素选择器 |
根据下标获取(下标从1开始) |
//div[@id='food']/p[1] |
获取第一个p子元素 |
倒数索引 |
//div[@id='food']/*[last()](倒数第一个) //div[@id='food']/*[last()-1](倒数第二个) //div[@id='food']/*[last()-2](倒数第三个) |
last()表明倒数第一个元素
|
|
postion():表明元素的位置 |
//div[@id='food']/*[position()=2] |
表示第二个元素 |
|
//div[@id='food']/[position()=last()] |
表示最后一个元素 |
||
//div[@id='food']/[position()=last()-2] |
表示倒数第三个元素 |
||
//div[@id='food']/[position()>=last()-2] |
表示最后三个元素 |
||
组选择器 |
用竖线隔开 |
//p | //button |
等价于css中的p, button |
相邻兄弟选择器 |
following-sibling:: |
//*[@id=’food’]/following-sibling::div |
选择id=food节点的相邻兄弟div元素
|
preceding-sibling:: |
//*[@id=’food’]/preceding-sibling::div |
选择id=food节点的前面的兄弟P元素
|
元素的相对定位:
如:
food = driver.find_element_by_id("food")
eles = food.find_elements_by_xpath('./p')
若是不加点 ,eles = food.find_elements_by_xpath('/p') 与 eles = driver.find_elements_by_xpath('/p') -----这两个方式效果是同样的