当前位置: 首页 > 知识库问答 >
问题:

我正在尝试使用Python和Selenium拖放多个jQuery列表元素

封德华
2023-03-14

我试图使用Python和Selenium拖放多个元素。

我有一个

第一个动作效果很好,但是接下来的动作出现了一些魔法,我想改变网页中项目的顺序。

这是我的python代码

from selenium import webdriver
from selenium.webdriver import ActionChains


driver = webdriver.Chrome()
driver.get("https://jqueryui.com/sortable/")

print(driver.title)
move = ActionChains(driver)
driver.switch_to.frame(driver.find_element_by_tag_name("iframe"))

item1= driver.find_element_by_xpath("//ul[@id='sortable']/li[text()='Item 3']")
before_y = item1.location.get('y')
item_height = item1.size.get('height')
move.drag_and_drop_by_offset(item1,0,item_height+10).perform()

item2= driver.find_element_by_xpath("//ul[@id='sortable']/li[text()='Item 1']")
before_y = item2.location.get('y')
item_height = item2.size.get('height')
move.drag_and_drop_by_offset(item2,0,item_height+10).perform()

item1= driver.find_element_by_xpath("//ul[@id='sortable']/li[text()='Item 3']")
before_y = item1.location.get('y')
item_height = item1.size.get('height')
move.drag_and_drop_by_offset(item1,0,item_height+10).perform()

我的最终目标是将这些项目按[2,3,1,5,6,4,7]或任何其他顺序排序


共有2个答案

施翰学
2023-03-14

拖放偏移量是累加的。在本例中,您可以看到,要将第一个元素从一个位置移动到下一个位置,您应该一步一步地将第一次移动50个像素到底部,但下一次,要再次移动50个像素到底部,您必须在Y轴上加上“0”:

i1=driver.find_elements_by_xpath("//ul[@id='sortable']/li")[0]

#Move i1 the first 50 pixels to bottom
move.drag_and_drop_by_offset(i1,0,50).perform()

#Move i1 the again 50 pixels to bottom
move.drag_and_drop_by_offset(i1,0,0).perform()  

#Move i1 the again 50 pixels to bottom
move.drag_and_drop_by_offset(i1,0,0).perform()

最好的问候

梁丘赞
2023-03-14

基于这个答案-https://stackoverflow.com/a/40171414/16452840在末尾应用. show就足够了。它只拖动Element一次。

item1= driver.find_element_by_xpath("//ul[@id='sortable']/li[text()='Item 3']")
before1 = item1.location.get('y')
item_height = item1.size.get('height')
move.drag_and_drop_by_offset(item1,item_height,item_height+10)
time.sleep(5)

item2= driver.find_element_by_xpath("//ul[@id='sortable']/li[text()='Item 1']")
before2 = item2.location.get('y')
item_height = item2.size.get('height')
move.drag_and_drop_by_offset(item2,0,item_height+10)
time.sleep(5)

item1= driver.find_element_by_xpath("//ul[@id='sortable']/li[text()='Item 3']")
before_y = item1.location.get('y')
item_height = item1.size.get('height')
move.drag_and_drop_by_offset(item1,0,item_height+10).perform()
 类似资料:
  • 问题内容: 我有一个页面使用jQuery拖放,并且我想使用Selenium为此过程构建一个相对健壮的测试套件。 查看Selenium时,我发现它在jQuery插件上有一个 拖放 命令,例如:FullCalendar,但是当我使用Selenium IDE尝试记录“拖放”时,没有任何记录的事件。 因此,我应该尝试使用jQuery选择器来定位事件吗? 由于以下操作无效(针对示例页面上的“ 12p午餐”

  • 我试图使用Selenium的Action类将JQuery嵌套可排序列表中的源元素拖放到另一个元素。我使用了标准的dragAndDrop方法,并将其分解为clickAndHold、moveToElement和release,但这两种方法都不适用。我甚至尝试使用源元素内部的子元素来拖动,但结果是一样的。 当我运行脚本时,测试返回为已通过,因此我知道正在找到元素,并且选择器有效。Selenium认为拖放

  • 我使用jQuery来满足我的拖放需求,为什么不能在这里工作是我下面的代码,我的需求是:首先,当我将图像拖到表格单元格中时,我要启用/显示一个div;然后,我要将一个跨度拖到新显示的div中,当图像被拖动时,这个跨度就不能在启用的div中拖放了。为什么需要帮助我是jQuery的新手。 null null 因此,问题是拖动图像后,组成的div块被启用/附加在表格单元格内,之后,当我们将跨距拖动到表格单

  • 问题内容: 我正在尝试遵循Zed Shaw的《困难方法学习Python》指南。我需要在Powershell中使用python。我在中安装了Python 2.7.3 。每当我在Powershell中键入python时,都会出现一个错误,指出“ python”一词无法识别为cmdlet,函数,脚本文件或可操作程序的名称。我也输入了以下内容: 提供了建议的解决方案,但是在Powershell中输入pyt

  • 问题内容: 我无法使用Python WebDriver绑定进行拖放。我正在Mac OS X上使用Google Chrome和Firefox。这里有一个线程,那里有人遇到类似问题。 我尝试使用: 您是否设法使Python WebDriver拖放工作? 问题答案: 为了给出更新的答案,我已经验证了它现在确实可以在Mac上运行。 参考

  • 我刚刚开始学习python。所以在这一章中,基本上是使用硒创建一个价格跟踪器。 下面的照片是我使用selenium试图获得的。 截图 如果我尝试 它显示了红海的线条,没有布赫的信息:没有这样的元素 然后我试着 输出为: 我真的是编程新手...所以我试着在最后使用. text,运气不好! 我怎么做才能得到价格?