Java+Selenium——window接口相关方法介绍

windows接口下有几个方法,主要是用来控制浏览器窗口设置,例如窗口大小,最大,最小,全屏,位置。我们先来看看window接口下有什么方法。

void setSize(Dimension targetSize);
void setPosition(Point targetPosition);
Dimension getSize();
Point getPosition();
void maximize();
void fullscreen();

============================================================================

 

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(3000);
             
             driver.get("https://www.baidu.com/");  
             
             Thread.sleep(3000);
             

             

             
              Thread.sleep(10000);
             
        }catch (Exception e) 
        {
            e.printStackTrace();
        }finally 
        {
            driver.quit();
        
         }
   }
    

}

 

 

 

 

======================================================================================================

 

 

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);
             
             
             Point targetPosition = new Point(500, 500);         //设置窗口左上角那个点出现的位置为(300,600)
             driver.manage().window().setPosition(targetPosition);
             System.out.println(driver.manage().window().getPosition());
             
             Thread.sleep(5000);
             
             driver.get("https://www.baidu.com/");  
             
             Thread.sleep(3000);
             

             

             
              Thread.sleep(10000);
             
        }catch (Exception e) 
        {
            e.printStackTrace();
        }finally 
        {
            driver.quit();
        
         }
   }
    

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