Selenium Python:move#u to#u元素不';别动

Selenium Python:move#u to#u元素不';别动,python,selenium,selenium-webdriver,Python,Selenium,Selenium Webdriver,我目前正在编写一个脚本,自动完成游戏 随着游戏的进行,已经发现的物品库不断增加,最终你必须滚动到一些较低的物品。 当我试图抓取的项目不在屏幕上时,Selenium抛出异常MoveTargetOutOfBoundsException 我试图通过捕获异常,然后使用move\u to\u element将该项带到Selenium来修复此问题。但当我尝试时,什么也没发生。有什么想法吗 try: ActionChains(driver).drag_and_drop(elempic2

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

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

我试图通过捕获异常,然后使用
move\u to\u 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))
引发的错误:

Traceback (most recent call last):
  File "littlealch.py", line 60, in findelements
    ActionChains(driver).drag_and_drop(elempic2, workspace).perform()
  File "/home/joco/.local/lib/python3.6/site-packages/selenium/webdriver/common/action_chains.py", line 80, in perform
    self.w3c_actions.perform()
  File "/home/joco/.local/lib/python3.6/site-packages/selenium/webdriver/common/actions/action_builder.py", line 76, in perform
    self.driver.execute(Command.W3C_ACTIONS, enc)
  File "/home/joco/.local/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "/home/joco/.local/lib/python3.6/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.MoveTargetOutOfBoundsException: Message: (1624, 957.5) is out of bounds of viewport width (1920) and height (942)
如果你想亲自尝试,那就是回购协议


感谢您的帮助:)

我会将滚动逻辑转换为不需要目标元素出现在视图中的逻辑,如下所示:

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向下滚动页面,直到可以执行工作。

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

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向下滚动页面,直到可以执行工作为止