1. SendKeys
package com.my.simple;
import java.io.File;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class SendKeys {
public static void main(String[] args) throws InterruptedException {
WebDriver dr = new ChromeDriver();
File file = new File("src/send_keys.html");
String filePath = "file:///" + file.getAbsolutePath();
System.out.printf("访问 %s \n", filePath);
dr.get(filePath);
Thread.sleep(1000);
// copy content of A
dr.findElement(By.id("A")).sendKeys(Keys.chord(Keys.CONTROL + "a"));
Thread.sleep(1000);
dr.findElement(By.id("A")).sendKeys(Keys.chord(Keys.CONTROL + "x"));
// paste to B
dr.findElement(By.id("B")).sendKeys(Keys.chord(Keys.CONTROL + "v"));
// SendKeys to A
dr.findElement(By.id("A")).sendKeys(Keys.chord("测试复制黏贴"));
Thread.sleep(1000);
System.out.println("浏览器即将关闭");
dr.quit();
}
}
E:\20200719-WEB自动化实战\200719UI\Webdriver_Lesson200719\src\send_keys.html
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<title>send keys</title>
<body>
<h3>send keys</h3>
<div class="row-fluid">
<div class="span3">
<div class="well">
<label>A</label>
<textarea rows="10" , cols="10" id="A">test Sendkey api!</textarea>
</div>
</div>
<div class="span3">
<div class="well">
<label>B</label>
<textarea rows="10" , cols="10" id="B"></textarea>
</div>
</div>
</div>
</body>
</html>
2. Action
package com.my.simple;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
public class Action { // 处理鼠标键盘操作的。
public static void main(String[] args) throws InterruptedException {
WebDriver dr = new ChromeDriver();
dr.get("");
Thread.sleep(2000);
// 1、鼠标左键点击:
Actions action = new Actions(dr);
action.click(dr.findElement(By.xpath("xpath")));// 等价于 driver.findElement(By.xpath(xpath)).click();
// 2、鼠标左键双击:
action.doubleClick(dr.findElement(By.xpath("xpath")));
// 3、鼠标左键按下操作:
action.clickAndHold(dr.findElement(By.xpath("xpath")));
// 4、鼠标左键移动到元素操作:
action.moveToElement(dr.findElement(By.xpath("xpath")));
// 5、鼠标右键点击操作:
action.contextClick(dr.findElement(By.xpath("xpath")));
// 6、组合的鼠标操作(将目标元素拖拽到指定的元素上):
action.dragAndDrop(dr.findElement(By.xpath("xpath")), dr.findElement(By.xpath("xpath")));
// 7、组合的鼠标操作(将目标元素拖拽到指定的位置上):
action.dragAndDropBy(dr.findElement(By.xpath("xpath")), 0, 100); // 其中 0为xOffset 为横坐标,100为yOffset 为纵坐标。
// 8.拖拽选择操作,鼠标按住不放,进行拖拽选择,然后释放鼠标,需要三个动作
action.clickAndHold(dr.findElement(By.id("1"))).moveToElement(dr.findElement(By.id("3"))).perform();
action.release();
// 键盘的模拟操作,包括普通按键,比如enter、backspace、tab等,还包括四个修饰键(Modifier Keys),分别是Caps
// Lock,Control,Option,Command。
// 普通按键使用时,直接使用sendkeys(theKeys)就可以,如按下enter键:
action.sendKeys(Keys.ENTER).perform();
// 使用快捷键alt+f4关闭窗口(但是该方法不稳定,时行时不行,不行居多)
action.keyDown(Keys.ALT).keyDown(Keys.F4).keyUp(Keys.ALT).perform();
// 使用ctrl+a全选
action.sendKeys(Keys.CONTROL + "a").perform();
dr.quit();
}
}
3. Alert弹出框
package com.my.simple;
import java.io.File;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class AlertExample {
public static void main(String[] args) throws InterruptedException {
WebDriver dr = new ChromeDriver();
File file = new File("src/alert_1.html");
String filePath = "file:///" + file.getAbsolutePath();
System.out.printf("访问 %s \n", filePath);
dr.get(filePath);
Thread.sleep(1000);
// 点击链接弹出alert
dr.findElement(By.cssSelector("input[type=\"button\"]")).click();
Thread.sleep(1000);
Alert alert = dr.switchTo().alert();
alert.accept(); //弹出窗口点击确认
Thread.sleep(1000);
System.out.println("浏览器即将关闭");
dr.quit();
}
}
<html>
<head>
<script type="text/javascript">
function display_alert()
{
alert("I am an alert box!!")
}
</script>
</head>
<body>
<input type="button" onclick="display_alert()"
value="Display alert box" />
</body>
</html>
4. Attribute(获取属性)
package com.my.simple;
import java.io.File;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class Attribute {
public static void main(String[] args) throws InterruptedException {
WebDriver dr = new ChromeDriver();
File file = new File("src/attribute.html");
String filePath = "file:///" + file.getAbsolutePath();
System.out.printf("访问 %s \n", filePath);
dr.get(filePath);
Thread.sleep(1000);
WebElement link = dr.findElement(By.id("tooltip"));
// 获得tooltip的内容
System.out.println("title:"+link.getAttribute("title")); //具体查看js dom对象的属性
//System.out.println(link.getAttribute("data-original-title")); //html5
// 获取该链接的text
System.out.println("getText:"+link.getText());
Thread.sleep(1000);
System.out.println("浏览器即将关闭");
dr.quit();
}
}
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<title>attribute</title>
</head>
<body>
<h3>attribute</h3>
<div class="row-fluid">
<div class="span6">
<a id="tooltip" href="#" data-toggle="tooltip"
title="watir-webdriver better than selenium-webdriver">hover to
see tooltip
</a>
</div>
</div>
</body>
</html>
5. ForwardAndBack(浏览器的前进后退操作)
package com.my.simple;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class ForwardAndBack { //浏览器的前进后退操作
public static void main(String[] args) throws InterruptedException {
WebDriver dr = new ChromeDriver();
dr.manage().window().maximize();
Thread.sleep(2000);
String firstUrl = "https://www.baidu.com";
System.out.printf("访问 %s \n", firstUrl);
dr.get(firstUrl);
Thread.sleep(1000);
String secondUrl = "http://www.soso.com";
System.out.printf("访问 %s \n", secondUrl);
dr.get(secondUrl);
Thread.sleep(1000);
System.out.printf("回退到 %s \n", firstUrl);
dr.navigate().back(); //浏览器回退
Thread.sleep(1000);
System.out.printf("前进到 %s \n", secondUrl);
dr.navigate().forward(); //浏览器前进
Thread.sleep(1000);
System.out.println("浏览器即将关闭");
dr.quit();
}
}
6. Frame
要特别注意的一点,在实际工作中,遇到元素莫名奇妙无法识别的时候,就需要看html 源码了,看一下元素是否在某个frame中,如果是则需要先切换到对应的frame里
package com.my.simple;
import java.io.File;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class Frame {
public static void main(String[] args) throws InterruptedException {
WebDriver dr = new ChromeDriver();
File file = new File("src/frame.html");
String filePath = "file:///" + file.getAbsolutePath();
System.out.printf("访问 %s \n", filePath);
dr.get(filePath);
Thread.sleep(1000);
//dr.findElement(By.id("kw")).sendKeys("Test");
// 先到f1再到f2
dr.switchTo().frame("f1");
dr.switchTo().frame("f2");
// 往f2中的百度关键字文本框中输入内容
dr.findElement(By.id("kw")).sendKeys("Test");
Thread.sleep(1000);
// 直接跳出所有frame
dr.switchTo().defaultContent();
// 再到f1
dr.switchTo().frame("f1");
dr.findElement(By.linkText("click")).click();
Thread.sleep(1000);
System.out.println("浏览器即将关闭");
dr.quit();
}
}
frame.html:
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<title>frame</title>
</head>
<body>
<div class="row-fluid">
<div class="span10 well">
<h3>frame</h3>
<iframe id="f1" src="inner.html" width="800", height="600"></iframe>
</div>
</div>
</body>
</html>
inner.html:
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<title>inner</title>
</head>
<body>
<div class="row-fluid">
<div class="span6 well">
<h3>inner</h3>
<iframe id="f2" src="http://www.baidu.com" width="700" height="500"></iframe>
<a
href="javascript:alert('watir-webdriver better than selenium webdriver;')">click</a>
</div>
</div>
</body>
</html>
7. Js
package com.my.simple;
import java.io.File;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class Js {
public static void main(String[] args) throws InterruptedException {
WebDriver dr = new ChromeDriver();
JavascriptExecutor jse = (JavascriptExecutor) dr;
File file = new File("src/js.html");
String filePath = "file:///" + file.getAbsolutePath();
System.out.printf("访问 %s \n", filePath);
dr.get(filePath);
Thread.sleep(2000);
System.out.println("点击链接");
jse.executeScript("window.document.getElementById('tooltip').click()");
Thread.sleep(2000);
// jse.executeScript("alert(\"Hello,js!\")");
System.out.println("打开腾讯首页");
jse.executeScript("window.open('http://www.qq.com/')");
// 备注:各个浏览器利用js关闭浏览器方法不同
dr.close();
}
}
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<title>js</title>
</head>
<body>
<h3>js</h3>
<div class="row-fluid">
<div class="span6">
<a id="tooltip" href="https://www.baidu.com" data-toggle="tooltip" title="test js">hover to see tooltip</a>
</div>
</div>
</body>
</html>
8. OpenAndCloseBrowser
package com.my.simple;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class OpenAndCloseBrowser {
public static void main(String[] args) {
System.out.println("浏览器即将打开");
WebDriver dr = new ChromeDriver();
System.out.println("浏览器即将关闭");
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
dr.quit(); // 不仅关闭浏览器,实例也销毁掉
// dr.close(); //dr实例不销毁,只是把浏览器关掉
System.out.println("关闭浏览器");
}
}
9. Select
对于非标准的select菜单来说 new Select方法是不work呢,那么这个时候就是使用对象的click方法进行处理
package com.my.simple;
import java.io.File;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.Select;
public class SelectExample {
public static void main(String[] args) throws Exception {
WebDriver dr = new ChromeDriver();
File file = new File("src/Select.html");
String filePath = "file:///" + file.getAbsolutePath();
System.out.printf("访问 %s \n", filePath);
dr.get(filePath);
Thread.sleep(2000);
// Select s= new Select(dr.findElement(By.name("select2"))) ;
// s.selectByVisibleText("");
new Select(dr.findElement(By.name("select2"))).selectByVisibleText("IE8");
Thread.sleep(1000);
new Select(dr.findElement(By.name("select2"))).selectByVisibleText("IE7");
Thread.sleep(1000);
System.out.println("浏览器即将关闭");
dr.quit();
}
}
<html>
<body>
<select name="select2" size="1">
<option value="1" selected>选择浏览器</option>
<option value="2">IE8</option>
<option value="3">chrome</option>
<option value="4">firefox</option>
<option value="5">IE7</option>
</select>
</body>
</html>
10. Status
package com.my.simple;
import java.io.File;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class Status {
public static void main(String[] args) throws InterruptedException {
WebDriver dr = new ChromeDriver();
File file = new File("src/status.html");
String filePath = "file:///" + file.getAbsolutePath();
System.out.printf("访问 %s \n", filePath);
dr.get(filePath);
Thread.sleep(1000);
WebElement textField = dr.findElement(By.name("user"));
System.out.println(textField.isEnabled());
System.out.println(dr.findElement(By.className("btn")).isEnabled());
Thread.sleep(1000);
System.out.println("浏览器即将关闭");
//((JavascriptExecutor)dr).executeScript("document.write( \" Hello!\")"); //js代码
dr.quit();
}
}
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<title>status</title>
</head>
<body>
<h3>status</h3>
<div class="row-fluid">
<div class="span3">
<input name="user" placeholder="Disabled TextField" disabled />
</div>
<div class="span3">
<a class="btn disabled">Disabled Button</a>
</div>
<div class="span3">
<input name="radio" type="radio" />
</div>
</div>
</body>
</html>
11. TitleAndUrl
package com.my.simple;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class TitleAndUrl {
public static void main(String[] args) throws InterruptedException {
WebDriver dr = new ChromeDriver();
String url = "http://www.baidu.com";
System.out.printf("访问 %s \n", url);
dr.get(url);
Thread.sleep(2000);
System.out.printf("title of current page is %s\n", dr.getTitle());
System.out.printf("url of current page is %s\n", dr.getCurrentUrl());
System.out.println("浏览器即将关闭");
dr.quit();
}
}
12. TitleAndUrlWithMultiBrowser
对于ie的访问,需要进行设置
dr=new InternetExplorerDriver(); //ie11>安全>Internet、本次Internet、受信任的站点、受限制的站点 统一设置为 启用保护模式;缩放100%
package com.my.simple;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class TitleAndUrlWithMultiBrowser {
public static void main(String[] args) throws InterruptedException {
WebDriver dr = null;
// 或者把 chrome.exe 和Ie.exe 加载到环境变量的path中,这样就不需要 使用System.setProperty
// System.setProperty("webdriver.chrome.driver","D:\\lesson\\全栈\\driver\\chromedriver.exe");
try {
// dr = new ChromeDriver();
dr = new FirefoxDriver();
// dr=new InternetExplorerDriver(); //ie11>安全>Internet、本次Internet、受信任的站点、受限制的站点
// 统一设置为 启用保护模式;缩放100%
String url = "https://www.baidu.com";
System.out.printf("访问 %s \n", url);
// WebElement wel=dr.findElement(By.id("test"));
// wel.click();
// dr.findElement(By.id("test")).click();
dr.get(url);
// dr.findElement(By By.id("")).click();
Thread.sleep(2000);
System.out.printf("title of current page is %s\n", dr.getTitle());
System.out.printf("url of current page is %s\n", dr.getCurrentUrl());
System.out.println("浏览器即将关闭");
dr.quit();
} catch (Exception e) {
System.out.println("exception");
dr.quit();
}
// Thread.sleep(2000);
}
}
13. Upload
如果 你的上传文件窗口时 标准的input对话框,可以使用sendkeys的方法来上传文件,但是出于安全考虑,公司都会使用特殊的
控件来做上传文件处理,这个时候我们需要借助其他的类来解决问题 robot
package com.my.simple;
import java.awt.AWTException;
import java.io.File;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class Upload {
public static void main(String[] args) throws InterruptedException, AWTException {
WebDriver dr = new ChromeDriver();
// RobotKeyboard rk= new RobotKeyboard();
File file = new File("src/upload_file.html");
String filePath = "file:///" + file.getAbsolutePath();
System.out.printf("访问 %s \n", filePath);
dr.get(filePath);
Thread.sleep(1000);
dr.findElement(By.cssSelector("input[type=file]")).sendKeys("e:\\头像.jpg");
Thread.sleep(1000);
System.out.println("浏览器即将关闭");
// dr.quit();
}
}
13.1 RobotExp
package com.my.webdriver;
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
public class RobotExp {
public static void main(String[] args) {
try {
Robot robot = new Robot();
// 定义5秒的延迟以便你打开notepad
// Robot 开始写
robot.delay(5000);
robot.keyPress(KeyEvent.VK_SHIFT);
robot.keyPress(KeyEvent.VK_H);
robot.keyPress(KeyEvent.VK_I);
robot.keyPress(KeyEvent.VK_SPACE);
robot.keyPress(KeyEvent.VK_C);
robot.keyPress(KeyEvent.VK_A);
robot.keyPress(KeyEvent.VK_O);
robot.keyPress(KeyEvent.VK_E);
robot.keyPress(KeyEvent.VK_R);
robot.keyRelease(KeyEvent.VK_SHIFT);
robot.keyPress(KeyEvent.VK_ENTER);
} catch (AWTException e) {
e.printStackTrace();
}
}
}
//设计该类的目的是对selenium无法进行对象输入字符情况时提供处理方法,该类为单例模式,提供字符的输入和特殊按键的输入
package com.my.webdriver;
import static java.awt.event.KeyEvent.*;
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
public class RobotKeyboard {
private static RobotKeyboard uniqueInstance = null;
private Robot robot;
public RobotKeyboard() throws AWTException {
this.robot = new Robot();
}
public static synchronized RobotKeyboard getInstance() throws AWTException {
if (uniqueInstance == null) {
uniqueInstance = new RobotKeyboard();
}
return uniqueInstance;
}
// 提供字符串的输入
public void type(CharSequence characters) {
int length = characters.length();
for (int i = 0; i < length; i++) {
char character = characters.charAt(i);
type(character);
}
}
// 提供键盘中特殊按键的输入
public void typeKey(String p_string, int p_times) {
if (p_string.equalsIgnoreCase("Tab"))
doTypeKey(KeyEvent.VK_TAB, p_times);
else if (p_string.equalsIgnoreCase("Enter"))
doTypeKey(KeyEvent.VK_ENTER, p_times);
else if (p_string.equalsIgnoreCase("Up"))
doTypeKey(KeyEvent.VK_UP, p_times);
else if (p_string.equalsIgnoreCase("Down"))
doTypeKey(KeyEvent.VK_DOWN, p_times);
else if (p_string.equalsIgnoreCase("Left"))
doTypeKey(KeyEvent.VK_LEFT, p_times);
else if (p_string.equalsIgnoreCase("Right"))
doTypeKey(KeyEvent.VK_RIGHT, p_times);
else if (p_string.equalsIgnoreCase("Shift"))
doTypeKey(KeyEvent.VK_SHIFT, p_times);
else if (p_string.equalsIgnoreCase("ESC"))
doTypeKey(KeyEvent.VK_ESCAPE, p_times);
else if (p_string.equalsIgnoreCase("Backspace"))
doTypeKey(KeyEvent.VK_BACK_SPACE, p_times);
else
doTypeKey(0, p_times);
}
// 提供字符的输入
public void type(char character) {
switch (character) {
case 'a':
doType(VK_A);
break;
case 'b':
doType(VK_B);
break;
case 'c':
doType(VK_C);
break;
case 'd':
doType(VK_D);
break;
case 'e':
doType(VK_E);
break;
case 'f':
doType(VK_F);
break;
case 'g':
doType(VK_G);
break;
case 'h':
doType(VK_H);
break;
case 'i':
doType(VK_I);
break;
case 'j':
doType(VK_J);
break;
case 'k':
doType(VK_K);
break;
case 'l':
doType(VK_L);
break;
case 'm':
doType(VK_M);
break;
case 'n':
doType(VK_N);
break;
case 'o':
doType(VK_O);
break;
case 'p':
doType(VK_P);
break;
case 'q':
doType(VK_Q);
break;
case 'r':
doType(VK_R);
break;
case 's':
doType(VK_S);
break;
case 't':
doType(VK_T);
break;
case 'u':
doType(VK_U);
break;
case 'v':
doType(VK_V);
break;
case 'w':
doType(VK_W);
break;
case 'x':
doType(VK_X);
break;
case 'y':
doType(VK_Y);
break;
case 'z':
doType(VK_Z);
break;
case 'A':
doType(VK_SHIFT, VK_A);
break;
case 'B':
doType(VK_SHIFT, VK_B);
break;
case 'C':
doType(VK_SHIFT, VK_C);
break;
case 'D':
doType(VK_SHIFT, VK_D);
break;
case 'E':
doType(VK_SHIFT, VK_E);
break;
case 'F':
doType(VK_SHIFT, VK_F);
break;
case 'G':
doType(VK_SHIFT, VK_G);
break;
case 'H':
doType(VK_SHIFT, VK_H);
break;
case 'I':
doType(VK_SHIFT, VK_I);
break;
case 'J':
doType(VK_SHIFT, VK_J);
break;
case 'K':
doType(VK_SHIFT, VK_K);
break;
case 'L':
doType(VK_SHIFT, VK_L);
break;
case 'M':
doType(VK_SHIFT, VK_M);
break;
case 'N':
doType(VK_SHIFT, VK_N);
break;
case 'O':
doType(VK_SHIFT, VK_O);
break;
case 'P':
doType(VK_SHIFT, VK_P);
break;
case 'Q':
doType(VK_SHIFT, VK_Q);
break;
case 'R':
doType(VK_SHIFT, VK_R);
break;
case 'S':
doType(VK_SHIFT, VK_S);
break;
case 'T':
doType(VK_SHIFT, VK_T);
break;
case 'U':
doType(VK_SHIFT, VK_U);
break;
case 'V':
doType(VK_SHIFT, VK_V);
break;
case 'W':
doType(VK_SHIFT, VK_W);
break;
case 'X':
doType(VK_SHIFT, VK_X);
break;
case 'Y':
doType(VK_SHIFT, VK_Y);
break;
case 'Z':
doType(VK_SHIFT, VK_Z);
break;
case '`':
doType(VK_BACK_QUOTE);
break;
case '0':
doType(VK_0);
break;
case '1':
doType(VK_1);
break;
case '2':
doType(VK_2);
break;
case '3':
doType(VK_3);
break;
case '4':
doType(VK_4);
break;
case '5':
doType(VK_5);
break;
case '6':
doType(VK_6);
break;
case '7':
doType(VK_7);
break;
case '8':
doType(VK_8);
break;
case '9':
doType(VK_9);
break;
case '-':
doType(VK_MINUS);
break;
case '=':
doType(VK_EQUALS);
break;
case '~':
doType(VK_SHIFT, VK_BACK_QUOTE);
break;
case '!':
doType(VK_EXCLAMATION_MARK);
break;
case '@':
doType(VK_AT);
break;
case '#':
doType(VK_NUMBER_SIGN);
break;
case '$':
doType(VK_DOLLAR);
break;
case '%':
doType(VK_SHIFT, VK_5);
break;
case '^':
doType(VK_CIRCUMFLEX);
break;
case '&':
doType(VK_AMPERSAND);
break;
case '*':
doType(VK_ASTERISK);
break;
case '(':
doType(VK_LEFT_PARENTHESIS);
break;
case ')':
doType(VK_RIGHT_PARENTHESIS);
break;
case '_':
doType(VK_UNDERSCORE);
break;
case '+':
doType(VK_PLUS);
break;
case '\t':
doType(VK_TAB);
break;
case '\n':
doType(VK_ENTER);
break;
case '[':
doType(VK_OPEN_BRACKET);
break;
case ']':
doType(VK_CLOSE_BRACKET);
break;
case '\\':
doType(VK_BACK_SLASH);
break;
case '{':
doType(VK_SHIFT, VK_OPEN_BRACKET);
break;
case '}':
doType(VK_SHIFT, VK_CLOSE_BRACKET);
break;
case '|':
doType(VK_SHIFT, VK_BACK_SLASH);
break;
case ';':
doType(VK_SEMICOLON);
break;
case ':':
doType(VK_SHIFT, VK_SEMICOLON);
break;
case '\'':
doType(VK_QUOTE);
break;
case '"':
doType(VK_QUOTEDBL);
break;
case ',':
doType(VK_COMMA);
break;
case '<':
doType(VK_LESS);
break;
case '.':
doType(VK_PERIOD);
break;
case '>':
doType(VK_GREATER);
break;
case '/':
doType(VK_SLASH);
break;
case '?':
doType(VK_SHIFT, VK_SLASH);
break;
case ' ':
doType(VK_SPACE);
break;
default:
throw new IllegalArgumentException("Cannot type character " + character);
}
}
private void doType(int keyCodes) {
if (keyCodes == 0) {
return;
}
robot.keyPress(keyCodes);
robot.keyRelease(keyCodes);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
private void doType(int keyShit, int keyCodes) {
if (keyCodes == 0) {
return;
}
robot.keyPress(keyShit);
robot.keyPress(keyCodes);
robot.keyRelease(keyCodes);
robot.keyRelease(keyShit);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
private void doTypeKey(int keyCodes, int times) {
if (keyCodes == 0) {
return;
}
for (int i = 0; i < times; i++) {
robot.keyPress(keyCodes);
robot.keyRelease(keyCodes);
}
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
package com.my.webdriver;
import java.awt.AWTException;
public class KeyDemo {
public static void main(String[] args) throws AWTException, InterruptedException {
Thread.sleep(3000);
RobotKeyboard.getInstance().type("d:\\logo.jpg");
Thread.sleep(1000);
RobotKeyboard.getInstance().typeKey("Tab", 2);
RobotKeyboard.getInstance().typeKey("Enter", 1);
}
}
14. Window
package com.my.simple;
import java.io.File;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class Window { // 切换浏览器的窗口
public static void main(String[] args) throws InterruptedException {
WebDriver dr = new ChromeDriver();
File file = new File("src/window.html");
String filePath = "file:///" + file.getAbsolutePath();
System.out.printf("访问 %s \n", filePath);
dr.get(filePath);
Thread.sleep(1000);
dr.findElement(By.linkText("Visit Baidu!")).click();
for (String s : dr.getWindowHandles()) {
dr.switchTo().window(s);
System.out.println(s);
if (dr.getTitle().equals("百度一下,你就知道")) {
System.out.println("切换到百度窗口!");
break;
}
}
Thread.sleep(1000);
System.out.println("浏览器即将关闭");
dr.quit();
}
public void swithToWindow(String p_url, String p_title) {
WebDriver dr = new ChromeDriver();
dr.get(p_url);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
dr.findElement(By.linkText("Visit Baidu!")).click();
for (String s : dr.getWindowHandles()) {
dr.switchTo().window(s);
System.out.println(s);
if (dr.getTitle().equals(p_title)) {
System.out.println("切换到百度窗口!");
break;
}
}
}
}