Python3 Selenium自動化web測試 ==> 第三節 經常使用WebDriver API使用示例上(24個API)

前置步驟:


 

  安裝selenium,chrome驅動,Python3.6css

 

學習目的:


 

   常見API的使用html

 

涉及的API:


 

step1: 訪問一個網址python

step2: 網頁的前進和後退web

step3: 刷新當前頁面chrome

step4: 瀏覽器窗口最大化api

step5: 獲取並設置當前窗口的位置瀏覽器

step6: 獲取並設置當前窗口的大小app

step7: 獲取頁面的title屬性值框架

step8: 獲取頁面HTML源代碼學習

step9: 獲取當前頁面的URL

step10: 獲取與切換瀏覽器窗口句柄

step11:獲取頁面元素的基本信息

step12: 獲取頁面元素的文本內容

step13: 判斷頁面元素是否可見

step14: 判斷頁面元素是否可操做

step15: 獲取頁面元素的屬性

step16: 獲取頁面的CSS屬性值

step17: 狀況輸入框中的內容

step18: 在輸入框中輸入指定的內容

step19: 單擊按鈕

step20: 雙擊某個元素

step21: 操做單選下拉列表

step22: 斷言單選列表選項值

step23: 操做多選的選擇列表

step24: 操做能夠輸入的下拉列表

 

正式步驟:


 

  測試使用的unittest框架代碼以下:

  

# -*-  coding:utf-8 -*-
from selenium import webdriver
import unittest

class WebdriverAPI(unittest.TestCase):
    def setUp(self):
        # 每一個用例都執行,在單個用例運行前執行
        #打開瀏覽器
        self.driver = webdriver.Chrome()

    def tearDown(self):
        #每一個用例都執行,在單個用例運行後執行
        #退出瀏覽器
        self.driver.quit()

    def test_visitURL(self):
        #測試步驟
        pass
    

if __name__ == '__main__':
    unittest.main()

 

 

step1: 訪問一個網址

    def test_visitURL(self):
        url = 'https://www.baidu.com/'
        self.driver.get(url)
        assert self.driver.title == '百度一下,你就知道'
        print(self.driver.title)

 

step2: 網頁的前進和後退

    def test_visitHistoryPage(self):
        firstUrl = 'https://www.baidu.com/'
        secondUrl = 'https://cn.bing.com/'
        #訪問第一個網址
        self.driver.get(firstUrl)
        time.sleep(2)
        #訪問第二個網址
        self.driver.get(secondUrl)
        time.sleep(2)
        #回到百度網址
        self.driver.back()
        time.sleep(2)
        #再回到bing
        self.driver.forward()
        time.sleep(2)

 

step3: 刷新當前頁面

self.driver.refresh()

 

step4: 瀏覽器窗口最大化

        #窗口最大化
        self.driver.maximize_window()

 

step5: 獲取並設置當前窗口的位置

        #獲取瀏覽器位置
        position = self.driver.get_window_position()
        #打印出瀏覽器的座標
        print('當前窗口x軸%s,y軸%s'%(position['x'],position['y']))
        #設置瀏覽器的位置
        self.driver.set_window_position(x = 100,y = 100)
        time.sleep(2)

  

step6: 獲取並設置當前窗口的大小

        #獲取瀏覽器窗口大小
        window_size = self.driver.get_window_size()
        print('current size %s :'%window_size)
        print('寬%s,高%s'%(window_size['width'],window_size['height']))
        #從新設置窗口大小
        self.driver.set_window_size(width=1000,height=1000)

 

step7: 獲取頁面的title屬性值

        #獲取並打印頁面title
        page_title = self.driver.title
        print(page_title)
        #斷言頁面title內容
        self.assertEqual(page_title,'百度一下,你就知道1','頁面屬性值錯誤')

 

step8: 獲取頁面HTML源代碼

        #獲取頁面的HTML源代碼
        page_source = self.driver.page_source
        print(page_source)
        #斷言源碼是否包含關鍵字百度,顯然會有回顯信息打印出來,正確則沒有回顯msg
        self.assertTrue('百度#'in page_source,'不包含')

 

step9: 獲取當前頁面的URL

        #獲取當前頁面url
        self.driver.get(firstUrl)
        correntPageUrl = self.driver.current_url
        print(correntPageUrl)

 

step10: 獲取與切換瀏覽器窗口句柄

        #獲取與切換瀏覽器窗口句柄
        self.driver.get(firstUrl)
        #獲取當前窗口的句柄
        firstHander = self.driver.current_window_handle
        #打印獲取的第一個窗口句柄
        print(firstHander)
        #輸入框輸入檢索內容:selenium
        self.driver.find_element_by_id('kw').send_keys('selenium3')
        #單擊搜索按鈕
        self.driver.find_element_by_id('su').click()
        #設置等待時間3s
        time.sleep(3)
        #單擊須要點擊的網頁連接
        self.driver.find_element_by_partial_link_text('Python+Selenium3最新配置 - CSDN博客').click()
        time.sleep(3)
        #獲取全部的句柄
        allHander = self.driver.window_handles
        print('當前窗口句柄:'+ allHander[-1])
        #獲取全部窗口句柄
        for hander in allHander:
            print(hander)
        #將操做句柄切換到當前窗口
        self.driver.switch_to.window(allHander[-1])
        time.sleep(3)
        #能夠關閉當前窗口,否則關閉的是firstUrl
        self.driver.close()
        time.sleep(3)
        print(firstHander)
        self.driver.switch_to.window(firstHander)
        self.driver.find_element_by_id('kw').clear()
        time.sleep(2)

 

