1.安裝
pip install selenium
2.元素定位
id: find_element_by_id()
name: find_element_by_name()
class_name: find_element_by_class_name()
tag_name: find_element_by_tag_name()
超鏈接定位(a標簽)
link_text: find_element_by_link_text(‘hao123’) 使用link_text,括號里內容必須完整
partial_link_text: find_element_by_partial_link_text(‘hao12’) 使用partial_link_text,括號里內容可不完整
css選擇器定位
find_element_by_css_selector(‘#kw’)
括號內#后面加id,. 后面呢加class,[name= ]代表查找name,[value= ]代表查找value
3.行為
ActionChains方法串列:
匯入:from selenium.webdriver.common.action_chains import ActionChains
click(on_element=None) 點擊一個元素
on_element:要點擊的元素,如果是None,點擊滑鼠當前的位置
click_and_hold(on_element=None) 滑鼠左鍵點擊一個元素并且保持
double_click(on_element=None) 雙擊一個元素
drag_and_drop(source, target) 滑鼠左鍵點擊source元素,然后移動到target元素釋放滑鼠按鍵
drag_and_drop_by_offset(source, xoffset,yoffset) 拖拽目標元素到指定的偏移點釋放
move_by_offset(xoffset,yoffset) 將當前滑鼠的位置進行移動
move_to_element(to_element) 把滑鼠移到一個元素的中間
move_to_element_with_offset(to_element,xoffset,yoffset) 滑鼠移動到元素的指定位置,偏移量以元素的左上角為基準
key_down(value,element=None) 按下鍵盤不釋放,我們應該只對那些功能鍵使用(Contril,Alt,Shift),value為功能鍵,element為目標元素,
key_up(value,element=None) 釋放鍵
perform() ——執行鏈中的所有動作
release(on_element=None) ——在某個元素位置松開滑鼠左鍵
send_keys(*keys_to_send) 向當前的焦點元素發送鍵
send_keys_to_element(element,*keys_to_send) 向指定的元素發送鍵
quit() 退出驅動,關閉所有關聯的視窗
4.截圖
picture_time = time.strftime("%Y-%m-%d-%H_%M_%S", time.localtime(time.time()))
driver.save_screenshot('./Picture/'+picture_time+'.png')
driver.get_screenshot_as_file('./Picture/' + picture_time + '.png')
兩種截圖的方法driver.save_screenshot()和driver.get_screenshot_as_file()
上述代碼寫的是,給截的圖片命名為截圖的時間,存放在Picture檔案夾里
5.等待機制
固定等待(time):固定等待幾秒,這種方式會導致這個腳本運行時間過長
第一種方法:import time
time.sleep(2)
第二種方法:from time import sleep
sleep(2)
隱式等待(implicitly_wait()):設定最長等待時間,如果規定時間內頁面元素被找到則執行下一步,否則等待到設定的最長時間,但這個等待是,程式會一直等待整個頁面加載完成,才會執行下一步,
driver.implicitly_wait(30)
顯式等待(webdriverwait):每隔一段時間(該時間一般都很短,默認為0.5秒,也可以自定義),執行自定義的程式判斷條件,如果判斷條件成立,就執行下一步,否則繼續等待,直到超過設定的最長等待時間,然后拋出TimeOutEcpection的例外資訊,result=WebDriverWait(driver,10).until(expected_conditions.alert_is_present())
until內為判斷條件,與expected_conditions結合使用還可以為:
title_is 判斷當前頁面的 title 是否完全等于(==)預期字串,回傳布林值
title_contains 判斷當前頁面的 title 是否包含預期字串,回傳布林值
presence_of_element_located 判斷某個元素是否被加到了 dom 樹里,并不代表該元素一定可見
visibility_of_element_located 判斷元素是否可見(可見代表元素非隱藏,并且元素寬和高都不等于 0)
visibility_of 同上一方法,只是上一方法引數為locator,這個方法引數是 定位后的元素
presence_of_all_elements_located 判斷是否至少有 1 個元素存在于 dom 樹中,舉例:如果頁面上有n 個元素的 class 都是’wp’,那么只要有 1 個元素存在,這個方法就回傳 True
text_to_be_present_in_element 判斷某個元素中的 text 是否 包含 了預期的字串
text_to_be_present_in_element_value 判斷某個元素中的 value 屬性是否包含 了預期的字串
frame_to_be_available_and_switch_to_it 判斷該 frame 是否可以 switch進去,如果可以的話,回傳True 并且 switch 進去,否則回傳 False
invisibility_of_element_located 判斷某個元素中是否不存在于dom樹或不可見
element_to_be_clickable 判斷某個元素中是否可見并且可點擊
staleness_of 等某個元素從 dom 樹中移除,注意,這個方法也是回傳 True或 False
element_to_be_selected 判斷某個元素是否被選中了,一般用在下拉串列
element_selection_state_to_be 判斷某個元素的選中狀態是否符合預期
element_located_selection_state_to_be 跟上面的方法作用一樣,只是上面的方法傳入定位到的element,而這個方法傳入 locator
alert_is_present 判斷頁面上是否存在 alert
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/340710.html
標籤:其他
