【Web自動化測試——代碼篇二】條條大路找元素

∮定位單個元素

打車去目的地須要告訴司機地名或者周圍標誌建築==,那麼咱們要讓機器去運行Web,就須要告訴Web元素對應的特徵。
接下來~~~算了,仍是以咱們萬能的度娘爲例ο(=•ω<=)ρ⌒☆
圖片.pngcss

例子 屬性 屬性值 定位代碼
Java Python Ruby
搜索框 id kw - find_element_by_id("kw") find_element(:id,'kw')
class s_ipt - find_element_by_class_name("s_ipt") find_element(:class,'s_ipt')
name wd - find_element_by_name("wd") find_element(:name,'wd')
tag input - find_element_by_tag_name("input") find_element(:tag_name,'input')
文字連接 標籤a連接 hao123 - find_element_by_link_text("hao123") find_element(:link,'hao123')
標籤a連接 hao123 - find_element_by_partial_link_text("hao") find_element(:partial_link_text,'hao')
XPath id kw - find_element_by_xpath("//*[@id='kw']") find_element(:xpath,"//*[@id='kw']")
name wd - find_element_by_xpath("//*[@name='wd']") find_element(:xpath,"//*[@name='wd']")
class s_ipt - find_element_by_xpath("//*[@class='s_ipt']") find_element(:xpath,"//*[@class='s_ipt']")
maxlength 100 - find_element_by_xpath("//*[@maxlength='100']") find_element(:xpath,"//*[@maxlength='100']")
autocomplete off - find_element_by_xpath("//*[@autocpmplete='off']") find_element(:xpath,"//*[@autocpmplete='off']")
type submit - find_element_by_xpath("//*[@type='submit']") find_element(:xpath,"//*[@type='submit']")
CSS id kw - find_element_by_css_selector("#kw") find_element(:css,'#kw')
name wd - find_element_by_css_selector("[name='wd']") find_element(:css,"[name='wd']")
class s_ipt - find_element_by_css_selector(".s_ipt") find_element(:css,'.s_ipt')
autocomplete off - find_element_by_css_selector("[autocomplete=off]") find_element(:css,"[autocomplete=off]")
type submit - find_element_by_css_selector("[type='submit']") find_element(:css,"[type='submit']")
By id kw findElement(By.id("kw")) find_element(By.ID,"kw") -
name wd findElement(By.name("wd")) find_element(By.NAME,"wd") -
class s_ipt findElement(By.className("s_ipt")) find_element(By.CLASS_NAME,"s_ipt") -
html input findElement(By.tagName("input")) find_element(By.TAG_NAME,"input") -
標籤a連接 hao123 findElement(By.linkText("hao123")) find_element(By.LINK_TEXT,"hao123") -
標籤a連接 hao123 findElement(By.partialLinkText("hao")) find_element(By.PARTIAL_LINK_TEXT,"hao") -
XPath //*[@class='s_ipt'] findElement(By.xpath("//*[@class='s_ipt']")) find_element(By.XPATH,"//*[@class='s_ipt']") -
CSS span[@class='bg s_btn_wr']/input[@id='su'] findElement(By.cssSelector("span.bg s_btn_wr>input#su")) find_element(By.CSS_SELECTOR,"span.bg s_btn_wr>input#su") -
↑ 以上部分將經過某個元素的特徵來定位元素的(若有遺漏或差錯各位可自行補充修改)
要是像那種沒啥特點的「孩子」,咱們仍是經過它們的父輩來找到它們
↓ 「個人祖宗是body!」( ఠൠఠ )ノ「啪~哪來的熊孩子?我仍是html呢~」
XPath /html/body/div[1]/div[1]/div/div[1]/div/form/span[1]/input         /*經過絕對路徑找到百度輸入框*/
/html/body/div[1]/div[1]/div/div[1]/div/form/span[2]/input         /*經過絕對路徑找到百度搜索按鈕*/
//span[@class='bg s_ipt_wr']/input                                              /*經過層級與屬性結合找到百度輸入框*/
//form[@id='form']/span[2]/input                                               /*經過層級與屬性結合找到百度搜索按鈕*/
//input[@id='kw' and @class='su']/span/input                           /*經過邏輯運算符獲取元素,經過某個元素多個特徵找到元素*/
CSS form#form>span>input#kw(CSS不過多給出案例,想要了解更多可自行去查找資料(/ω\*)……… (/ω•\*))
( ̄y▽ ̄)╭ Ohohoho.....雖然獲取元素的方法有不少,可是較經常使用的仍是XPath。獲取XPath的途徑有倆種:
☛  無敵的我,大家都是渣渣_____(ˉ▽ ̄~) 切~~
☛  有那個時間不如多擼根肉串,舊版Firefox的FireBug(新版Firefox自帶獲取xpath),Ctrl C/Ctrl V一下(o゜▽゜)o☆

**代碼時間 **

     實踐出真知。列了這麼多方法,仍是有必要檢測一下是否有效,雖然沒法所有驗證,可是仍是要往下看往下看往下看☟☟☟html

Java

圖片.png

Python

圖片.png

Ruby

圖片.png

圖片.gif

 

∰定位一組元素

      定義一組元素的方法與定位單個元素的方法相似,惟一的區別在單詞"element"是否加了"s"。元素組的通常操做場景:web

  • 批量操做元素(如全選)
  • 選擇一組元素中所須要的部分(如全選後,取消不須要的部分)
  Java Python Ruby
id driver.findElements(By.id,) find_elements_by_id find_elements(:id,)
name driver.findElements(By.name,) find_elements_by_name find_elements(:name,)
class driver.findElements(By.className,) find_elements_by_class_name find_elements(:class,)
tag driver.findElements(By.tagName,) find_elements_by_tag_name find_elements(:tag_name,)
link driver.findElements(By.linkText,) find_elements_by_link_text find_elements(:link,)
partial_link driver.findElements(By.partialLinkText,) find_elements_by_partial_link_text find_elements(:partial_link_text,)
xpath driver.findElements(By.xpath,) find_elements_by_xpath find_elements(:xpath,)
css driver.findElements(By.cssSelector,) find_elements_by_css_selector find_elements(:css,)
在這裏只簡簡單單羅列出了8大種定位一組元素的方法,但實際上能夠參考定位單個元素的方法上進行擴展,在這就不展示完整了(記得加's'!!!)

有話要說

    自動化不只適用於web端,也能夠適用於手機端,雖然說它們之間調用的包呀,或者說是定位元素方法不太同樣,可是它們定位元素的介質是同樣的,若是要是你寫的代碼定位不到元素,那就要檢查你的元素介質是不是對的,尤爲是用絕對路徑定位的時候呦ヾ( ̄▽ ̄)ByeBye瀏覽器

 

【上一篇】【Web自動化測試——代碼篇一】簡單的Web自動化腳本測試

【下一篇】【Web自動化測試——代碼篇三】經常使用方法——控制瀏覽器spa

相關文章
相關標籤/搜索