selenium常用封装方法

1.下拉框的选择方法

WebElement element = driver.findElement(By.cssSelector());

Element.click();

//这是点击下拉框的定位方法

Thread.sleep(2000);//点击下拉框后等待下方元素加载

然后再找到需要选择的选项

WebElement element = driver.findElement(By.xpath());

Element.click;

 

2.日历控件处理方法

String js = "document.getElementById('foundingTime').removeAttribute('readonly')";
//移除readonly属性
((JavascriptExecutor)driver).executeScript ( js );

//通过赋值方法
driver.findElement ( By.id ( "foundingTime" ) ).sendKeys ( "2019-10-31" );

 

要导入 import org.opena.selenium.JavascriptExecutor;方法

 

3.鼠标悬停加载元素方法

WebElement xuanfu = driver.findElement ( By.xpath () );
Actions  action = new Actions(driver);
action.moveToElement ( xuanfu).perform ();
driver.findElement ( By.linkText ( "" ) ).click ();

 

4.处理弹窗

public void Tanchuan(){
    Alert alert=driver.switchTo ().alert ();
    alert.accept ();
}

 

 

5.多窗口处理

//两窗口不同关闭旧窗口,停留在新窗口

 public void Closechuankou(){
    String handle = driver.getWindowHandle();
    // 获取所有页面的句柄,并循环判断不是当前的句柄
    for (String handles : driver.getWindowHandles()) {
        if (!handles.equals(handle))
            driver.close();
        driver.switchTo().window(handles);
    }
}

//切换旧窗口

public void QholdWindow(){
    String handles = driver.getWindowHandle ();
    // 获取所有页面的句柄,并循环判断不是当前的句柄
    for (String handle : driver.getWindowHandles()) {
        if (!handle.equals(handles))
            continue;
        driver.switchTo().window(handle);
    }driver.switchTo ().window ( handles );
}

 

 

6.字符串比较

String a=new String("abc");

String b=new String("abc");

if(a.equals(b))//a.equals(b)这个返回的是一个布尔类型(bool):真为ture 假为false

{

System.out.println("OK");

}

7.表格的时候/目录树

driver.switchTo ().frame ( "treeFrame" );//通过frame逐级往下找
WebElement click = driver.findElement(By.xpath ( "//*[@id=\"treemenu\"]/li[2]" ));
click.click ();

 

遇到不同层iframe的时候,应该先返回主页面,然后再通过iframe查找元素

driver.switchTo ().defaultContent ();返回主页面

8.处理弹窗的方法

public void Tanchuan(){
    Alert alert=driver.switchTo ().alert ();
    alert.accept ();
}

9.设置浏览器窗口大小

driver.manage().window().setSize(new Dimension(1920,1080))

Driver.manage().window().maxmize();

  public void Windowset(int value,int value2){
    driver.manage().window().setSize(new Dimension (value,value2) );
}

 

 

 

 

10.查询结果进行断言的封装

public void Duanyan(String a,String value){
    WebElement element = driver.findElement ( By.xpath ( value) );
    element.getText ();
    if (element.getText ().equals ( a )){
        System.out.println ("查询结果正确="+element.getText () );
}   else {
        System.out.println ("查询结果错误="+element.getText () );
    }
}

 

11.滚动浏览器封装

public void La() throws InterruptedException {
    for (int i = 0; i < 10; i++) {
        Thread.sleep(1000);
        ((JavascriptExecutor) driver).executeScript("window.scrollTo(0,"+(i * 500)+")");
    }
}

 

 

12.上传本地文件的方法封装

public void File() throws InterruptedException, AWTException {
    StringSelection sel = new StringSelection("C:\\Users\\Administrator\\Desktop\\优秀党员\\4.jpeg");
    Toolkit.getDefaultToolkit().getSystemClipboard().setContents(sel,null);
    System.out.println("selection" +sel);
    Robot robot = new Robot();
    Thread.sleep ( 2000 );
    robot.keyPress( KeyEvent.VK_ENTER);
    // 释放回车
    robot.keyRelease(KeyEvent.VK_ENTER);
    // 按下 CTRL+V
    robot.keyPress(KeyEvent.VK_CONTROL);
    robot.keyPress(KeyEvent.VK_V);
    // 释放 CTRL+V
    robot.keyRelease(KeyEvent.VK_CONTROL);
    robot.keyRelease(KeyEvent.VK_V);
    Thread.sleep(1000);
    // 点击回车 Enter
    robot.keyPress(KeyEvent.VK_ENTER);
    robot.keyRelease(KeyEvent.VK_ENTER);
}

13.获得页面title

String title = driver.getTitle();
System.out.println(title);

 

 

14.浏览器封装

public static WebDriver driver;

static {
    try {
        driver = getDrive();
    } catch (InterruptedException e) {
        e.printStackTrace ();
    }
}public static WebDriver getDrive() throws InterruptedException {

    System.setProperty ( "webdriver.chrome.driver", "D:\\TEST\\gongju\\chromedriver.exe" );

    WebDriver driver = new ChromeDriver ();

    driver.manage ().window ().maximize ();

    driver.get ( URL );
    Thread.sleep ( 2000 );
    driver.findElement ( By.name ( "account" ) ).sendKeys ( value );
    driver.findElement ( By.name ( "password" ) ).sendKeys ( value ) ;
    driver.findElement ( By.name ( "valcode" ) ).sendKeys ( value );
    driver.findElement ( By.xpath ( 登录按钮) ).click ();
    Thread.sleep ( 2000 );
    Thread.sleep ( 5000 );
    return driver;
}

 

 

15.截图方法

public void  Screenshot(String value) throws IOException {
    //执行屏幕截图操作
    File srcFile = ((TakesScreenshot)driver).getScreenshotAs( OutputType.FILE);
    //通过FileUtils中的copyFile()方法保存getScreenshotAs()返回的文件;"屏幕截图"即时保存截图的文件夹
    try {
        FileUtils.copyFile(srcFile, new File(value));
    } catch (IOException e) {
        e.printStackTrace();
    }
}

16.窗口拉动

public void La() throws InterruptedException {
    for (int i = 0; i < 10; i++) {
        Thread.sleep(1000);
        ((JavascriptExecutor) driver).executeScript("window.scrollTo(0,"+(i * 500)+")");
    }
}

 

 

17.删除多余项

public void Delete() throws InterruptedException {
    for(int i=0;i<=8;i++){
        driver.findElement ( By.xpath ( "//*[@id=\"tr_id_1\"]/td[5]/input[2]" ) ).click ();
        Thread.sleep ( 300 );

}

}

18.调用键盘回车键

Robot robot = new Robot();

 

              robot.keyPress(KeyEvent.VK_ENTER);

              robot.keyRelease(KeyEvent.VK_ENTER);

上一篇:2021-05-27


下一篇:HDU 5673 Robot