2

I have searched all the forums but I didn't get a correct answer for my issue. My web page to test has a link hidden below, and I am trying to find it manually by searching for it with xpath or the ID attribute of the element, but I am not able to find it when I am running the web driver script. Even when it is not giving any error on that element, I am getting an error on next command/line.

I found below code from the forums, which is scrolling whole page. I don't want this, I want to scroll down vertically in a specific div area as in screen shot.

JavascriptExecutor jsx = (JavascriptExecutor)driver;
jsx.executeScript("ctl00_Menu1_scrollDiv.scrollBy(0,250)", "");

enter image description here

enter image description here

enter image description here

div id for this is "ctl00_Menu1_scrollDiv"
Element id: ctl00_Menu1_DlMenu_ctl09_LnkMenuname

Please help me on this issue. Thanks in advance.

Help will be appreciated.

4
  • is keeping a frame inside the div an option ?? Commented Sep 5, 2013 at 12:05
  • Thanks for the reply. Inside div they have used a table. please find the attached new screen shot Code.jpg Commented Sep 5, 2013 at 12:15
  • how does the table creates a prob ?? keep the iframe inside div , and the table inside iframe Commented Sep 5, 2013 at 12:17
  • 1
    Why in the world would you use an iframe? Bad advice there. Commented Sep 5, 2013 at 12:30

4 Answers 4

3

This is umendra tomar , I have found very simple solution for this, please use the below code for scroll a specific div in html using selenium .

JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("document.getElementById('scrollCustom').scrollTop= 450");

scrollCustom =  this is the ID of your div which you want to scroll.
document.getElementById =  this is use in javascript to locate a webelement. 

Don't worry we can use this in java using javascriptExecutor

Sign up to request clarification or add additional context in comments.

Comments

2

Check if this works for you.

var s = $('#ctl00_Menu1_scrollDiv').scrollTop(); 

This will give the current value of the scroll in the div.Use this only if you want to scroll inside a div to a certain point dynamically. Otherwise you can hardcode the scrollTop value inside animate()

Using the current value of your scroll you can parameterize the given below scrollTop parameter

$("#ctl00_Menu1_scrollDiv").animate({ scrollTop: "100px" }); // Here 100 px is just an example

I had used this to scroll a large div programmatically in my webdriver framework. Also, this will work if your AUT has jQuery loaded in the browser.

In Java:

JavascriptExecutor js;
js = (JavascriptExecutor) driver;
js.executeScript("$(\"#ctl00_Menu1_scrollDiv\").animate({ scrollTop: \"100px\" })");

4 Comments

Are you asking for native webdriver code (In java) to perform scroll? I fear it's not possible. You will HAVE to use JavascriptExecutor.
@UmamaheshwarThota Please consider accepting the answer if any of them help.
Sure Abhijeet Vaikar.
Thanks Abhijeet Vaikar for your answer. i was asking you the same code you have given second time in Java. It worked for me. Thanks again. JavascriptExecutor js; js = (JavascriptExecutor) driver; js.executeScript("$(\"#ctl00_Menu1_scrollDiv\").animate({ scrollTop: \"100px\" })");
0

First you should not just reference an element by the id. You should set scrollTop to scroll it to a position.

document.getElementById("ctl00_Menu1_scrollDiv").scrollTop(250);

1 Comment

Thanks for the reply, but can you please give code in java, how to use this above statement.
0

Non-JQuery solution based on this post.

((JavascriptExecutor) driver).executeScript(
    "arguments[0].scrollTop=arguments[1].offsetTop",
    divWithScrollbarElement,
    elementToScrollTo);

where divWithScrollbarElement is the div element which you are looking to scroll, and elementToScrollTo is the child element which you want to make viewable (which in my case was actually the parent of the element which I was initially trying to view). If elementToScrollTo is not actually in the DOM yet, you may need to use the script once to scroll down as far as possible, and then again once more elements have loaded.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.