Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to scroll scrollbar horizontally which is inside a window using java

Tags:

java

selenium

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)", "");
like image 651
Srikanth Avatar asked Feb 17 '16 13:02

Srikanth


People also ask

How do I scroll horizontally in Windows?

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.

How do I add a horizontal scroll bar in Java?

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.

How do you scroll scrollbar horizontal which is inside a window using Selenium?

executeScript("window. scrollBy(250,0)", ""); java. selenium.

How do you use the scroll wheel horizontally?

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.


2 Answers

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.

like image 130
Ferrybig Avatar answered Oct 19 '22 22:10

Ferrybig


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);
like image 25
Pankaj Kumar Katiyar Avatar answered Oct 19 '22 22:10

Pankaj Kumar Katiyar