標籤:test tail 開始 cin 滑鼠 空格 toc 查看 any
1:通過ID
2:通過Name
3:通過xpath
3.1 通過xpath的絕對路徑,從最外層開始找
例如/html/body/div[1]/div[4]/div[2]/div/form/span[1]/input
3.2 通過xpath的相對路徑
3.2.1 找元素本身
xpath同樣可以利用元素自身的屬性, 例如 xpath=//*[@id=‘kw1‘]
//表示某個層級下,*表示某個標籤名,@id=kw1表示這個元素有個id等於kw1
也可以指定標籤名, 例如 xpath=//input[@id=‘kw1]
元素本身可以利用的屬性不局限於id或者name,例如: xpath=//input[@type=‘text‘]
xpath=input[@autocomplete=‘off‘]
3.2.2 通過元素的上級找到元素
Xpath=//span[@id=‘aa‘]/span/input
3.2.3 布爾值寫法
通過布爾值用多個屬性確定一個元素,例如 xpath=//span[@id=‘aa‘ and @name=‘bb‘]
3.2.4 contains用法 [contains(@attribute, string)] 如: 使用xpath的匹配功能,//input[contains(@id,‘nt‘)]
Xpath的相關所有知識:http://www.w3school.com.cn/xpath/xpath_syntax.asp xpath表示方法:
username=driver.find_element_by_xpath("//form[input/@name=‘username‘]")(form有input子項目,該子項目有name屬性)username=driver.find_element_by_xpath("//form[@id=‘loginForm‘]/input[1]")
username=driver.find_element_by_xpath("//input[@name=‘username‘]")
clear_button=driver.find_element_by_xpath("//input[@name=‘continue‘][@type=‘button‘]") (input元素有name以及type兩個屬性)
xpath知識相關網站:http://www.w3schools.com/https://www.w3.org/TR/xpath/http://www.zvon.org/comp/r/tut-XPath_1.html 用於查看xpath的外掛程式:
- XPath Checker - suggests XPath and can be used to test XPath results.
- Firebug - XPath suggestions are just one of the many powerful features of this very useful add-on.
- XPath Helper - for Google Chrome
4: 通過css path
4.1Css path的知識: http://www.w3school.com.cn/cssref/css_selectors.asphttp://saucelabs.com/resources/articles/selenium-tips-css-selectors
4.2Css選取器知識【適用於chrome->console】 Css選取器布爾值寫法: and-> "[alt=‘no column layout‘]|[src=‘/Images/nocol.png‘]" - 選擇alt屬性為‘no column layout‘以及src屬性為/Image/nocol.png 的元素。
| 選取器 |
例子 |
例子描述 |
CSS |
| .class |
.intro |
選擇 class="intro" 的所有元素。 |
1 |
| #id |
#firstname |
選擇 id="firstname" 的所有元素。 |
1 |
| * |
* |
選擇所有元素。 |
2 |
| element |
p |
選擇所有 <p> 元素。 |
1 |
| element,element |
div,p |
選擇所有 <div> 元素和所有 <p> 元素。 |
1 |
| element element |
div p |
選擇 <div> 元素內部的所有 <p> 元素。 |
1 |
| element>element |
div>p |
選擇父元素為 <div> 元素的所有 <p> 元素。 |
2 |
| element+element |
div+p |
選擇緊接在 <div> 元素之後的所有 <p> 元素。 |
2 |
| [attribute] |
[target] |
選擇帶有 target 屬性所有元素。 |
2 |
| [attribute=value] |
[target=_blank] |
選擇 target="_blank" 的所有元素。 |
2 |
| [attribute~=value] |
[title~=flower] |
選擇 title 屬性包含單詞 "flower" 的所有元素。 |
2 |
| [attribute|=value] |
[lang|=en] |
選擇 lang 屬性值以 "en" 開頭的所有元素。 |
2 |
| :link |
a:link |
選擇所有未被訪問的連結。 |
1 |
| :visited |
a:visited |
選擇所有已被訪問的連結。 |
1 |
| :active |
a:active |
選擇活動連結。 |
1 |
| :hover |
a:hover |
選擇滑鼠指標位於其上的連結。 |
1 |
| :focus |
input:focus |
選擇獲得焦點的 input 元素。 |
2 |
| :first-letter |
p:first-letter |
選擇每個 <p> 元素的首字母。 |
1 |
| :first-line |
p:first-line |
選擇每個 <p> 元素的首行。 |
1 |
| :first-child |
p:first-child |
選擇屬於父元素的第一個子項目的每個 <p> 元素。 |
2 |
| :before |
p:before |
在每個 <p> 元素的內容之前插入內容。 |
2 |
| :after |
p:after |
在每個 <p> 元素的內容之後插入內容。 |
2 |
| :lang(language) |
p:lang(it) |
選擇帶有以 "it" 開頭的 lang 屬性值的每個 <p> 元素。 |
2 |
| element1~element2 |
p~ul |
選擇前面有 <p> 元素的每個 <ul> 元素。 |
3 |
| [attribute^=value] |
a[src^="https"] |
選擇其 src 屬性值以 "https" 開頭的每個 <a> 元素。 |
3 |
| [attribute$=value] |
a[src$=".pdf"] |
選擇其 src 屬性以 ".pdf" 結尾的所有 <a> 元素。 |
3 |
| [attribute*=value] |
a[src*="abc"] |
選擇其 src 屬性中包含 "abc" 子串的每個 <a> 元素。 |
3 |
| :first-of-type |
p:first-of-type |
選擇屬於其父元素的首個 <p> 元素的每個 <p> 元素。 |
3 |
| :last-of-type |
p:last-of-type |
選擇屬於其父元素的最後 <p> 元素的每個 <p> 元素。 |
3 |
| :only-of-type |
p:only-of-type |
選擇屬於其父元素唯一的 <p> 元素的每個 <p> 元素。 |
3 |
| :only-child |
p:only-child |
選擇屬於其父元素的唯一子項目的每個 <p> 元素。 |
3 |
| :nth-child(n) |
p:nth-child(2) |
選擇屬於其父元素的第二個子項目的每個 <p> 元素。 |
3 |
| :nth-last-child(n) |
p:nth-last-child(2) |
同上,從最後一個子項目開始計數。 |
3 |
| :nth-of-type(n) |
p:nth-of-type(2) |
選擇屬於其父元素第二個 <p> 元素的每個 <p> 元素。 |
3 |
| :nth-last-of-type(n) |
p:nth-last-of-type(2) |
同上,但是從最後一個子項目開始計數。 |
3 |
| :last-child |
p:last-child |
選擇屬於其父元素最後一個子項目每個 <p> 元素。 |
3 |
| :root |
:root |
選擇文檔的根項目。 |
3 |
| :empty |
p:empty |
選擇沒有子項目的每個 <p> 元素(包括文本節點)。 |
3 |
| :target |
#news:target |
選擇當前活動的 #news 元素。 |
3 |
| :enabled |
input:enabled |
選擇每個啟用的 <input> 元素。 |
3 |
| :disabled |
input:disabled |
選擇每個禁用的 <input> 元素 |
3 |
| :checked |
input:checked |
選擇每個被選中的 <input> 元素。 |
3 |
| :not(selector) |
:not(p) |
選擇非 <p> 元素的每個元素。 |
3 |
| ::selection |
::selection |
選擇被使用者選取的元素部分。 |
3 |
4.2 Css在console中和在Rebotframework中的區別:
-在rebotframework中: css=i[class^=‘x-icon‘]
-在consonle中:$(‘i[class^="x-icon"]‘)
4.3 * 通過jquery選擇
如: jquery=.active [title="Remove"]
http://www.w3school.com.cn/jquery/jquery_selectors.asp
4.4Css選取器知識【適用於selenium文法】
|
* |
通用元素選取器,匹配任何元素 |
|
E |
標籤選取器,匹配所有使用E標籤的元素 |
|
.info |
class選取器,匹配所有class屬性中包含info的元素 |
|
#footer |
id選取器,匹配所有id屬性等於footer的元素 |
|
E,F |
多元素選取器,同時匹配所有E元素或F元素,E和F之間用逗號分隔 |
|
E F |
後代元素選取器,匹配所有屬於E元素後代的F元素,E和F之間用空格分隔 |
|
E > F |
Robotframework中的元素locator[xpath和css]