How to scroll the scrollbar horizontally inside the window using java

My problem is to scroll the scroll bar horizontally that is inside the window. I used this code, but it scrolls the horizontal strip of windows, and not exactly scrolls the panel inside this window.

    WebElement scroll = driver.findElement(By.xpath("//div[@id='gvLocationHorizontalRail']"));
    JavascriptExecutor js = (JavascriptExecutor)driver;
    js.executeScript("window.scrollBy(250,0)", "");
+4
source share
4 answers

You are using javascript, which scrolls the main window, if you want to scroll the element, you must first get the element by id, and then change its property scrollLeft:

JavascriptExecutor js = (JavascriptExecutor)driver; 
js.executeScript(
    "document.getElementById('gvLocationHorizontalRail').scrollLeft += 250", "");

If you want to change the scroll bar that moves up and down, you must change the property scrollTop.

+4
source
In addition to answer by Ferrybig, can you try this:

WebElement scroll = driver.findElement(By.xpath("//div[@id='gvLocationHorizontalRail']"));
JavascriptExecutor js = (JavascriptExecutor)driver; 
js.executeScript(
driver.execute_script("arguments[0].scrollIntoView()", scroll);
+4

:

WebElement problematicElement= driver.findElement(By.xpath("//div[@id='blah']"));
(JavascriptExecutor)driver.executeScript("arguments[0].scrollIntoView()", problematicElement);
+3

You do not need to use Javascript for this. I had problems with solutions working the second, third, and fourth time the page was loaded. The code below works and always works without errors. It moves the scroll bar to the right. I broke it because I prefer to see each action on its own for readability.

            myElement = (new WebDriverWait(driver, 30))
                    .until(ExpectedConditions.elementToBeClickable(By.cssSelector(".ngscroll-scrollbar")));
            myElement.click();
            Actions move = new Actions(driver);
            move.moveToElement(myElement).clickAndHold();
            move.moveByOffset(125,0);
            move.release();
            move.perform();
0
source

Source: https://habr.com/ru/post/1629189/


All Articles