gpt4 book ai didi

python - 为什么 ActionChains 拖放 (Selenium WD) 不适用于此脚本?

转载 作者:行者123 更新时间:2023-12-01 08:13:16 25 4
gpt4 key购买 nike

我根据 ActionChains documentation 制作了一个脚本。但画面一动不动。哪里错了?

from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
import time

driver = webdriver.Firefox()
driver.implicitly_wait(1)
driver.maximize_window()
driver.get("https://parrot-tutorial.com/html/api_dragdrop.html")
element = driver.find_element_by_xpath("//img[@src='/images/parrot.png']")
element2 = driver.find_element_by_xpath("//div[@id='box3']")

action = ActionChains(driver)
action.move_to_element(element).click_and_hold()
action.move_to_element(element2).release()
action.perform()

time.sleep(2)
driver.quit()

最佳答案

Selenium 提供了一个 drag_and_drop 方法,您尝试过吗?它看起来像这样:

from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains

driver = webdriver.Firefox()
driver.get("your.site.with.dragndrop.functionality.com")
source_element = driver.find_element_by_name('your element to drag')
dest_element = driver.find_element_by_name('element to drag to')
ActionChains(driver).drag_and_drop(source_element, dest_element).build().perform()

请参阅文档 here

关于python - 为什么 ActionChains 拖放 (Selenium WD) 不适用于此脚本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55105310/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com