0

I am currently working on a Script that completes the game little alchemy 2 automatically.

As the game progresses the library of the already discovered Items grows and eventually you have to scroll to get to some of the lower ones. Selenium throws the exception MoveTargetOutOfBoundsException when the Item I try to grab is not on the screen.

I tried fixing this by catching the exception and then using move_to_element to bring the Item in reach of Selenium. But when I tried it out nothing happened. Any Ideas?

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))

Error that gets thrown:

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)

If you want to try it out yourself here is the repo.

Thank you for your Help :)

Jocomol
  • 303
  • 6
  • 18

1 Answers1

2

I would convert the scrolling logic to something that doesn't require the target element to be in view, like something along these lines:

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))

This just uses JavaScript to scroll down the page until the work can be performed.

duhaime
  • 25,611
  • 17
  • 169
  • 224