首页 > 代码库 > java selenium webdriver处理JS操作窗口滚动条

java selenium webdriver处理JS操作窗口滚动条

未经作者允许,禁止转载!!!

java selenium webdriver处理JS操作窗口滚动条

java selenium webdriver处理JS操作窗口滚动条

import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class JS20161212 {

    public static void main(String[] args) throws InterruptedException {
        // TODO Auto-generated method stub
        System.setProperty("webdriver.chrome.driver","C:\\Training\\Automation\\Software\\drivers\\chromedriver_win32\\chromedriver.exe");
        WebDriver driver=new ChromeDriver();
        driver.get("https://www.hao123.com/");
        driver.manage().window().maximize();
        Thread.sleep(2000);
        
        JavascriptExecutor js = (JavascriptExecutor)driver;
        js.executeScript("scrollTo(0,10000)");----------------------------------------------向下拉到底
        Thread.sleep(2000);
        js.executeScript("scrollBy(0, 0-document.body.scrollHeight *99/100)");--------------向上拉到顶
        Thread.sleep(2000);
        js.executeScript("scrollTo(0,100000)");---------------------------------------------向下拉到底
        Thread.sleep(2000);
        js.executeScript("scrollTo(0,1)");--------------------------------------------------向上拉到顶
        Thread.sleep(2000);
        js.executeScript("scrollTo(0,1000)");
        Thread.sleep(2000);
        js.executeScript("scrollBy(0, 0-document.body.scrollHeight *1/2)");-----------------拉到中间
        Thread.sleep(2000);
        js.executeScript("scrollBy(0, 0-document.body.scrollWidht *1/2)");------------------左右拉到中间
        
    
//      ((JavascriptExecutor)driver).executeScript("scrollTo(0,10000)");----------------精简后的代码
//      Thread.sleep(2000);
//      ((JavascriptExecutor)driver).executeScript("scrollTo(0,1)");

    }

}

 

java selenium webdriver处理JS操作窗口滚动条

 

java selenium webdriver处理JS操作窗口滚动条