My problem is to scroll the scroll bar horizontally which is inside window I used this code but it scroll the windows horizontal bar not that exactly scroll bar inside that window.
WebElement scroll = driver.findElement(By.xpath("//div[@id='gvLocationHorizontalRail']"));
JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("window.scrollBy(250,0)", "");
You can move the contents of windows that have horizontal scroll bars to the left or right using the keyboard and the mouse. Move contents to the right: Press the Shift key and scroll the mouse wheel up. Move contents to the left: Press the Shift key and scroll the mouse wheel down.
Java Prime Pack JScrollPane(Component view) − To create a scrollPane on a component. JScrollPane. setHorizontalScrollBarPolicy(JScrollPane. HORIZONTAL_SCROLLBAR_ALWAYS) − To show a horizontal bar always.
executeScript("window. scrollBy(250,0)", ""); java. selenium.
Horizontal scrolling can be achieved by clicking and dragging a horizontal scroll bar, swiping sideways on a desktop trackpad or trackpad mouse, pressing left and right arrow keys, or swiping sideways with one's finger on a touchscreen.
You are using javascript that scrolls the main window, if you want to scroll a element, you should first get the element by id, then change the its scrollLeft
property:
JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript(
"document.getElementById('gvLocationHorizontalRail').scrollLeft += 250", "");
If you instead want to change the scrollbar that moves up and down, you should change the scrollTop
property.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With