0

I am unable to click a button that's outside of the viewport on a page. I have tried a couple of things, none of which work:

  1. button.click()

  2. Actions.click(button).peform()

  3. Actions.moveToElement(button).click().build().peform()

All of the above throw a MoveTargetOutOfBoundsException.

I have tried scrolling the button into view, but these all don't do anything:

  1. ((JavascriptExecutor)driver).executeScript("window.scrollTo(0, document.body.scrollHeight)")

  2. ((JavascriptExecutor)driver).executeScript("arguments[0].scrollIntoView();", button)

  3. driver.findElement(By.tagName("body")).sendKeys(Keys.PAGE_DOWN)

I have checked the page for iframes; the page doesn't contain any. It should be noted that when I add a delay and scroll the page down manually, the button is immediately clicked when it's in view, so that leads me to believe it's not an issue with the button, but rather with the page not wanting to be scrolled down.

I cannot share the page, but I can provide snippets of the page's HTML if needed.

  • Java 16
  • Selenium 4
  • Chromedriver 99
Mattemingda
  • 61
  • 1
  • 1
  • 8

2 Answers2

0

You can try scrolling the page with method independent to that element, like

((JavascriptExecutor)driver).executeScript("window.scrollTo(0, document.body.scrollHeight)")

Checking each time for presence or visibility of that button element.
And only when this condition is met to click on that element with Selenium button.click() method or with Actions Actions.moveToElement(button).click().build().peform()

Prophet
  • 32,350
  • 22
  • 54
  • 79
  • I have already established in my original post that `((JavascriptExecutor)driver).executeScript("window.scrollTo(0, document.body.scrollHeight)")` doesn't do anything, so I don't see why this would work. – Mattemingda Mar 24 '22 at 10:04
  • OK, if so we need to understand why this doesn't scroll your page. maybe the content is inside some `div` container? – Prophet Mar 24 '22 at 10:06
  • Google something like `selenium scroll into div` – Prophet Mar 24 '22 at 10:09
0

You need to take care of a couple of things as follows:

  • button.click(): Ideally to invoke click on the element you need to induce WebDriverWait for the elementToBeClickable() which automatically scrolls the element within the viewport.

    new WebDriverWait(driver, Duration.ofMillis(20)).until(ExpectedConditions.elementToBeClickable(By.cssSelector("elementCssSelector"))).click();
    
  • new Actions(driver).click(button).perform(): Same as the previous step you may like to induce WebDriverWait for the elementToBeClickable() and build and peform as follows:

    new Actions(driver).click(new WebDriverWait(driver, Duration.ofMillis(20)).until(ExpectedConditions.elementToBeClickable(By.cssSelector("elementCssSelector")))).build().perform()
    
  • new Actions(driver).moveToElement(button).click().build().perform(): To move the focus to the element you need to induce WebDriverWait for the visibilityOfElementLocated() as follows:

    new Actions(driver).moveToElement(new WebDriverWait(driver, Duration.ofMillis(20)).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("elementCssSelector")))).click().build().perform()
    
Shaaer
  • 527
  • 5
  • 17
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352