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 selfdrag_and_drop里有兩個動作:click_and_hold(在source元素上單擊鼠標(biāo)不松開),release(在target元素上釋放點擊狀態(tài)的鼠標(biāo))。中間加一個鼠標(biāo)移動的動作是否可行呢?
我把拖拽的流程改成了:
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不準(zhǔn)確,觸發(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ā)后元素變小 重新獲取元素以便能準(zhǔn)確計算鼠標(biāo)偏移量
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 提交者)認(rèn)為拖放錯誤是一個webdriver網(wǎng)絡(luò)驅(qū)動問題,在Selenium中提供任何暫時避開網(wǎng)絡(luò)的方法并不是一個好主意。詳細(xì)內(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)文章
Python利用appium實現(xiàn)模擬手機滑動操控的操作
Appium 是一個開源、跨平臺的自動化測試工具,用于測試原生和輕量移動應(yīng)用,支持 iOS, Android 和 FirefoxOS 平臺。本文將利用appium實現(xiàn)模擬手機滑動操控的操作,感興趣的可以了解一下2022-07-07
利用Python爬取微博數(shù)據(jù)生成詞云圖片實例代碼
這篇文章主要給大家介紹了關(guān)于利用Python爬取微博數(shù)據(jù)生成詞云圖片的相關(guān)資料,文中通過示例代碼介紹非常詳細(xì),對大家學(xué)習(xí)或者使用python具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-08-08
Pygame庫200行代碼實現(xiàn)簡易飛機大戰(zhàn)
本文主要介紹了Pygame庫200行代碼實現(xiàn)簡易飛機大戰(zhàn),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-12-12