step11:獲取頁面元素的基本信息

        firstUrl = 'https://www.baidu.com/'
        self.driver.get(firstUrl)
        testElement = self.driver.find_element_by_xpath("//a[text()='新聞']")
        print(testElement.tag_name,testElement.size)

 

step12: 獲取頁面元素的文本內容

        testElementText = self.driver.find_element_by_xpath("//a[text()='新聞']")
        print(testElementText.text)

 

 

step13: 判斷頁面元素是否可見

        testElementVisiable = self.driver.find_element_by_xpath("//a[text()='新聞']")
        print(testElementVisiable.is_displayed())

 

 

step14: 判斷頁面元素是否可操做,是否已選

        testElementVisiable = self.driver.find_element_by_xpath("//a[text()='新聞']")
        print(testElementVisiable.is_displayed())
        #判斷頁面元素是否可操做
        print(testElementVisiable.is_enabled())

 

 

step15: 獲取頁面元素的屬性

        #獲取頁面元素屬性
        attribution = testElementVisiable.get_attribute('class')
        print(attribution)
        attribution1 = testElementVisiable.get_attribute('name')
        print(attribution1)
        attribution2 = testElementVisiable.get_attribute('text')
        print(attribution2)

 

 

step16: 獲取頁面的CSS屬性值

        css_property = self.driver.find_element_by_xpath("//a[text()='新聞']")
        print(css_property.value_of_css_property('height'))
        print(css_property.value_of_css_property('width'))
        print(css_property.value_of_css_property('font-size'))

 

 

step17: 清空輸入框中的內容

        self.driver.find_element_by_id('kw').send_keys('test')
        self.driver.find_element_by_id('kw').clear()

 

 

step18: 在輸入框中輸入指定的內容

self.driver.find_element_by_id('kw').send_keys('test')

 

 

step19: 單擊按鈕

self.driver.find_element_by_partial_link_text('Python+Selenium3最新配置 - CSDN博客').click()

 

 

step20: 雙擊某個元素

        input = self.driver.find_element_by_id('kw')
        input.send_keys('雙擊全選,背景高亮')
        from selenium.webdriver import ActionChains
        action_chains = ActionChains(self.driver)
        #雙擊後,高亮兩個字背景高亮,只是爲了證實雙擊起效了
        action_chains.double_click(input).perform()
        time.sleep(3)

 

 

step21: 操做單選下拉列表

測試用下拉html頁面代碼

<html>
<body>
<form>
<select name="cars">
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="fiat">Fiat</option>
<option value="audi">Audi</option>
</select>
</form>
</body>
</html>

 

測試腳本:

        #操做簡單的下拉列表
        url = 'F:\\python_stack\\python_autotest\\webdriver_api\\xiala.html'
        self.driver.get(url)
        all_options = self.driver.find_elements_by_tag_name('option')
        for option in all_options:
            print(option.text)
            print(option.get_attribute('value'))
            option.click()
            time.sleep(1)

 

 

step22: 斷言單選列表選項值

        url = 'F:\\python_stack\\python_autotest\\webdriver_api\\xiala.html'
        self.driver.get(url)
        from selenium.webdriver.support.ui import Select
        select_element = Select(self.driver.find_element_by_name('cars'))
        current_options = select_element.options
        current_optionsList = []
        #遍歷options,並生成option文本值的列表
        for option in current_options:
            print(option.text)
            current_optionsList.append(option.text)

        print(current_optionsList)
        expect_optionsList = ['Volvo','Saab','Fiat','Audi']
        #斷言兩個列表是否相同
        self.assertListEqual(current_optionsList,expect_optionsList)

 

 

step23: 操做多選的選擇列表

        url = 'F:\\python_stack\\python_autotest\\webdriver_api\\xiala.html'
        self.driver.get(url)
        from selenium.webdriver.support.ui import Select
        select_element = Select(self.driver.find_element_by_name('cars'))
        select_element.select_by_index(0)
        select_element.select_by_visible_text('Fiat')
        select_element.select_by_value('audi')
        time.sleep(3)
        #取消選中的單位
        select_element.deselect_all()
        time.sleep(3)

 

 

step24: 操做能夠輸入的下拉列表

測試用HTML代碼

<!DOCTYPE html>
<html>
<body>
<div style="position: relative;">
<input list="pasta" id = "select">
    <datalist id ="pasta">
        <option>C</option>
        <option>Java</option>
        <option>Python</option>
        <option>C#</option>
        <option>Ruby</option>
    </datalist>
</div>
</body>
</html>

 

測試腳本:

        #帶輸入的下拉列表操做
        url = 'F:\\python_stack\\python_autotest\\webdriver_api\\data.html'
        self.driver.get(url)
        from selenium.webdriver.support.ui import Select
        from selenium.webdriver.common.keys import Keys
        self.driver.find_element_by_id("select").clear()
        time.sleep(1)
        #輸入的同時向下按箭頭
        self.driver.find_element_by_id("select").send_keys("Java",Keys.ARROW_DOWN)
        time.sleep(2)
        self.driver.find_element_by_id("select").send_keys(Keys.ENTER)
        time.sleep(2)

 

 

難點分析:


 

 API看似簡單,實際的簡單應用中,仍是花了不少時間去實際運行,眼高手低很差

 

學習總結:


 

 還有24個經常使用API,下班後繼續

 

參考資料:

參考英文官方資料:http://selenium-python.readthedocs.io/locating-elements.html

相關文章
相關標籤/搜索