selenium python bindings 元素定位

1. 輔助 Firepathcss

Firefox是全部作前端的必不可少的瀏覽器由於firebug的頁面元素顯示很清晰。用selenium 去定位元素的時候Firefox還有一個很是友好的工具就是firepath。下載firepath,點擊頁面元素選擇firepath選項,就會給出一種找到元素的路徑。前端

能夠選擇的方式有xPath、sizzleJS、css三種方法。python

a.sizzle沒有用過,因爲過於麻煩還refactor過一個job改掉sizzle的定位方式。瀏覽器

b.xPath確定會找到這個元素且沒有遇到重複的現象,可是這個路徑可能會過長。工具

c.css則會因前端頁面的寫法不一樣而不必定能找到對應的。網站

因此這是個參考的方式,若是無明確的id或者class能夠先用firepath 的xPath定位下而後化繁爲簡比較不容易出錯。spa

 

定位的方式和寫法不少,不一一都再舉例子,參考下selenium python bindings的網站,而後找幾個例子逆向推出若是能搞清楚對應的頁面結構就完事兒了~給些示例供之後參考orm

 

2. xPathip

textArea_script = driver.find_element_by_xpath("//div[@class='CodeMirror']/div/textarea") element

textArea_script = driver.find_element_by_xpath("//*[@class='CodeMirror']/div/textarea")

textArea_script = driver.find_element_by_xpath("//textarea[contains(text(), 'text')]")

textArea_script = driver.find_element_by_xpath("//*[contains(text(), 'text')]")

這四種其實都能找到同一個元素

 

next = x("//input[@value = '下一步']")

edit_next = x("//input[@name = 'detail_subject_submit']")

 

3. class&id

最簡單的定位方法,只須要知道class用. id用#的區別就能夠了(依賴本身寫的方法)~日常仍是用find_element_by_class/id 就能夠

login_form = driver.find_element_by_id('loginForm')

content = driver.find_element_by_class('content')

 

4. 其餘

還有不少關於Link text或者tag等等的定位方法,使用頻度較低,必須使用的時候再找官網例子便可~

相關文章
相關標籤/搜索