How to scroll a specific DIV using Selenium WebDriver with Java?

前端 未结 10 1055
后悔当初
后悔当初 2020-12-02 18:41

Some of WebElements are not recognized by WebDriver, WebDriver fails to find the element which is not visible in browser\'s visible area.

In order to make the WebEle

相关标签:
10条回答
  • 2020-12-02 19:07

    Scroll Down:

    import org.openqa.selenium.JavascriptExecutor;
    WebDriver driver = new FirefoxDriver();
    JavascriptExecutor jse = (JavascriptExecutor)driver;
    jse.executeScript("scroll(0, 250)"); //y value '250' can be altered
    

    Scroll up:

    JavascriptExecutor jse = (JavascriptExecutor)driver;
    jse.executeScript("scroll(250, 0)"); //x value '250' can be altered
    

    Scroll bottom of the Page:

    JavascriptExecutor jse = (JavascriptExecutor)driver;
    jse.executeScript("window.scrollTo(0,Math.max(document.documentElement.scrollHeight,document.body.scrollHeight,document.documentElement.clientHeight));");
    

    or

    Actions actions = new Actions(driver);
    actions.keyDown(Keys.CONTROL).sendKeys(Keys.END).perform();
    

    Full scroll to bottom in slow motion:

    for (int second = 0;; second++) {
        if(second >=60){
            break;
        }
        ((JavascriptExecutor) driver).executeScript("window.scrollBy(0,400)", ""); //y value '400' can be altered
        Thread.sleep(3000);
    }
    

    or

    JavascriptExecutor jse = (JavascriptExecutor)driver;
    for (int second = 0;; second++) {
        if(second >=60){
            break;
        }
        jse.executeScript("window.scrollBy(0,800)", ""); //y value '800' can be altered
        Thread.sleep(3000);
    }
    

    Scroll automatically to your WebElement:

    Point hoverItem =driver.findElement(By.xpath("Value")).getLocation();
    ((JavascriptExecutor)driver).executeScript("return window.title;");    
    Thread.sleep(6000);
    ((JavascriptExecutor)driver).executeScript("window.scrollBy(0,"+(hoverItem.getY())+");"); 
    // Adjust your page view by making changes right over here (hoverItem.getY()-400)
    

    or

    ((JavascriptExecutor)driver).executeScript("arguments[0].scrollIntoView();", driver.findElement(By.xpath("Value')]")));
    

    or

    WebElement element = driver.findElement(By.xpath("Value"));
    Coordinates coordinate = ((Locatable)element).getCoordinates(); 
    coordinate.onPage(); 
    coordinate.inViewPort();
    
    0 讨论(0)
  • 2020-12-02 19:07

    Consider your HTML is like below:

    <div id="someId" class="className" style="position: relative; top: 0px; left: opx;>
    

    you can observe style attribute in div check for the value of top which is 0px in above example

    Now try to do something like:

    $('.className').animate({top: "-60px"});
    

    which will help you to scroll down. Its a JavaScript executor so of course you need to implement it.

    0 讨论(0)
  • 2020-12-02 19:11

    The easiest way to do that is executing a Javascript to scroll the element up/down.

    JavascriptExecutor jsExec = (JavascriptExecutor) driver;
    jsExec.executeScript("document.getElementById('id').scrollDown += 100");
    
    0 讨论(0)
  • 2020-12-02 19:12

    My 'WORKAROUND' is to scroll to the position by element's x and y co-ordinates. I also added an offset to y so that any header/footer or other element don't block the visibility of the element that I want to scroll to.

    I have my answer posted under this question -

    Selenium webdriver can't click on a link outside the page

    0 讨论(0)
  • 2020-12-02 19:13

    Another way of doing it using JavascriptExceutor's scrollIntoView() method:

    WebElement DIVelement = driver.findElement(By.xpath("xpath to div"));
    
    JavascriptExecutor jse = (JavascriptExecutor)driver;
    jse.executeScript("arguments[0].scrollIntoView(true)", DIVelement);
    
    0 讨论(0)
  • 2020-12-02 19:17
    driver.get("http://www.***.com/");
    driver.manage().window().maximize();
    WebElement scroll = driver.findElement(By.id("someId"));
    scroll.sendKeys(Keys.PAGE_DOWN);
    
    0 讨论(0)
提交回复
热议问题