用Selenium实现拖拽操作

2022-04-29  本文已影响0人  吱吱菌啦啦

将a拖到b上,有三种方法:

实现方式如下:

from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By

class TestActionChains():
    def setup(self):
        self.driver = webdriver.Chrome()
        # 隐式等待
        self.driver.implicitly_wait(5)
        self.driver.get('https://sahitest.com/demo/dragDropMooTools.htm')

    def teardown(self):
        self.driver.quit()

    def test_drag(self):
        element_drag = self.driver.find_element(By.XPATH, '//*[@id="dragger"]')
        element_drog = self.driver.find_element(By.XPATH, '/html/body/div[2]')
        action = ActionChains(self.driver)
        # 方法一
        # action.drag_and_drop(element_drag, element_drog).perform()
        # 方法二
        action.click_and_hold(element_drag).release(element_drog).perform()
        # 方法三
        action.click_and_hold(element_drag).move_to_element(element_drog).release().perform()
上一篇下一篇

猜你喜欢

热点阅读