• webdriver-键盘操作 for java


     

    import java.awt.AWTException;
    import java.awt.Robot;
    import java.awt.event.InputEvent;
    import org.openqa.selenium.By;
    import org.openqa.selenium.Keys;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.firefox.FirefoxDriver;
    import org.openqa.selenium.interactions.Actions;


    public class selenium_keyboard_action {
    /*Keys类提供了键盘的相关方法,sendKeys模拟键盘输入,可以输入键盘上的按键,也可以模拟组合键*/
    WebDriver driver=new FirefoxDriver();
    Actions action=new Actions(driver);
    public static void main(String[] args) throws Exception {
    selenium_keyboard_action ska=new selenium_keyboard_action();
    ska.init();
    //ska.test1();
    //ska.test2();
    ska.robotUse();
    }
    public void init(){
    driver.get("http://www.baidu.com/");
    }
    public void test1(){
    WebElement element=driver.findElement(By.id("kw"));
    element.sendKeys("test");//A向百度搜索框中输入test
    element.sendKeys(Keys.BACK_SPACE);//删除最后一个t
    element.sendKeys(Keys.SPACE);//空格键
    element.sendKeys("ting");//输入ting
    element.sendKeys(Keys.CONTROL,"a");//ctrl+a
    element.sendKeys(Keys.CONTROL,"x");//ctrl+x
    element.sendKeys(Keys.CONTROL,"v");//ctrlv
    element.sendKeys(Keys.ENTER);//ctrl+a
    }
    public void test2() throws Exception{//对右键菜单使用快捷键
    action.contextClick(driver.findElement(By.id("su"))).perform();
    Thread.sleep(2000);
    //action.sendKeys("v").perform();//模拟键盘输入v
    action.sendKeys(Keys.ARROW_DOWN).perform();//通过键盘按3次向下的箭头
    action.sendKeys(Keys.ARROW_DOWN).perform();
    action.sendKeys(Keys.ARROW_DOWN).perform();
    action.sendKeys(Keys.ENTER).perform();//按回车键
    }
    public void robotUse() throws Exception{//使用robot模拟鼠标键盘的操作
    action.moveToElement(driver.findElement(By.linkText("登录")));
    try {
    Thread.sleep(2000);
    Robot r=new Robot();
    //r.mousePress(InputEvent.BUTTON1_MASK);//按下鼠标左键
    //r.mouseRelease(InputEvent.BUTTON1_MASK);
    r.keyPress(KeyEvent.VK_DOWN);//在下拉菜单中点击一次键盘的向下键
    r.keyRelease(KeyEvent.VK_DOWN);

    r.keyPress(KeyEvent.VK_ENTER);//按下回车键
    r.keyRelease(KeyEvent.VK_ENTER);//释放回车键
    } catch (AWTException e) {
    e.printStackTrace();
    }
    }
    }
  • 相关阅读:
    Taro文件上传:Blob Url下载Blob对象本身并通过接口上传到服务器
    Taro项目遇到的问题
    RPC是什么?
    句柄
    正向代理和反向代理
    Go Micro搭建简单微服务
    gRPC奇怪的编译命令protoc
    官网下载Git方法
    Go/golang:解决依赖包模块安装问题
    URL
  • 原文地址:https://www.cnblogs.com/51testing/p/9397755.html
Copyright © 2020-2023  润新知