1

WebDriver fails to find the element which is not visible in browser's visible area.In order to make the WebElement viewable by WebDriver, We need to make that element to visible in browser's view to do scroll down on particular div! I tried lot, doesn't helped me. Hence its still not working at all . Kindly advise

My Code :

((JavascriptExecutor)driver).executeScript("arguments[0].scrollIntoView();", driver.findElement(By.xpath("//*[@id='lobbyMain']/div[3]/div[2]/ul/li[1]/div[1]/h3/a"))).onclick;
Jayden Ng
  • 141
  • 1
  • 1
  • 14

2 Answers2

0

See the basic way to scroll is:

Webelement element = driver.findElement(By.xpath("//*[@id='lobbyMain']/div[3]/div[2]/ul/li[1]/div[1]/h3/a"))

JavascriptExecutor js = (JavascriptExecutor) element;
    int yPosition = element.getLocation().getY();

    for (int second = 0;; second++) {
        if(second >=4){
            break;
        }
        ((JavascriptExecutor) driver).executeScript("window.scrollBy(0,200)", ""); //y value '400' can be altered
        Thread.sleep(3000);

You can vary seconds as per your convenience.

Or else Just refer to below link. It will help you.

https://www.seleniumeasy.com/selenium-tutorials/scrolling-web-page-with-selenium-webdriver-using-java

If you still face problem, then do reply me. :-)

Kishan Patel
  • 1,385
  • 3
  • 13
  • 24
0

Selenium will automatically scroll to the element for you when you perform a click on an element. You can simply do:

driver.findElement(By.xpath("//*[@id='lobbyMain']/div[3]/div[2]/ul/li[1]/div[1]/h3/a")).click()

And selenium should find it, scroll to it and click on it. If this does not work, I sometimes use this to scroll:

((JavascriptExecutor) driver).executeScript(
            "scroll(" + element.getLocation().getX() + "," + element.getLocation().getY() + ")");
Mobrockers
  • 2,128
  • 1
  • 16
  • 28