例:<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') -----這兩個方式效果是同樣的