1

I have ran into a problem while trying to run the following script:

def searchByBrand(brand):
    try:
        driver.implicitly_wait(20)
        action = ActionChains(driver)
        cat_button = driver.find_element_by_xpath(
                "//button[@class='icon-btn icon-btn--no-text icon-btn--primary']"
                )
        driver.implicitly_wait(10)

        CHECKBOX_XPATH = "//input[@type='checkbox' and @aria-label='Acer']"
        checkbox = driver.find_element_by_xpath(CHECKBOX_XPATH)

        # Clicks the "Filter by" button
        action.move_to_element(cat_button).click().perform()

        # Waits until checkbox is present, then waits until it's visible
        # and then clicks it
        wait = WebDriverWait(driver, 20).until(
                EC.visibility_of_element_located((By.XPATH,CHECKBOX_XPATH)))

        driver.execute_script("document.body.style.transform='scale(0.5)';")
        time.sleep(3)
        driver.execute_script("arguments[0].scrollIntoView();", wait)
        time.sleep(3)
        wait = WebDriverWait(driver, 20).until(
                EC.element_to_be_clickable((By.XPATH,CHECKBOX_XPATH)))

        action.move_to_element(wait).click().perform()

    finally:
        print("Filtered successfully.")

This is the output after running it:

o@o-pc:~$ python selenium_example.py
Filtered successfully.
Traceback (most recent call last):
  File "selenium_example.py", line 86, in <module>
    main()
  File "selenium_example.py", line 82, in main
    searchByBrand("Acer")
  File "selenium_example.py", line 77, in searchByBrand
    action.move_to_element(wait).click().perform()
  File "/home/o/.pyenv/versions/3.8.3/lib/python3.8/site-packages/selenium/webdriver/common/action_chains.py", line 80, in perform
    self.w3c_actions.perform()
  File "/home/o/.pyenv/versions/3.8.3/lib/python3.8/site-packages/selenium/webdriver/common/actions/action_builder.py", line 76, in perform
    self.driver.execute(Command.W3C_ACTIONS, enc)
  File "/home/o/.pyenv/versions/3.8.3/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "/home/o/.pyenv/versions/3.8.3/lib/python3.8/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.MoveTargetOutOfBoundsException: Message: (304, -148) is out of bounds of viewport width (681) and height (669)

I tried the solutions proposed in this question: Selenium - MoveTargetOutOfBoundsException with Firefox

I didn't quite get the "accepted" answer. I also tried zooming out and putting sleeps in between actions/scripts; didn't work. Haven't tried rezising the Firefox window since I'm using a tiling windows manager (I don't know if that affects the performance of my script).

EDIT: I tried the same script not using i3 (the tiling windows manager I mentioned), commented

action.move_to_element(cat_button).click().perform()

And it worked. Any thoughts?

  • `zooming in/out` and _putting sleeps_ aren't really solutions to address `MoveTargetOutOfBoundsException`. Do you have a question on the _"accepted" answer_? – undetected Selenium Jun 15 '20 at 08:46
  • That's what the other users recommended. And yes, I do have a question. Since the issue is the viewport (from what I understand is "the user's visible area of a web page."), and I can see the checkbox; why does the script still not work? – F. Carratto Jun 15 '20 at 08:53
  • As far as the _"accepted" answer_ is concerned, if you look at the _xpath_, it contains `footer` which is bound to be at the bottom of the page, which gives me the license to scroll `document.body.scrollHeight` – undetected Selenium Jun 15 '20 at 08:59
  • What do you mean by that? I think I already had the "license" to scroll since `driver.execute_script("arguments[0].scrollIntoView();", wait)` worked just fine. I still don't get why the checkbox is out of the viewport. Doesn't make much sense to me – F. Carratto Jun 15 '20 at 09:15
  • just use click() function of webelement. It'll do everything you want... scroll, move, click... in one efficient method call. – pcalkins Jun 15 '20 at 20:51

0 Answers0