Selenium常用語法總結
一、Selenium常用定位語法
1.元素定位
(1)ID定位元素:
find_element_by_id(‘’)
(2)通過元素的類名稱定位元素:
find_element_by_class_name(‘’)
(3)通過元素的html中的位置定位元素:
find_element_by_xpath(‘’)
xpath語法詳見:https://www.runoob.com/xpath/xpath-tutorial.html
(4)通過元素的標簽名稱定位元素:
find_element_by_tag_name(‘’)
(5)通過元素的css定位元素:
find_element_by_css_selector(‘’)
css選擇器語法詳見:https://www.cnblogs.com/yuangushidai/p/10401713.html
(6)通過元素的名定位元素:
find_element_by_name(‘’)
(7)通過元素的鏈接名稱定位元素:
find_element_by_link_text(‘’)
(8)通過元素的部分鏈接名稱定位元素:
find_element_by_partial_link_text(‘’)
2.元素操作
(1)driver.find_element_by_id(‘’).click() #點擊
(2)driver.find_element_by_id(‘’).send_keys(“123456”) #輸入
(3)driver.find_element_by_id(‘’).clear()
或者如下:
ele = driver.find_element_by_id(‘’)
ele.clear()
ele.send_keys(‘’)
ele.click()
3,頁面操作
打開網頁:driver.get("https://www.baidu.com")
關閉網頁:driver.close()
在輸入框中輸入內容:send_keys(“”)
清空輸入框中的內容:text.clear()
獲取輸入框中的內容:get_attribute(‘value’)
刷新頁面:driver.refresh()
頁面前進后退:
driver.forward()
driver.back()
4.下拉選擇框
先引入下面的包:
from selenium.webdriver.support.ui import Select
(1)選擇列表:
select_by_index(self, index) #以index屬性值來查找匹配的元素並選擇;
select_by_value(self, value) #以value屬性值來查找該option並選擇;
select_by_visible_text(self, text) #以text文本值來查找匹配的元素並選擇;
first_selected_option(self) #選擇第一個option 選項 ;
(2)清除選擇列表
deselect_by_index(self, index)#以index屬性值來查找匹配的元素並取消選擇;
deselect_by_value(self, value)#以value屬性值來查找該option並取消選擇;
deselect_by_visible_text(self, text)#以text文本值來查找匹配的元素並取消選擇;
deselect_all(self) #將所有選擇清除;
(3)選項
options #以列表形式返回屬於此select標簽的所有option
all_selected_options #全部選擇了的option的列表
first_selected_option #第一個被選中的option元素如果select沒有multiple值,此時獲取值為當前選擇的option
(4)用法如下:
eleTime = Select(Driver.find_element_by_id('cc_start_time'))通過find定位好select元素后面進行選擇操作
eleTime.select_by_value() 等
5.單選框
radio=driver.find_element_by_id("BookMode")
radio.click() #選擇某個單選項
radio.clear() #清空某個單選項
radio.isSelected() #判斷某個單選項是否已經被選擇
6.多選框
checkbox = driver.find_element_by_id("myCheckbox")
checkbox.click()
checkbox.clear()
checkbox.isSelected() ##判斷是否選中
7.彈出對話框
driver.switch_to_alert().text #獲取彈出框信息
driver.switch_to_alert().accept() #接受警告(等於點了個確定)
driver.switch_to_alert().dismiss() #不接受警告(等於點了個取消)
driver.switch_to_alert().send_keys(“hello”) #給警告框輸入內容
用法如下:
driver.find_element_by_xpath("//*[@id='gxszButton']/a").click()
driver.switch_to_alert().dismiss() #解散該警告框
driver.switch_to_alert().accept() #確認
texts = dirver.switch_to_alert().text()#獲取彈出框文本
print(text)
8.表單
approve = driver.find_element_by_id("approve")
approve.submit() //只適合於表單的提交
或者
driver.find_element_by_id("approve").submit()
9.上傳文件
上傳文件的元素操作:
adFileUpload =driver.find_element_by_id("WAP-upload")
filePath = "C:\test\\uploadfile\\media_ads\\test.jpg"
adFileUpload.sendKeys(filePath)
10.調用js
Web driver對Java Script的調用是通過JavascriptExecutor來實現的,例如:
js1 = 'document.getElementById("su").click();'
Driver.execute_script(js1)
10.超時設置
(1)強制等待 sleep(xx),不建議總用這種等待方式,太死板,嚴重影響程序執行速度
time.sleep(5)
(2)隱性等待implicitly_wait(xx),隱性等待對整個driver的周期都起作用,所以只要設置一次即可
Driver.implicitly_wait(10)
(3) 顯性等待WebDriverWait,配合該類的until()和until_not()方法,就能夠根據判斷條件而進行靈活地等待
需要調用庫如下:
from selenium.webdriver.support.wait import WebDriverWait
WebDriverWait(driver, 超時時長, 調用頻率, 忽略異常).until(可執行方法, 超時時返回的信息)
例子:
WebDriverWait(driver,30).until(lambdax:x.find_element_by_id("kw")).send_keys("sky")
二、Selenium常用鼠標操作
鼠標操作需導入下面的包:
from selenium.webdriver.common.actionchains import ActionChains
常用鼠標操作事件:
click(on_element=None) #單擊鼠標左鍵
click_and_hold(on_element=None) #點擊鼠標左鍵,按住不放
context_click(on_element=None) #點擊鼠標右鍵
double_click(on_element=None) #雙擊鼠標左鍵
drag_and_drop(source, target) #拖拽到某個元素然后松開
drag_and_drop_by_offset(source, xoffset, yoffset) #拖拽到某個坐標然后松開
move_by_offset(xoffset, yoffset) #鼠標移動到距離當前位置(x,y)
move_to_element(to_element) #鼠標移動到某個元素
move_to_element_with_offset(to_element, xoffset, yoffset) #將鼠標移動到距某個元素多少距離的位置
release(on_element=None) #在某個元素位置松開鼠標左鍵
perform() #執行鏈中的所有動作
用法如下:
先定位要操作的元素,在執行鼠標操作
Right_click = driver.find_element_by_id(‘’)
ActionChains(driver).context_click(right_click).perfor
鼠標移動、拖拽詳細操作參見:
https://www.cnblogs.com/mengyu/p/6901489.html
三、Selenium常用鍵盤操作
1.在使用鍵盤操作前需導入Key包,如下;
from selenium.webdriver.common.keys import Keys
常用按鍵有:
回車鍵 Keys.ENTER
刪除鍵 Keys.BACK_SPACE
空格鍵 Keys.SPACE
制表鍵 Keys.TAB
回退鍵 Keys.ESCAPE
刷新鍵 Keys.F5
2.常用的組合鍵有:
send_keys(Keys.CONTROL,'a') #全選(Ctrl+A)
send_keys(Keys.CONTROL,'c') #復制(Ctrl+C)
send_keys(Keys.CONTROL,'x') #剪切(Ctrl+X)
send_keys(Keys.CONTROL,'v') #粘貼(Ctrl+V)
3.其他按鍵詳見Keys包與鍵盤按鍵對應表
四、常用技巧:
1.凍結瀏覽器:setTimeout(()=>{debugger;},8000) 針對浮動變化元素定位
2.頁面跳轉:
(1)driver.switch_to.window(driver.window_handles[-1]) # 將標簽對象變為新頁面標簽、新網址新標簽頁面書刷新
將處理對象變為新標簽頁面,否則瀏覽器操作對象會找不到要操作頁面中的元素
(2)sreach_window = Driver.current_window_handle
這一句會切換到新url對象,如果不寫這句,瀏覽器對象會去原來url頁面中,找元素操作,這時候我們希望操作的元素找不到就會報錯
3.frame切換
當你發現定位方法沒問題,但定位不到元素時,該元素可能是存在於frame中,你需要先切入frame后再定位元素
(1)switch_to.frame 切入frame
(2)switch_to_default_content() 切回主頁面(切出frame)
用法如下:
driver.switch_to.frame(self.driver.find_element_by_xpath('//*[@id="x-URS-iframe"]'))
五、瀏覽器及驅動下載地址
瀏覽器驅動下載地址:
chromedriver驅動下載地址:
http://chromedriver.storage.googleapis.com/index.html
geckodriver驅動下載地址:
https://github.com/mozilla/geckodriver/releases
iedriverserver驅動下載地址:
http://selenium-release.storage.googleapis.com/index.html
firefox下載地址
http://ftp.mozilla.org/pub/firefox/releases/
chrome下載地址:
https://www.slimjet.com/chrome/google-chrome-old-version.php
注意:
1.firefox56以下不需要安裝webdriver驅動,firefox57(firefox47及以上版本)需要安裝geckodriver-v0.19.1-win32/64。
2.IEDriverServer的版本號和Selenium的版本號一定要一致
六、附表
鍵盤操作:
Keys包與鍵盤按鍵對應表
NULL = '\ue000'
CANCEL = '\ue001' # ^break
HELP = '\ue002'
BACKSPACE = '\ue003'
BACK_SPACE = BACKSPACE #刪除鍵
TAB = '\ue004' #TAB鍵
CLEAR = '\ue005'
RETURN = '\ue006'
ENTER = '\ue007' #回車鍵
SHIFT = '\ue008' #Shift鍵
LEFT_SHIFT = SHIFT
CONTROL = '\ue009'
LEFT_CONTROL = CONTROL #Ctrl 鍵
ALT = '\ue00a' #Alt 鍵
LEFT_ALT = ALT
PAUSE = '\ue00b'
ESCAPE = '\ue00c' #ECS鍵
SPACE = '\ue00d' #空格鍵
PAGE_UP = '\ue00e' #PgUp 鍵
PAGE_DOWN = '\ue00f' #PgDwon 鍵
END = '\ue010' #END 鍵
HOME = '\ue011' #HOME 鍵
LEFT = '\ue012' #左鍵
ARROW_LEFT = LEFT
UP = '\ue013' #上鍵
ARROW_UP = UP
RIGHT = '\ue014'
ARROW_RIGHT = RIGHT #右鍵
DOWN = '\ue015' #下鍵
ARROW_DOWN = DOWN
INSERT = '\ue016' #insert鍵
DELETE = '\ue017' #del鍵
SEMICOLON = '\ue018' #';'鍵
EQUALS = '\ue019' #'='鍵
#數字鍵盤
NUMPAD0 = '\ue01a' # number pad keys
NUMPAD1 = '\ue01b'
NUMPAD2 = '\ue01c'
NUMPAD3 = '\ue01d'
NUMPAD4 = '\ue01e'
NUMPAD5 = '\ue01f'
NUMPAD6 = '\ue020'
NUMPAD7 = '\ue021'
NUMPAD8 = '\ue022'
NUMPAD9 = '\ue023'
MULTIPLY = '\ue024' # '*' 鍵
ADD = '\ue025' # '+' 鍵
SEPARATOR = '\ue026' #','鍵
SUBTRACT = '\ue027' # '-' 鍵
DECIMAL = '\ue028' # '.'鍵
DIVIDE = '\ue029' #'/'鍵
F1 = '\ue031' # function keys
F2 = '\ue032'
F3 = '\ue033'
F4 = '\ue034'
F5 = '\ue035'
F6 = '\ue036'
F7 = '\ue037'
F8 = '\ue038'
F9 = '\ue039'
F10 = '\ue03a'
F11 = '\ue03b'
F12 = '\ue03c'
META = '\ue03d'
COMMAND = '\ue03d'
--------------------------------
此文借鑒多篇博客,愚此拜謝
借鑒處文中已經說明來源,如有侵權,立刪!