How to scroll in Selenium?

 

Answer: One cannot scroll in selenium, for that you have to use JavaScriptExecutor class:

1.Using scrollBy method in JavaScriptExecutor:

    main(){
            
                System.setProperty("webdriver.chrome.driver", "C:\\Users\\Krunal.Kadu\\Desktop\\Krunal\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("http://practice.automationtesting.in/");

JavaScriptExecutor js = (driver)JavaScriptExecutor; // cast driver to jsexecutor type
                                                                   js.executeScript("arguements[0].scrollBy(0,250);",driver.findElement(By.xpath("//div/input[1]")));

            }

2. Using scrollIntoViewMethod in JavaScriptExecutor:

    js.executeScript("arguements[0].scrollIntoView(true);",driver.findElement(By.xpath("//div/input[1]")));

Interview Question: Suppose I land on a page but the Webdriver is trying to find an element which is not visible to the eye, one has to scroll down to the element to view it. How will you handle this situation?

Comments

Popular posts from this blog

Jenkins CICD in One Page

Why do we need a build tool?

Deutsche Bank Interview Questions - 2024