如何在Selenium的Python中拖动元素?

huangapple go评论55阅读模式
英文:

How to drag the element in selenium's python?

问题

I have the example code to drag the box but it can not work.
Could you support me to find the root cause?
Many thanks 如何在Selenium的Python中拖动元素?

import time

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service as ChromeService
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver import ActionChains


def DragAndDrop():
    driver = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install()))
    driver.maximize_window()
    driver.implicitly_wait(10)
    driver.get("https://letcode.in/draggable")
    dragSource = driver.find_element(By.ID, "header")
    print("dragSource outerHTML: " + str(dragSource.get_attribute('outerHTML')))
    dragContainer = driver.find_element(By.XPATH, "//div[@class='example-boundary']")
    print("dragContainer size: " + str(dragContainer.size))

    action = ActionChains(driver)
    # +SOLUTION1
    action.move_by_offset(0.1 * dragContainer.size['width'], 0.1 * dragContainer.size['height']).release().perform()
    # +SOLUTION2
    # action.drag_and_drop_by_offset(dragSource, 15, 15).perform()
    time.sleep(2)

if __name__ == "__main__":
    DragAndDrop()

Both solution 1 and 2 can not work.

Could you support me to find the root cause?

英文:

I have the example code to drag the box but it can not work.
Could you support me to find the root cause?
Many thanks 如何在Selenium的Python中拖动元素?

import time

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service as ChromeService
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver import ActionChains


def DragAndDrop():
    driver = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install()))
    driver.maximize_window()
    driver.implicitly_wait(10)
    driver.get("https://letcode.in/draggable")
    dragSource = driver.find_element(By.ID, "header")
    print("dragSource outerHTML: " + str(dragSource.get_attribute('outerHTML')))
    dragContainer = driver.find_element(By.XPATH, "//div[@class='example-boundary']")
    print("dragContainer size: " + str(dragContainer.size))

    action = ActionChains(driver)
    # +SOLUTION1
    action.move_by_offset(0.1 * dragContainer.size['width'], 0.1 * dragContainer.size['height']).release().perform()
    # +SOLUTION2
    # action.drag_and_drop_by_offset(dragSource, 15, 15).perform()
    time.sleep(2)

if __name__ == "__main__":
    DragAndDrop()

Both solution 1 and 2 can not work.

Could you support me to find the root cause?

答案1

得分: 0

以下是翻译好的代码部分:

有时候一般的拖放操作在下面的代码中不起作用,此代码将鼠标移动到dragSource元素,然后暂停1秒钟,点击并保持,然后暂停1秒钟,然后通过计算出的x和y偏移量移动它。之后,它再次将鼠标移动到dragSource元素,然后通过相同的偏移量移动它,然后暂停1秒钟。最后,释放鼠标点击以完成拖放操作
经过测试,能够拖放

import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service as ChromeService
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver import ActionChains

def DragAndDrop():
    driver = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install()))
    driver.maximize_window()
    driver.implicitly_wait(10)
    driver.get("https://letcode.in/draggable")
    dragSource = driver.find_element(By.ID, "header")
    print("dragSource outerHTML: " + str(dragSource.get_attribute('outerHTML')))
    dragContainer = driver.find_element(By.XPATH, "//div[@class='example-boundary']")
    print("dragContainer size: " + str(dragContainer.size))

    action = ActionChains(driver)
    x = int(dragContainer.size['width'] * 0.1)
    y = int(dragContainer.size['height'] * 0.1)
    action.move_to_element(dragSource).pause(1).click_and_hold(dragSource).pause(1).move_by_offset(x, y).move_to_element(
        dragSource).move_by_offset(x, y).pause(1).release().perform()

if __name__ == "__main__":
    DragAndDrop()

选项#2也有效

action.click_and_hold(dragSource).move_by_offset(x, y).move_by_offset(x, y).release().perform()
英文:

Sometime the general drag and drop doesn't try work below code, This code moves the mouse to the dragSource element then pauses for 1 sec, clicks and holds it then pauses for 1 sec, then moves it by the calculated offsets x and y. After that, it moves the mouse to the dragSource element again and moves it by the same offsets then pauses for 1 sec. Finally, it releases the mouse click to complete the drag and drop action
Tested and Was able to drag and drop

import time

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service as ChromeService
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver import ActionChains


def DragAndDrop():
    driver = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install()))
    driver.maximize_window()
    driver.implicitly_wait(10)
    driver.get("https://letcode.in/draggable")
    dragSource = driver.find_element(By.ID, "header")
    print("dragSource outerHTML: " + str(dragSource.get_attribute('outerHTML')))
    dragContainer = driver.find_element(By.XPATH, "//div[@class='example-boundary']")
    print("dragContainer size: " + str(dragContainer.size))

    action = ActionChains(driver)
    x = int(dragContainer.size['width'] * 0.1)
    y = int(dragContainer.size['height'] * 0.1)
    action.move_to_element(dragSource).pause(1).click_and_hold(dragSource).pause(1).move_by_offset(x,
                                                                                                   y).move_to_element(
        dragSource).move_by_offset(x, y).pause(1).release().perform()


if __name__ == "__main__":
    DragAndDrop()

Option #2 Also works

action.click_and_hold(dragSource).move_by_offset(x, y).move_by_offset(x, y).release().perform()

huangapple
  • 本文由 发表于 2023年5月11日 02:24:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/76221560.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定