from seleniumimport webdriver
import unittest
from timeimport sleep
class visitBysou(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Chrome()
def tearDown(self):
self.driver.close()
#訪問某個網(wǎng)址
def test_visitURL(self):
visitURL ='http://www.sogou.com'
self.driver.get(visitURL)
assert self.driver.title.find('搜狗') >=0,'assert error'
#網(wǎng)頁的前進與后退
def test_visitRecenURL(self):
firstVisitURL ='http://www.sogou.com'
secondVisitURl ='http://www.baidu.com'
self.driver.get(firstVisitURL)
self.driver.get(secondVisitURl)
self.driver.back()
self.driver.forward()
#刷新當(dāng)前網(wǎng)頁
def test_refreshCurrentpage(self):
URL ='http://www.sogou.com'
self.driver.get(URL)
self.driver.refresh()
#窗口放大
def test_maxmizeWindow(self):
URL ='http://www.sogou.com'
self.driver.get(URL)
self.driver.maximize_window()
#獲取并設(shè)置當(dāng)前窗口的位置
def test_window_position(self):
URL ='http://www.sogou.com'
self.driver.get(URL)
position =self.driver.get_window_position()
print('##################################')
print(position['x'],position['y'])
print('##############################')
self.driver.set_window_position(y=200,x=400)
sleep(5)
print(self.driver.get_window_position())
#獲取并設(shè)置當(dāng)前窗口的大小
def test_window_size(self):
URL ='http://www.sogou.com'
self.driver.get(URL)
sizeDict =self.driver.get_window_size()
print('#############')
print(sizeDict['width'],sizeDict['height'])
#設(shè)置大小
self.driver.set_window_size(width=200,height=400,windowHandle='current')
print(self.driver.get_window_size())
#獲取打開當(dāng)前頁面的title值
def test_getTitle(self):
URL ='http://www.sogou.com'
self.driver.get(URL)
title =self.driver.title
print('當(dāng)前頁面的title屬性是',title)
self.assertEqual(title,'搜狗搜索引擎 - 上網(wǎng)從搜狗開始','頁面顯示錯誤')
#獲取當(dāng)前頁面的html源碼
def test_getPageSource(self):
URL ='http://www.sogou.com'
self.driver.get(URL)
pagesource =self.driver.page_source
print(pagesource)
self.assertTrue('搜狗' in pagesource,'頁面顯示錯誤')
self.assertIn('搜狗',pagesource,'頁面展示錯誤')
#獲取當(dāng)前頁面的url地址
def test_getUrl(self):
URL ='http://www.sogou.com'
self.driver.get(URL)
Url =self.driver.current_url
print(Url)
self.assertEqual(Url,'https://www.sogou.com/','頁面展示錯誤')
#獲取與切換瀏覽器句柄
def test_operatewindowhandle(self):
URL ='https://www.baidu.com'
self.driver.get(URL)
#獲取當(dāng)前句柄
now_handle =self.driver.current_window_handle
print(now_handle)
self.driver.find_element(by='xpath',value='//*[@id="kw"]').send_keys('w3cshool')
self.driver.find_element_by_xpath('//*[@id="su"]').click()
import time
time.sleep(3)
self.driver.find_element_by_xpath('//*[@id="1"]/h3/a').click()
time.sleep(5)
all_handle =self.driver.window_handles
print(all_handle)
print(all_handle[-1])
for handlein all_handle:
if handle != now_handle:
print('########################')
print(handle)
self.driver.switch_to.window(now_handle)
time.sleep(1)
# self.driver.close()
print(now_handle)
self.driver.switch_to.window(all_handle[-1])
time.sleep(1)
#獲取頁面元素
def test_getBasicinfo(self):
URL ='https://www.baidu.com'
self.driver.get(URL)
sleep(5)
newelement =self.driver.find_element_by_partial_link_text('新聞')
print(newelement.tag_name,newelement.size)
#獲取頁面元素的文本內(nèi)容
def test_getwebElement(self):
URL ='https://www.baidu.com'
self.driver.get(URL)
sleep(4)
newelement =self.driver.find_element_by_partial_link_text('新聞')
a_test = newelement.text
self.assertEqual(a_test,'新聞')
#判斷元素是否可以操作
def test_getwebElementIsEnabled(self):
URL ='F:\gitstorehouse\selenium3.0\webdriverAPI接口\測試頁面\不可見元素.html'
self.driver.get(URL)
sleep(4)
input1 =self.driver.find_element_by_id('input1')
print(input1.is_enabled())
input1 =self.driver.find_element_by_id('input2')
print(input1.is_enabled())
input1 =self.driver.find_element_by_id('input3')
print(input1.is_enabled())
#獲取頁面元素css屬性值
def test_getwebelementcssValue(self):
URL ='http://www.sogou.com'
self.driver.get(URL)
searchBox =self.driver.find_element_by_id('query')
print(searchBox.value_of_css_property('height'))
print(searchBox.value_of_css_property('width'))
print(searchBox.value_of_css_property('font-family'))
# 獲取頁面元素的屬性
def test_getwebelementAttribute(self):
URL='http://www.sogou.com'
self.driver.get(URL)
searchBox =self.driver.find_element_by_id('query')
print(searchBox.get_attribute('name'))
searchBox.send_keys('測試輸入')
print(searchBox.get_attribute('value'))
print(searchBox.get_property('name'))
#雙擊某個元素
def test_dobleclick(self):
URL ='F:\gitstorehouse\selenium3.0\webdriverAPI接口\測試頁面\雙擊.html'
self.driver.get(URL)
inputbox =self.driver.find_element_by_id('input1')
from selenium.webdriver import ActionChains
action_chains = ActionChains(self.driver)
action_chains.double_click(inputbox).perform()
sleep(5)
#遍歷下拉框所有元素并打印選擇項的文本和內(nèi)容
def test_printselectText(self):
URL ='F:\gitstorehouse\selenium3.0\webdriverAPI接口\測試頁面\下拉框.html'
self.driver.get(URL)
select =self.driver.find_element_by_name('fruit')
all_options = select.find_elements_by_tag_name('option')
for optionin all_options:
print(option.text)
#attribute 屬性
print(option.get_attribute('value'))
option.click()
sleep(3)
#定位下拉框的三種方式
def test_operateDropList(self):
URL ='F:\gitstorehouse\selenium3.0\webdriverAPI接口\測試頁面\下拉框.html'
self.driver.get(URL)
from selenium.webdriver.support.uiimport Select
select_element = Select(self.driver.find_element_by_name('fruit'))
#打印默認(rèn)選擇的文本
print(select_element.first_selected_option.text)
#獲取所有的頁面元素對象
all_options = select_element.options
print(len(all_options))
'''
is_enabled() 判斷元素是否存在
is_selected() 判斷元素是否被選中
'''
if all_options[1].is_enabled()and not all_options[1].is_selected():
#方法一:通過序號選擇第二個元素,序號從零開始
select_element.select_by_index(1)
print(select_element.all_selected_options[0].text)
print(type(select_element.all_selected_options))
self.assertEqual(select_element.all_selected_options[0].text,'西瓜')
sleep(3)
#方法二通過顯示文本定位
select_element.select_by_visible_text('橘子')
print(select_element.all_selected_options[0].text)
self.assertEqual(select_element.all_selected_options[0].text,'橘子')
#通過選項value值進行定位
select_element.select_by_value('shanzha')
print(select_element.all_selected_options[0].text)
self.assertEqual(select_element.all_selected_options[0].text,'山楂')
#對下拉框的所有內(nèi)容進行驗證
def test_checkSelectText(self):
URL ='F:\gitstorehouse\selenium3.0\webdriverAPI接口\測試頁面\下拉框.html'
self.driver.get(URL)
from selenium.webdriver.support.uiimport Select
#定位元素
select_element = Select(self.driver.find_element_by_name('fruit'))
#獲取所有下拉元素
all_options = select_element.options
element_list = ['桃子', '西瓜', '橘子', '山楂', '荔枝']
print(type(element_list))
actual_options =map(lambda options:options.text,all_options)
print(type(actual_options))
self.assertListEqual(element_list,list(actual_options))
#操作多選的選擇列表
def test_operMutipleoptionsDropList(self):
URL ='F:\gitstorehouse\selenium3.0\webdriverAPI接口\測試頁面\下拉框.html'
self.driver.get(URL)
from selenium.webdriver.support.uiimport Select
select_element = Select(self.driver.find_element_by_name('fruit'))
#選擇元素纹因,多選
select_element.select_by_index(1)
select_element.select_by_visible_text('山楂')
select_element.select_by_value('lizhi')
for optinsin select_element.all_selected_options:
print(optins.text)
#取消所選元素
select_element.deselect_all()
for optinsin select_element.all_selected_options:
print(optins.text)
sleep(5)
print('###########################################')
#重新選擇元素
select_element.select_by_index(2)
select_element.select_by_visible_text('山楂')
select_element.select_by_value('lizhi')
for optinsin select_element.all_selected_options:
print(optins.text)
sleep(2)
#取消選擇元素
select_element.deselect_by_index(1)
select_element.deselect_by_value('lizhi')
select_element.deselect_by_visible_text('山楂')
sleep(5)
########################################################################################################
#操作可以輸入的下拉列表(輸入同時)
def test_operateMulitileOptionDropList(self):
url ='F:\gitstorehouse\selenium3.0\webdriverAPI接口\測試頁面\可以輸入的下拉列表.html'
self.driver.get(url)
from selenium.webdriver.common.keysimport Keys
self.driver.find_element_by_id('select').clear()
sleep(2)
self.driver.find_element_by_id('select').send_keys('F',Keys.ARROW_DOWN)
sleep(1)
self.driver.find_element_by_id('select').send_keys(Keys.ARROW_DOWN)
sleep(10)
#操作單選框
def test_operateRadio(self):
url ='F:\gitstorehouse\selenium3.0\webdriverAPI接口\測試頁面\操作單選框.html'
self.driver.get(url)
berryRadio =self.driver.find_element_by_xpath('/html/body/form/input[1]')
berryRadio.click()
#斷言‘草莓'
self.assertTrue(berryRadio.is_selected(),'草莓沒選中')
if berryRadio.is_selected():
watermelonRadion =self.driver.find_element_by_xpath('/html/body/form/input[2]')
watermelonRadion.click()
self.assertFalse(berryRadio.is_selected())
#查找所有name屬性是‘fruit’的單選元素對象
radioList =self.driver.find_elements_by_tag_name('input')
#循環(huán)遍歷radioList中的每個但選按鈕锯玛,查找value值為‘orange’的單選框
#如果找到此單選框后挟鸠,發(fā)現(xiàn)未處于選中狀態(tài)辜羊,則調(diào)用click方法
for radoin radioList:
if rado.get_attribute('value')=='orange':
if not rado.is_selected():
rado.click()
sleep(5)
self.assertEqual(rado.get_attribute('value'),'orange')
#操作復(fù)選框
def test_operateCheckBox(self):
url ='F:\gitstorehouse\selenium3.0\webdriverAPI接口\測試頁面\復(fù)選框.html'
self.driver.get(url)
#定位其中一個元素選擇中
berryCheckBox =self.driver.find_element_by_xpath('/html/body/form/input[1]')
berryCheckBox.click()
self.assertTrue(berryCheckBox.is_selected(),'復(fù)選框沒有被選擇')
if berryCheckBox.is_selected():
berryCheckBox.click()
self.assertFalse(berryCheckBox.is_selected())
radioList =self.driver.find_elements_by_tag_name('input')
for radoin radioList:
if not rado.is_selected():
rado.click()
sleep(2)
#斷言頁面源碼中的關(guān)鍵字
def test_assertKeyWord(self):
hurl ='http://www.baidu.com'
self.driver.get(hurl)
self.driver.find_element_by_xpath('//*[@id="kw"]').send_keys('光榮之路')
sleep(5)
assert '光榮之路' in self.driver.page_source,'不存在'
#對當(dāng)前瀏覽器窗口截圖
def test_captureScreenInCurrentWindow(self):
hurl ='http://www.baidu.com'
self.driver.get(hurl)
try:
self.driver.find_element_by_xpath('//*[@id="kw"]').send_keys('光榮之路')
result =self.driver.get_screenshot_as_file('F:\gitstorehouse\selenium3.0\webdriverAPI接口\截圖/1.jpg')
print(result)
except IOError as e:
raise e
#拖拽頁面元素
def test_dragPageElement(self):
url ='http://jqueryui.com/resources/demos/draggable/scroll.html'
self.driver.get(url)
sleep(5)
#定位三個可拖動元素
oneelement =self.driver.find_element_by_xpath('//*[@id="draggable"]')
twoelement =self.driver.find_element_by_xpath('//*[@id="draggable2"]')
threenelement =self.driver.find_element_by_xpath('//*[@id="draggable3"]')
#導(dǎo)入可提供拖拽元素的模塊ActionChain
from selenium.webdriverimport ActionChains
#實例化一個新的ActionChains
action_chains = ActionChains(self.driver)
action_chains.drag_and_drop(oneelement,twoelement).perform()
sleep(2)
#將頁面上的元素向右下角拖動10個元素畦幢,拖動5次
for iin range(5):
action_chains.drag_and_drop_by_offset(threenelement,10,10).perform()
sleep(2)
#模擬鍵盤單個按鍵操作 瀏覽器版本過高
def test_simualteASingleKing(self):
hurl ='http://www.baidu.com'
self.driver.get(hurl)
#導(dǎo)入按鍵模塊
from selenium.webdriver.common.keysimport Keys
from selenium.webdriverimport ActionChains
action_chains = ActionChains(self.driver)
query =self.driver.find_element_by_xpath('//*[@id="kw"]')
sleep(3)
#點擊F12
action_chains.key_down(Keys.F12).key_up(Keys.F12).perform()
# query.send_keys(Keys.F12)
query.send_keys('selenium')
#點擊回車
query.send_keys(Keys.ENTER)
sleep(3)
#tong過內(nèi)建模塊實現(xiàn)全選屈芜,剪切以及粘貼
def test_simuationCombinatKeys(self):
from selenium.webdriverimport ActionChains
from selenium.webdriver.common.keysimport Keys
hurl ='http://www.baidu.com'
self.driver.get(hurl)
input =self.driver.find_element_by_xpath('//*[@id="kw"]')
input.click()
input.send_keys('光榮之路')
sleep(2)
action_chains = ActionChains(self.driver)
action_chains.key_down(Keys.CONTROL).send_keys('a').key_up(Keys .CONTROL).perform()
sleep(2)
action_chains.key_down(Keys.CONTROL).send_keys('v').key_up(Keys.CONTROL).perform()
self.driver.get(hurl)
self.driver.find_element_by_xpath('//*[@id="kw"]').click()
action_chains.key_down(Keys.CONTROL).send_keys('v').key_up(Keys.CONTROL).perform()
sleep(5)
if __name__=='__main__':
unittest.main()
selenium API
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
- 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來橄碾,“玉大人卵沉,你說我怎么就攤上這事颠锉。” “怎么了史汗?”我有些...
- 文/不壞的土叔 我叫張陵琼掠,是天一觀的道長。 經(jīng)常有香客問我停撞,道長瓷蛙,這世上最難降的妖魔是什么? 我笑而不...
- 正文 為了忘掉前任戈毒,我火速辦了婚禮艰猬,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘埋市。我一直安慰自己姥宝,他們只是感情好,可當(dāng)我...
- 文/花漫 我一把揭開白布恐疲。 她就那樣靜靜地躺著腊满,像睡著了一般。 火紅的嫁衣襯著肌膚如雪培己。 梳的紋絲不亂的頭發(fā)上碳蛋,一...
- 文/蒼蘭香墨 我猛地睜開眼敌蜂,長吁一口氣:“原來是場噩夢啊……” “哼箩兽!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起章喉,我...
- 正文 年R本政府宣布幢踏,位于F島的核電站,受9級特大地震影響许师,放射性物質(zhì)發(fā)生泄漏房蝉。R本人自食惡果不足惜,卻給世界環(huán)境...
- 文/蒙蒙 一微渠、第九天 我趴在偏房一處隱蔽的房頂上張望搭幻。 院中可真熱鬧,春花似錦逞盆、人聲如沸檀蹋。這莊子的主人今日做“春日...
- 文/蒼蘭香墨 我抬頭看了看天上的太陽俯逾。三九已至,卻和暖如春舅逸,著一層夾襖步出監(jiān)牢的瞬間桌肴,已是汗流浹背。 一陣腳步聲響...
推薦閱讀更多精彩內(nèi)容
- 1.能夠通過robot對象操作鍵盤上的按鍵完成復(fù)制粘貼拳魁,切換焦點和回車等常用操作。 2.要使用Robot類首先要導(dǎo)...
- 簡介: 網(wǎng)上找了蠻久艘包,好多中文api文檔都被設(shè)置權(quán)限瀏覽的猛,趁還未完全被商業(yè)化,記錄一個方便日后使用注:任何形式轉(zhuǎn)載...
- 本人大四學(xué)生想虎,用iOS設(shè)備兩年多了,真正的接觸開發(fā)有半年時間吧叛拷,之前Java基礎(chǔ)還行∩喑現(xiàn)在感覺有點小瓶頸,很多東西...