selenium?drag_and_drop不生效的解決辦法
做自動化時發(fā)現(xiàn)用drag_and_drop模擬拖拽沒效果,頁面上只能看到元素source閃了一下,但是并沒有拖拽到元素target上(推測可能是我用系統(tǒng)頁面在拖拽時有個JS效果,但是drag_and_drop模擬拖拽的時候執(zhí)行太快沒能觸發(fā)JS,所以沒有把這兩個元素拖拽到一起)。
通過不斷嘗試,終于解決了,這里記錄一下,希望其他人遇到類似情況時能有所啟發(fā)。方法1是我嘗試的過程;方法2是我看到的另一種方法,雖然試驗了下沒效果,但說不定對其他的拖拽場景是有效的。
方法1:分解drag_and_drop動作
從源碼可以看出drag_and_drop的源碼執(zhí)行了兩個操作,既然直接用drag_and_drop不行,那調(diào)整下這兩個操作或許可行
def drag_and_drop(self, source, target): """ Holds down the left mouse button on the source element, then moves to the target element and releases the mouse button. :Args: - source: The element to mouse down. - target: The element to mouse up. """ self.click_and_hold(source) self.release(target) return self
drag_and_drop里有兩個動作:click_and_hold(在source元素上單擊鼠標不松開),release(在target元素上釋放點擊狀態(tài)的鼠標)。中間加一個鼠標移動的動作是否可行呢?
我把拖拽的流程改成了:
ActionChains(self.driver).click_and_hold(source).perform() ActionChains(self.driver).move_by_offset(x, y).perform() ActionChains(self.driver).release(target).perform()
試驗了一下,在執(zhí)行move_by_offset動作的時候能觸發(fā)JS的效果,只不過位移的xy不準確,觸發(fā)不了另一個JS,只要計算好要偏移的位置就好了
最終的實現(xiàn):
def drag_and_drop(self): source = self.find_element_and_scroll_into_view(source_loc) target = self.find_element_and_scroll_into_view(target_loc) # 先移動一點 觸發(fā)js效果 觸發(fā)后元素變小 重新獲取元素以便能準確計算鼠標偏移量 ActionChains(self.driver).click_and_hold(source).move_by_offset(5, 0).perform() drag_source = self.find_element(change_source_loc) x1, x2 = (drag_source.location.get("x"), drag_source.location.get("x") + drag_source.size.get("width")) y1, y2 = (drag_source.location.get("y"), drag_source.location.get("y") + drag_source.size.get("height")) source_middle_x = (x1 + x2) / 2 source_middle_y = (y1 + y2) / 2 x3, x4 = (target.location.get("x"), target.location.get("x") + target.size.get("width")) y3, y4 = (target.location.get("y") + 0.5 * target.size.get("height"), target.location.get("y") + target.size.get("height")) target_middle_x = (x3 + x4) / 2 target_middle_y = (y3 + y4) / 2 x = target_middle_x - source_middle_x y = target_middle_y - source_middle_y ActionChains(self.driver).move_by_offset(x, y).perform() ActionChains(self.driver).release(target).perform()
拖拽效果:
方法2:使用seletools解決
雖然我試了下沒效果,但是感覺是有用的,這里一并記錄下。
selenium的drag_and_drop方法在某些場景下無效,這是官方很久就已經(jīng)知道的BUG,只不過沒有在源碼中修復(fù),而是提供了單獨的包,因為David Burnes(核心 Selenium 提交者)認為拖放錯誤是一個webdriver網(wǎng)絡(luò)驅(qū)動問題,在Selenium中提供任何暫時避開網(wǎng)絡(luò)的方法并不是一個好主意。詳細內(nèi)容可以閱讀文章「Selenium Drag and Drop Bug Workaround」
安裝
pip install seletools
使用
from seletools.actions import drag_and_drop source = driver.find_element(xxx) target = driver.find_element(xxx) drag_and_drop(driver, source, target)
到此這篇關(guān)于selenium drag_and_drop不生效的解決辦法的文章就介紹到這了,更多相關(guān)selenium drag_and_drop不生效內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Laravel+Dingo/Api 自定義響應(yīng)的實現(xiàn)
這篇文章主要介紹了Laravel+Dingo/Api 自定義響應(yīng)的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-02-02調(diào)試Python程序代碼的幾種方法總結(jié)
這篇文章主要介紹了調(diào)試Python程序代碼的幾種方法總結(jié),文中代碼基于Python2.x版本,需要的朋友可以參考下2015-04-04python的pytest框架之命令行參數(shù)詳解(下)
這篇文章主要介紹了python的pytest框架之命令行參數(shù)詳解,今天將繼續(xù)更新其他一些命令選項的使用,和pytest收集測試用例的規(guī)則,需要的朋友可以參考下2019-06-06python爬蟲簡單的添加代理進行訪問的實現(xiàn)代碼
本文通過實例代碼給大家介紹了python爬蟲簡單的添加代理進行訪問,代碼簡單易懂,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2019-04-04Python數(shù)據(jù)分析之?Matplotlib?折線圖繪制
這篇文章主要介紹了Python數(shù)據(jù)分析之?Matplotlib?折線圖繪制,在數(shù)據(jù)分析中,數(shù)據(jù)可視化也非常重要,下文通過數(shù)據(jù)分析展開對折線圖的繪制,需要的小伙伴可以參考一下2022-05-05tensorflow2.10使用BERT實現(xiàn)Semantic Similarity過程解析
這篇文章主要為大家介紹了tensorflow2.10使用BERT實現(xiàn)Semantic Similarity過程解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-04-04