python中selenium操作下拉滾動條的幾種方法匯總
UI自動化中經(jīng)常會遇到元素識別不到,找不到的問題,原因有很多,比如不在iframe里,xpath或id寫錯了等等;但有一種是在當前顯示的頁面元素不可見,拖動下拉條后元素就出來了。
比如下面這樣一個網(wǎng)頁,需要進行拖動下拉條后才能通過selenium找到密碼輸入框的元素,
在python中有幾種方法解決這種問題,簡單介紹下,給需要的人:
方法一)使用js腳本直接操作,方法如下:
js="var q=document.getElementById('id').scrollTop=10000" driver.execute_script(js)
或:
js="var q=document.documentElement.scrollTop=10000" driver.execute_script(js)
這里的id為滾動條的id,但js中沒有xpath的方法,所以滾動條沒有id的網(wǎng)頁此方法不適用
方法二)使用js腳本拖動到提定地方
target = driver.find_element_by_id("id_keypair") driver.execute_script("arguments[0].scrollIntoView();", target) #拖動到可見的元素去
這個方法可以將滾動條拖動到需要顯示的元素位置,此方法用途比較廣,可以使用
方法三)根據(jù)頁面顯示進行變通,發(fā)送tab鍵
在本例中的頁面中,密碼是輸入框,正常手工操作時,可以通過tab鍵會切換到密碼框中,所以根據(jù)此思路,在python中也可以發(fā)送tab鍵來切換,使元素顯示
from selenium.webdriver.common.keys import Keys driver.find_element_by_id("id_login_method_0").send_keys(Keys.TAB)
update
前段時間使用robotframe work框架時,selenium2library里面有一個非常好用的功能Focus,會自動定位到元素,研讀一下源碼:
def focus(self, locator): """Sets focus to element identified by `locator`.""" element = self._element_find(locator, True, True) self._current_browser().execute_script("arguments[0].focus();", element)
從源碼中我們可以看到,此方法與我們在python自己寫的方法二)一致,工具給我們做了封裝。
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
- Python selenium 三種等待方式詳解(必會)
- 玩轉(zhuǎn)python selenium鼠標鍵盤操作(ActionChains)
- python selenium 獲取標簽的屬性值、內(nèi)容、狀態(tài)方法
- Python + selenium自動化環(huán)境搭建的完整步驟
- Python使用selenium實現(xiàn)網(wǎng)頁用戶名 密碼 驗證碼自動登錄功能
- Python中使用 Selenium 實現(xiàn)網(wǎng)頁截圖實例
- SpringBoot優(yōu)化啟動速度的方法實現(xiàn)
- Python selenium 三種等待方式解讀
- Selenium(Python web測試工具)基本用法詳解
- Python selenium文件上傳方法匯總
- python+selenium 定位到元素,無法點擊的解決方法
- python3+selenium自動化測試框架詳解
- python selenium 彈出框處理的實現(xiàn)
- selenium+python實現(xiàn)自動登錄腳本
- selenium+python實現(xiàn)自動化登錄的方法
- Python Selenium 之關(guān)閉窗口close與quit的方法
- python+selenium實現(xiàn)登錄賬戶后自動點擊的示例
- Python中selenium庫的用法詳解
相關(guān)文章
python 實現(xiàn)多維數(shù)組(array)排序
今天小編就為大家分享一篇python 實現(xiàn)多維數(shù)組(array)排序,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-02-02Python中的collections集合與typing數(shù)據(jù)類型模塊
這篇文章介紹了Python中的collections集合與typing數(shù)據(jù)類型模塊,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-05-05