Python Selenium 部分API詳細使用方法(一)

*********************************************************************************************css

1、瀏覽器操做

一、瀏覽器最大化html

driver.maximize_window() #將瀏覽器最大化顯示

二、設置瀏覽器寬、高前端

driver.set_window_size(480, 800)#設置瀏覽器寬480、高800顯示

三、控制瀏覽器前進、後退node

driver.back()#瀏覽器後退
driver.forward()#瀏覽器前進

*********************************************************************************************python

2、簡單對象的定位

webdriver 提供了一系列的元素定位方法,經常使用的有如下幾種:web

  •  id
  •  name
  •  class name
  •  tag name
  •  link text
  •  partial link text
  •  xpath
  •  css selector

分別對應python webdriver 中的方法爲:瀏覽器

  • find_element_by_id()
  • find_element_by_name()
  • find_element_by_class_name()
  • find_element_by_tag_name()
  • find_element_by_link_text()
  • find_element_by_partial_link_text()
  • find_element_by_xpath()
  • find_element_by_css_selector()

    一、id 和name 定位佈局

    id 和name 是咱們最經常使用的定位方式,由於大多數元素都有這兩個屬性,並且在對控件的id 和name
    命名時通常使其有意義也會取不一樣的名字。經過這兩個屬性使咱們找一個頁面上的屬性變得至關容易。性能

    好比:學習

    #id=」gs_htif0」
    find_element_by_id("gs_htif0")
    #name=」btnK」
    find_element_by_name("btnK")

    二、tag name 和class name 定位

    好比:

    #<div id="searchform" class="jhp_big" style="margin-top:-2px">
    #<form id="tsf" onsubmit="return name="f" method="GET" action="/search">
    find_element_by_class_name("jhp_big")
    find_element_by_tag_name("div")

    tag name 定位應該是全部定位方式中最不靠譜的一種了,由於在一個頁面中具備相同tag name 的元
    素極其容易出現。

    三、link text 與partial link text 定位

    有時候須要操做的元素是一個文字連接,那麼咱們能夠經過link text 或partial link text 進行元素
    定位。好比:

    #<a href="http://news.baidu.com" name="tj_news">新聞</a>
    #<a href="http://tieba.baidu.com" name="tj_tieba">貼吧</a>
    #<a href="http://zhidao.baidu.com" name="tj_zhidao">一個很長的文字鏈接</a>
    
    #經過link text 定位元素:
    find_element_by_link_text("新聞")
    find_element_by_link_text("貼吧")
    find_element_by_link_text("一個很長的文字鏈接")
    
    #通partial link text 也能夠定位到上面幾個元素:
    find_element_by_partial_link_text("新")
    find_element_by_partial_link_text("吧")
    find_element_by_partial_link_text("一個很長的")

    當一個文字鏈接很長時,咱們能夠只取其中的一部分,只要取的部分能夠惟一標識元素。通常一個頁
    面上不會出現相同的文件連接,經過文字連接來定位元素也是一種簡單有效的定位方式。

    四、XPath 定位

    XPath 是一種在XML 文檔中定位元素的語言。由於HTML 能夠看作XML 的一種實現,因此selenium 用
    戶但是使用這種強大語言在web 應用中定位元素。

    如下面一段html代碼爲例:

    <html class="w3c">
    <body>
         <div class="page-wrap">
         <div id="hd" name="q">
             <form target="_self" action="http://www.so.com/s">
                  <span id="input-container">
                        <input id="input" type="text" x-webkit-speech="" autocomplete="off" suggestwidth="501px" >

    (1)使用絕對路徑定位:

    當咱們所要定位的元素很難找到合適的方式時,均可以通這種絕對路徑的方式位,缺點是當元素在很
    多級目錄下時,咱們不得不要寫很長的路徑,並且這種方式難以閱讀和維護。

    find_element_by_xpath("/html/body/div[2]/form/span/input")

    (2)使用相對路徑定位:

    find_element_by_xpath("//input[@id=’input’]") #經過自身的id 屬性定位
    find_element_by_xpath("//span[@id=’input-container’]/input") #經過上一級目錄的id 屬性定位
    find_element_by_xpath("//div[@id=’hd’]/form/span/input") #經過上三級目錄的id 屬性定位
    find_element_by_xpath("//div[@name=’q’]/form/span/input")#經過上三級目錄的name 屬性定位

    經過上面的例子,咱們能夠看到XPath 的定位方式很是靈活和強大的,XPath 能夠作布爾邏輯運算,例如://div[@id=’hd’ or @name=’q’]。

    固然,它的缺陷也很是明顯:

    一、性能差,定位元素的性能要比其它大多數方式差;

    二、不夠健壯,XPath會隨着頁面元素佈局的改變而改變;

    3. 兼容性很差,在不一樣的瀏覽器下對XPath 的實現是不同的。

     

    下面插播一下xpath的知識:

    (1)路徑表達式:

    表達式 描述
    nodename 選取此節點的全部子節點。
    / 從根節點選取。
    // 從匹配選擇的當前節點選擇文檔中的節點,而不考慮它們的位置。
    . 選取當前節點。
    .. 選取當前節點的父節點。
    @ 選取屬性。

    實例

    在下面的表格中,咱們已列出了一些路徑表達式以及表達式的結果:

    路徑表達式 結果
    bookstore 選取 bookstore 元素的全部子節點。
    /bookstore

    選取根元素 bookstore。

    註釋:假如路徑起始於正斜槓( / ),則此路徑始終表明到某元素的絕對路徑!

    bookstore/book 選取屬於 bookstore 的子元素的全部 book 元素。
    //book 選取全部 book 子元素,而無論它們在文檔中的位置。
    bookstore//book 選擇屬於 bookstore 元素的後代的全部 book 元素,而無論它們位於 bookstore 之下的什麼位置。
    //@lang 選取名爲 lang 的全部屬性。

    實例

    在下面的表格中,咱們列出了帶有謂語的一些路徑表達式,以及表達式的結果:

    路徑表達式 結果
    /bookstore/book[1] 選取屬於 bookstore 子元素的第一個 book 元素。
    /bookstore/book[last()] 選取屬於 bookstore 子元素的最後一個 book 元素。
    /bookstore/book[last()-1] 選取屬於 bookstore 子元素的倒數第二個 book 元素。
    /bookstore/book[position()<3] 選取最前面的兩個屬於 bookstore 元素的子元素的 book 元素。
    //title[@lang] 選取全部擁有名爲 lang 的屬性的 title 元素。
    //title[@lang='eng'] 選取全部 title 元素,且這些元素擁有值爲 eng 的 lang 屬性。
    /bookstore/book[price>35.00] 選取 bookstore 元素的全部 book 元素,且其中的 price 元素的值須大於 35.00。
    /bookstore/book[price>35.00]/title 選取 bookstore 元素中的 book 元素的全部 title 元素,且其中的 price 元素的值須大於 35.00。

    (2)選取未知節點

    XPath 通配符可用來選取未知的 XML 元素。

    通配符 描述
    * 匹配任何元素節點。
    @* 匹配任何屬性節點。
    node() 匹配任何類型的節點。

    實例

    在下面的表格中,咱們列出了一些路徑表達式,以及這些表達式的結果:

    路徑表達式 結果
    /bookstore/* 選取 bookstore 元素的全部子元素。
    //* 選取文檔中的全部元素。
    //title[@*] 選取全部帶有屬性的 title 元素。

     

    五、CSS 定位

    CSS(Cascading Style Sheets)是一種語言,它被用來描述HTML 和XML 文檔的表現。CSS 使用選擇器來爲頁面元素綁定屬性。這些選擇器能夠被selenium 用做另外的定位策略。CSS 能夠比較靈活選擇控件的任意屬性,通常狀況下定位速度要比XPath 快,但對於初學者來講比較難以學習使用,下面咱們就詳細的介紹CSS 的語法與使用:

    例以下面一段代碼:

    <div class="formdiv">
    <form name="fnfn">
    <input name="username" type="text"></input>
    <input name="password" type="text"></input>
    <input name="continue" type="button"></input>
    <input name="cancel" type="button"></input>
    <input value="SYS123456" name="vid" type="text">
    <input value="ks10cf6d6" name="cid" type="text">
    </form>
    <div class="subdiv">
    <ul id="recordlist">
    <p>Heading</p>
    <li>Cat</li>
    <li>Dog</li>
    <li>Car</li>
    <li>Goat</li>
    </ul>
    </div>
    </div>

    經過CSS 語法進行匹配的實例:

    關於自動化的定位問題:
    自動化測試的元素定位一直是困擾自動化測試新手的一個障礙,由於咱們在自動化實施過程當中會碰到
    各式各樣的對象元素。雖然XPath 和CSS 能夠定位到複雜且比較難定位的元素,但相比較用id 和name 來
    說增長了維護成本和學習成本,相比較來講id/name 的定位方式更直觀和可維護,有新的成員加入的自動
    化時也增長了人員的學習成本。因此,測試人員在實施自動化測試時必定要作好溝通,規範前端開發人員
    對元素添加id/name 屬性,或者本身有修改HTML 代碼的權限。

*********************************************************************************************

相關文章
相關標籤/搜索