Java+Selenium——利用Javascript处理滚动条——利用webdriver执行脚本直接滚动到指定坐标位置

package rjcs;

import java.io.File;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.Point;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.firefox.FirefoxDriver;

import org.openqa.selenium.Dimension;
import java.text.SimpleDateFormat;
import org.openqa.selenium.*;


public class zmjsj 
{
    
    public static void main(String[] args)
    
    {
         System.setProperty("webdriver.firefox.bin","C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe");    //设置火狐的安装路径,防止系统找不到
            
         FirefoxDriver driver = new FirefoxDriver();        //初始化FireFox浏览器实例,并打开浏览器
         
        try
        {
             driver.manage().window().maximize();         //最大化窗口
             
             Thread.sleep(3000);        
             
             driver.manage().window().maximize();         //最大化窗口
             
             //driver.manage().window().setSize(new Dimension(400,600));
             Dimension targetSize = new Dimension(400,600); 
              driver.manage().window().setSize(targetSize);
              
              System.out.println(driver.manage().window().getSize());
              
              
              
             Thread.sleep(5000);

             driver.get("https://www.baidu.com/");  
             
             
             
             Thread.sleep(5000);
             
             ((JavascriptExecutor)driver).executeScript("scrollTo(0,300)");
             
             
             Thread.sleep(5000);
             
             ((JavascriptExecutor)driver).executeScript("scrollTo(600,600)");
             
             
              Thread.sleep(10000);
             
        }catch (Exception e) 
        {
            e.printStackTrace();
        }finally 
        {
            driver.quit();
        
         }
   }
    

}
posted @ 2020-01-20 18:45  小白龙白龙马  阅读(519)  评论(0编辑  收藏  举报