Selenium Python:move_to_元素不是m

2024-06-16 09:28:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我目前正在编写一个脚本,自动完成游戏little alchemy 2。在

随着游戏的进行,已经发现的物品库会不断增加,最终你必须滚动到一些较低的物品。 当我尝试获取的项目不在屏幕上时,Selenium抛出异常MoveTargetOutOfBoundsException。在

我尝试通过捕捉异常并使用move_to_element将该项带到Selenium的范围内来解决这个问题。但当我试了一下,什么也没发生。有什么想法吗?在

try:
            ActionChains(driver).drag_and_drop(elempic2, workspace).perform()
        except MoveTargetOutOfBoundsException:
            print ("Need to Scroll because if element 2")
            ActionChains(driver).move_to_element(elempic2).perform()
            ActionChains(driver).drag_and_drop(elempic2, workspace).perform()
        print ("Element 2 dragged: " + str(elemname2.text))

引发的错误

^{pr2}$

如果你想自己试一试,here就是回购。在

感谢您的帮助:)


Tags: andto游戏movedriverseleniumelement物品
1条回答
网友
1楼 · 发布于 2024-06-16 09:28:18

我会将滚动逻辑转换为不需要目标元素在视图中的内容,例如:

y_position = 0
searching = True

while searching:
  try:
    ActionChains(driver).drag_and_drop(elempic2, workspace).perform()
    searching = False
  except MoveTargetOutOfBoundsException:
    y_position += 500
    driver.execute_script('window.scrollTo(0, ' + str(y_position) + ');')
  print ('Element 2 dragged: ' + str(elemname2.text))

这只是使用JavaScript向下滚动页面,直到可以执行工作为止。在

相关问题 更多 >