Home > Software engineering >  executeScript() method returns me null instead of scrolling horizontally towards right
executeScript() method returns me null instead of scrolling horizontally towards right

Time:01-24

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'].

CodePudding user response:

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)

  • Related