日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

Python3 Selenium自动化web测试 == 第三节 常用WebDriver API使用示例上(24个API)

發布時間:2025/3/8 python 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Python3 Selenium自动化web测试 == 第三节 常用WebDriver API使用示例上(24个API) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

前置步驟:


?

  安裝selenium,chrome驅動,Python3.6

?

學習目的:


?

?  常見API的使用

?

涉及的API:


?

step1: 訪問一個網址

step2: 網頁的前進和后退

step3: 刷新當前頁面

step4: 瀏覽器窗口最大化

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

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

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 unittestclass WebdriverAPI(unittest.TestCase):def setUp(self):# 每個用例都執行,在單個用例運行前執行#打開瀏覽器self.driver = webdriver.Chrome()def tearDown(self):#每個用例都執行,在單個用例運行后執行#退出瀏覽器 self.driver.quit()def test_visitURL(self):#測試步驟passif __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屬性值

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

?

step8: 獲取頁面HTML源代碼

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

?

step9: 獲取當前頁面的URL

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

?

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

#獲取與切換瀏覽器窗口句柄 self.driver.get(firstUrl)#獲取當前窗口的句柄firstHander = self.driver.current_window_handle#打印獲取的第一個窗口句柄print(firstHander)#輸入框輸入檢索內容:seleniumself.driver.find_element_by_id('kw').send_keys('selenium3')#單擊搜索按鈕self.driver.find_element_by_id('su').click()#設置等待時間3stime.sleep(3)#單擊需要點擊的網頁鏈接self.driver.find_element_by_partial_link_text('Python+Selenium3最新配置 - CSDN博客').click()time.sleep(3)#獲取所有的句柄allHander = self.driver.window_handlesprint('當前窗口句柄:'+ 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 ActionChainsaction_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 Selectselect_element = Select(self.driver.find_element_by_name('cars'))current_options = select_element.optionscurrent_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 Selectselect_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 Selectfrom selenium.webdriver.common.keys import Keysself.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

?

轉載于:https://www.cnblogs.com/wuzhiming/p/8860087.html

總結

以上是生活随笔為你收集整理的Python3 Selenium自动化web测试 == 第三节 常用WebDriver API使用示例上(24个API)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。