Search code examples
javascriptjavaseleniumselenium-webdriverscroll

executeScript() method returns me null instead of scrolling horizontally towards right


The code is:

WebElement element=driver.findElement(By.xpath("//div[@class='table-wrapper']"));
JavascriptExecutor js=(JavascriptExecutor)driver;
js.executeScript("arguments[0].scrollLeft=arguments[0].scrollWidth", element);

While debugging at third step, executeScript returned me "null".

Xpath is correct and locating well. I can't able to find locator of scroll so I am using whole locator of that particular div. i.e //div[@class='table-wrapper'].


Solution

  • Instead of scrollLeft you can always Element.scrollIntoView() as follows:

    WebElement element=driver.findElement(By.xpath("//div[@class='table-wrapper']"));
    ((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", element);
    

    You can find a relevant detailed discussion in scrollIntoView() not working for horizontal scroll (Selenium)