1.獲取當(dāng)前頁(yè)面的Url:
方法:current_url?
實(shí)例:driver.current_url
2.獲取元素坐標(biāo):
方法:location
解釋:首先查找到你要獲取元素的锌妻,然后調(diào)用location方法
實(shí)例:driver.find_element_by_xpath("xpath").location
3.表單的提交:
方法:submit
解釋:查找到表單(from)直接調(diào)用submit即可
實(shí)例:driver.find_element_by_id("form1").submit()
4.獲取CSS的屬性值
方法:value_of_css_property(css_name)
實(shí)例:driver.find_element_by_css_selector("input.btn").value_of_css_property("input.btn")
5.獲取元素的屬性值
方法:get_attribute(element_name)
實(shí)例:driver.find_element_by_id("kw").get_attribute("kw")
6.判斷元素是否被選中
方法:is_selected()
實(shí)例:driver.find_element_by_id("form1").is_selected()
7.返回元素的大小
方法:size
實(shí)例:driver.find_element_by_id("iptPassword").size
返回值:{'width': 250, 'height': 30}
8.判斷元素是否顯示
方法:is_displayed()
實(shí)例:driver.find_element_by_id("iptPassword").is_displayed()
9.判斷元素是否被使用
方法:is_enabled()
實(shí)例:driver.find_element_by_id("iptPassword").is_enabled()
10.獲取元素的文本值
方法:text
實(shí)例:driver.find_element_by_id("iptUsername").text
11.元素賦值
方法:send_keys(*values)
實(shí)例:driver.find_element_by_id("iptUsername").send_keys('admin')
12.返回元素的tagName
方法:tag_name
實(shí)例:driver.find_element_by_id("iptUsername").tag_name
13.刪除瀏覽器所有的cookies
方法:delete_all_cookies()
實(shí)例:driver.delete_all_cookies()
14.刪除指定的cookie
方法:delete_cookie(name)
實(shí)例:deriver.delete_cookie("my_cookie_name")
15.關(guān)閉瀏覽器
方法:close()
實(shí)例:driver.close()
16.關(guān)閉瀏覽器并且退出驅(qū)動(dòng)程序
方法:quit()
實(shí)例:driver.quit()
17.返回上一頁(yè)
方法:back()
實(shí)例:driver.back()
18.清空輸入框
方法:clear()
實(shí)例:driver.clear()
19.瀏覽器窗口最大化
方法:maximize_window()
實(shí)例:driver.maximize_window()
20.查看瀏覽器的名字
方法:name
實(shí)例:drvier.name
21.返回當(dāng)前會(huì)話中的cookies
方法:get_cookies()
實(shí)例:driver.get_cookies()
22.根據(jù)cookie name 查找映射Value值
方法:driver.get_cookie(cookie_name)
實(shí)例:driver.get_cookie("NET_SessionId")
23.截取當(dāng)前頁(yè)面
方法:get_screenshot_as_file(filename)
實(shí)例:driver.get_screenshot_as_file("D:\\Program Files\\Python27\\NM.bmp")
24.獲取當(dāng)前窗口的坐標(biāo)
方法:get_window_position()
實(shí)例:driver.get_window_position()
25.獲取當(dāng)前窗口的長(zhǎng)和寬
方法:get_window_size()
實(shí)例:driver.get_window_size()
ActionChains類鼠標(biāo)操作的常用方法:
引入ActionChains類:from selenium.webdriver.common.action_chains import ActionChains
26.右擊
方法:context_click()
實(shí)例:ActionChains(driver).context_click(driver.find_element_by_id("id")).perform()
27.雙擊
方法:double_click()
實(shí)例:ActionChains(driver).double_click(driver.find_element_by_name("name")).perform()
28:鼠標(biāo)拖放
方法:drag_and_drop(source, target)
? source:鼠標(biāo)按下的源元素霜瘪;target:鼠標(biāo)釋放的目標(biāo)元素
實(shí)例:element = driver.find_element_by_name("name")
? target = driver.find_element_by_name("name")
? ? ? ActionChains(driver).drag_and_drop(element, target).perform()
29:鼠標(biāo)懸停在一個(gè)元素上(hover)
方法:move_to_element()
實(shí)例:above = driver.find_element_by_xpath("xpath路徑")
? ActionChains(driver).move_to_element(above).perform()
30:按下鼠標(biāo)左鍵在一個(gè)元素上
方法:click_and_hold()
實(shí)例:left = driver.find_element_by_name("name")
? ? ActionChains(driver).click_and_hold(left).perform()
鍵盤事件:
引入Keys類包:from selenium.webdriver.common.keys import Keys
31:輸入
方法:send_keys()
實(shí)例:driver.find_element_by_id("id").send_keys("XXX")
32:輸入空格
方法:send_keys(Keys.SPACE)
實(shí)例:driver.find_element_by_id("id").send_keys(Keys.SPACE)
33:ctrl + a? ? 全選輸入框的內(nèi)容
ctrl + x? ? 剪切輸入框的內(nèi)容
ctrl + v? ? 粘貼到輸入框
? ? ctrl + c? ? 復(fù)制
方法:send_keys(Keys.CONTROL,'a')
實(shí)例:driver.find_element_by_id("id").send_keys(Keys.CONTROL,'a')
34:回車代替點(diǎn)擊
方法:send_keys(Keys.ENTER)
實(shí)例:driver.find_element_by_id("id").send_keys(Keys.ENTER)
35:制表鍵(Tab)
方法:send_keys(Keys.TAB)
實(shí)例:driver.find_element_by_id("id").send_keys(Keys.TAB)
36:回退鍵(Esc)
方法:send_keys(Keys.ESCAPE)
實(shí)例:driver.find_element_by_id("id").send_keys(Keys.ESCAPE)
等待時(shí)間
導(dǎo)入 WebDriverWait 包
from selenium.webdriver.support.ui import WebDriverWait
導(dǎo)入 time 包
import time
37:固定等待時(shí)間
方法:sleep()
實(shí)例:time.sleep(5)? # 等待5秒
38:等待一個(gè)元素被發(fā)現(xiàn),或一個(gè)命令完成鄙煤,超出了設(shè)置時(shí)間則拋出異常智能等待。
方法:implicitly_wait()
實(shí)例:driver.implicitly_wait(30)
39. 在設(shè)置時(shí)間內(nèi)请垛,默認(rèn)每隔一段時(shí)間檢測(cè)一次當(dāng)前頁(yè)面元素是否存在顾复,如果超過(guò)設(shè)置時(shí)間檢測(cè)不到則拋出異常
"方法:WebDriverWait()"
#WebDriverWait(driver, timeout, poll_frequency=0.5, ignored_exceptions=None)
#——driver:WebDriver的驅(qū)動(dòng)程序(Ie, Firefox, Chrome或遠(yuǎn)程)
#——timeout:最長(zhǎng)超時(shí)時(shí)間侄刽,默認(rèn)以秒為單位
#——poll_frequency:休眠時(shí)間的間隔(步長(zhǎng))時(shí)間,默認(rèn)為0.5秒
#——ignored_exceptions:超時(shí)后的異常信息鹊汛,默認(rèn)情況下拋NoSuchElementException異常
實(shí)例:
element = WebDriverWait(driver, 10).until(lambda x: x.find_element_by_id("id"))
#一般由unit()或until_not()方法配合使用蒲赂,同上:
調(diào)用該方法提供的驅(qū)動(dòng)程序作為一個(gè)參數(shù),直到返回值不為False刁憋。
——until(method, message=’’)
調(diào)用該方法提供的驅(qū)動(dòng)程序作為一個(gè)參數(shù)滥嘴,直到返回值為False。
——until_not(method, message=’’)
# 還可以與expected_conditions 一起使用
from selenium.webdriver.support import expected_conditions as EC
# 實(shí)例: 判斷某個(gè)元素是否可見(jiàn)并且是enable的至耻,這樣才clickable
WebDriverWait(dr,15,1).until(EC.element_to_be_clickable((By.ID,"EmployeeListMenu")),"Not Find element")
40. 選擇當(dāng)前頁(yè)面上所有tag
name為input的元素
inputs = driver.find_elements_by_tag_name(‘input‘)
41:從中過(guò)濾出type為checkbox的元素若皱,并勾選上
for input in inputs:
if input.get_attribute(‘type‘) == ‘checkbox‘:
input.click()
42:使用CSS定位選擇所有type為checkbox的元素,并勾選上
checkboxes = driver.find_elements_by_css_selector(‘input[type = checkbox]‘)
for checkbox in checkboxes:
checkbox.click()
43:把最后一個(gè)checkbox的勾去掉尘颓,pop()方法空參數(shù)時(shí)走触,默認(rèn)移除list中的最后一個(gè)元素。
driver.find_elements_by_css_selector(‘input[type = checkbox]‘).pop().click()
切換活動(dòng)對(duì)象
44:切換瀏覽器handle
# 切換不同的tab頁(yè)
方法:driver.switch_to.window(window_name)
# 備注:從A頁(yè)跳轉(zhuǎn)到B頁(yè)疤苹,句柄已經(jīng)切換過(guò)去互广,但是焦點(diǎn)沒(méi)有切過(guò)去,所以需要switch_to.window卧土,把焦點(diǎn)也切過(guò)去惫皱,才可以在當(dāng)前頁(yè)進(jìn)行操作。
# 切換是思路尤莺,獲取所有的句柄旅敷,因?yàn)榉祷厥且粋€(gè)list,而且要切換的對(duì)象都是最后一個(gè)缝裁,可以使用[-1]直接切過(guò)去
# 例如:
driver.switch_to.window(driver.window_handles[-1])
45:返回上一級(jí)表單
方法: driver.switch_to_parent_content()#舊方法
? driver.switch_to.parent_content#新方法
46:返回最外層表單
方法: driver.switch_to_default_content()#舊方法
? driver.switch_to.default_content()#新方法
47:切換到指定frame中
方法:driver.switch_to.frame('xxx')
實(shí)例:driver.switch_to.frame('frame_name')
? driver.switch_to.frame(index)
? driver.switch_to.frame(driver.find_elements_by_tag_name("iframe")[0])? ? ? ?
48:獲取當(dāng)前page的title
方法:driver.title
實(shí)例:driver.title
49:焦點(diǎn)切換到彈窗扫皱。
方法:driver.switch_to_alert()
實(shí)例:driver.switch_to_alert()
50:前進(jìn)
方法:driver.forward()?
51:刷新頁(yè)面
方法:driver.refresh()