How do I scroll down vertically in a specific div in a web page

前端 未结 4 2034
情书的邮戳
情书的邮戳 2021-01-22 18:48

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

相关标签:
4条回答
  • 2021-01-22 19:00

    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);
    
    0 讨论(0)
  • 2021-01-22 19:02

    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\" })");
    
    0 讨论(0)
  • 2021-01-22 19:21

    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.

    0 讨论(0)
  • 2021-01-22 19:24

    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

    0 讨论(0)
提交回复
热议问题