三十一、使用页面的文字内容识别和处理新弹出的浏览器窗口

被测试网页的HTML源码:

 <html>
<head>
<meta charset="UTF-8" content="text/html">
<title>你喜欢的水果</title>
</head>
<body>
<p id="p1">你爱吃的水果么?</p>
<br><br>
<a href="http://www.sogou.com" target="_blank">sogou搜索</a>
</body>
</html>

Java语言版本的API实例代码:

 package test;

 import org.testng.annotations.Test;

 import org.testng.annotations.BeforeMethod;

 import java.io.File;
import java.util.Set; import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchWindowException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.AfterMethod; public class ChormeOpen {
WebDriver driver; @Test
public void opentest() {
File file = new File("");
String url = file.getAbsolutePath() + "/html/" + "file12.html";
driver.get(url);
String parentWindowHandle = driver.getWindowHandle();
WebElement sogouLink = driver.findElement(By.xpath("//a"));
sogouLink.click();
Set<String> allWindowsHandles = driver.getWindowHandles();
if(!allWindowsHandles.isEmpty()){
for(String windowHandle:allWindowsHandles){
try {
if(driver.switchTo().window(windowHandle).getPageSource().contains("搜狗搜索")){
driver.findElement(By.id("query")).sendKeys("sogou");
}
} catch (NoSuchWindowException e) {
// TODO: handle exception
e.printStackTrace();
}
}
}
driver.switchTo().window(parentWindowHandle);
Assert.assertEquals(driver.getTitle(), "你喜欢的水果");
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} @BeforeMethod
public void beforeMethod() {
System.setProperty("webdriver.chrome.driver", "F:\\selenium\\chromedriver.exe");
driver = new ChromeDriver();
} @AfterMethod
public void afterMethod() {
driver.quit();
} }

三十二、操作JavaScript的Alert弹窗

目的:能够模拟单击弹出的Alert窗口的-‘确定’-按钮

被测试网页的HTML源码:

 <html>
<head>
<meta charset="UTF-8" content="text/html">
<title>你喜欢的水果</title>
</head>
<body>
<input id="button" type="button" onclick="alert('这是一个alert弹出框');" value="单击此按钮"></input>
</body>
</html>

Java语言版本的API实例代码:

 package test;

 import org.testng.annotations.Test;

 import org.testng.annotations.BeforeMethod;

 import java.io.File;
import java.util.Set; import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.NoAlertPresentException;
import org.openqa.selenium.NoSuchWindowException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.AfterMethod; public class ChormeOpen {
WebDriver driver; @Test
public void opentest() {
File file = new File("");
String url = file.getAbsolutePath() + "/html/" + "file13.html";
driver.get(url);
WebElement button = driver.findElement(By.xpath("//input"));
button.click();
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
Alert alert = driver.switchTo().alert();
Assert.assertEquals("这是一个alert弹出框", alert.getText());
alert.accept();//单击确定按钮
} catch (NoAlertPresentException e) {
// TODO: handle exception
Assert.fail("alert未被找到");
e.printStackTrace();
}
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} @BeforeMethod
public void beforeMethod() {
System.setProperty("webdriver.chrome.driver", "F:\\selenium\\chromedriver.exe");
driver = new ChromeDriver();
} @AfterMethod
public void afterMethod() {
driver.quit();
} }

三十三、操作JavaScript的confirm弹窗

目的:能够模拟单击JavaScript弹出的confirm框中的“确定”和“取消”按钮。

被测试网页的HTML源码:

 <html>
<head>
<meta charset="UTF-8" content="text/html">
<title>你喜欢的水果</title>
</head>
<body>
<input id="button" type="button" onclick="confirm('这是一个confirm弹出框');" value="单击此按钮"></input>
</body>
</html>

Java语言版本的API实例代码:

 package test;

 import org.testng.annotations.Test;

 import org.testng.annotations.BeforeMethod;

 import java.io.File;

 import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.NoAlertPresentException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.AfterMethod; public class ChormeOpen {
WebDriver driver; @Test
public void opentest() {
File file = new File("");
String url = file.getAbsolutePath() + "/html/" + "file14.html";
driver.get(url);
WebElement button = driver.findElement(By.xpath("//input"));
button.click();
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
Alert alert = driver.switchTo().alert();
Assert.assertEquals("这是一个confirm弹出框", alert.getText());
//alert.accept();//单击确定按钮
alert.dismiss();//单击取消
} catch (NoAlertPresentException e) {
// TODO: handle exception
Assert.fail("confirm未被找到");
e.printStackTrace();
}
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} @BeforeMethod
public void beforeMethod() {
System.setProperty("webdriver.chrome.driver", "F:\\selenium\\chromedriver.exe");
driver = new ChromeDriver();
} @AfterMethod
public void afterMethod() {
driver.quit();
} }

三十四、操作JavaScript的prompt弹窗

目的:能够在JavaScript的prompt弹窗中输入自定义的字符串,单击“确定”按钮和“取消”按钮。

被测试网页的HTML源码:

 <html>
<head>
<meta charset="UTF-8" content="text/html">
<title>你喜欢的水果</title>
</head>
<body>
<input id="button" type="button" onclick="prompt('这是一个prompt弹出框');" value="单击此按钮"></input>
</body>
</html>

Java语言版本的API实例代码:

 package test;

 import org.testng.annotations.Test;

 import org.testng.annotations.BeforeMethod;

 import java.io.File;

 import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.NoAlertPresentException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.AfterMethod; public class ChormeOpen {
WebDriver driver; @Test
public void opentest() {
File file = new File("");
String url = file.getAbsolutePath() + "/html/" + "file15.html";
driver.get(url);
WebElement button = driver.findElement(By.xpath("//input"));
button.click();
try {
Thread.sleep(3000);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
Alert alert = driver.switchTo().alert();
Assert.assertEquals("这是一个prompt弹出框", alert.getText());
alert.sendKeys("想改变命运,就必须每天学习2小时");
Thread.sleep(3000);
alert.accept();//单击确定按钮
//alert.dismiss();//单击取消
} catch (NoAlertPresentException e) {
// TODO: handle exception
Assert.fail("confirm未被找到");
e.printStackTrace();
}catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
try {
Thread.sleep(3000);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} @BeforeMethod
public void beforeMethod() {
System.setProperty("webdriver.chrome.driver", "F:\\selenium\\chromedriver.exe");
driver = new ChromeDriver();
} @AfterMethod
public void afterMethod() {
driver.quit();
} }

三十五、操作Frame中的页面元素

目的:能够进入页面的不同Frame中进行页面元素的操作。

被测试网页的HTML源码:

frameset.html
frame_left.html
frame_middle.html
frame_right.html

Java语言版本的API实例代码:

 package test;

 import org.testng.annotations.Test;

 import org.testng.annotations.BeforeMethod;

 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;
import org.testng.Assert;
import org.testng.annotations.AfterMethod; public class ChormeOpen {
WebDriver driver; @Test
public void opentest() {
File file = new File("");
String url = file.getAbsolutePath() + "/html/" + "frameset.html";
driver.get(url);
driver.switchTo().frame("leftframe");
WebElement leftframeText = driver.findElement(By.xpath("//p"));
Assert.assertEquals("这是左侧frame页面上的文字", leftframeText.getText());
driver.switchTo().defaultContent();
//
driver.switchTo().frame("middleframe");
WebElement middleframeText = driver.findElement(By.xpath("//p"));
Assert.assertEquals("这是中间frame页面上的文字", middleframeText.getText());
driver.switchTo().defaultContent();
//
driver.switchTo().frame("rightframe");
WebElement rightframeText = driver.findElement(By.xpath("//p"));
Assert.assertEquals("这是右侧frame页面上的文字", rightframeText.getText());
driver.switchTo().defaultContent();
//
//使用索引。从0开始
driver.switchTo().frame(1);
middleframeText = driver.findElement(By.xpath("//P"));
Assert.assertEquals("这是中间frame页面上的文字", middleframeText.getText()); try {
Thread.sleep(3000);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} @BeforeMethod
public void beforeMethod() {
System.setProperty("webdriver.chrome.driver", "F:\\selenium\\chromedriver.exe");
driver = new ChromeDriver();
} @AfterMethod
public void afterMethod() {
driver.quit();
} }

三十六、使用Frame中的HTML源码内容来操作Frame

目的:能够使用Frame页面的HTML源码定位指定的Frame页面并进行操作。

被测试网页的HTML源码:

三十五被测试页面的HTML源码。

Java语言版本的API实例代码:

 package test;

 import org.testng.annotations.Test;

 import org.testng.annotations.BeforeMethod;

 import java.io.File;
import java.util.List; import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.AfterMethod; public class ChormeOpen {
WebDriver driver; @Test
public void opentest() {
File file = new File("");
String url = file.getAbsolutePath() + "/html/" + "frameset.html";
driver.get(url);
List<WebElement> frames = driver.findElements(By.tagName("frame"));
for(WebElement frame:frames){
driver.switchTo().frame(frame);
if(driver.getPageSource().contains("中间frame")){
WebElement middleframeText = driver.findElement(By.xpath("//p"));
Assert.assertEquals("这是中间frame页面上的文字", middleframeText.getText());
break;
}else{
driver.switchTo().defaultContent();
}
}
driver.switchTo().defaultContent();
try {
Thread.sleep(3000);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} @BeforeMethod
public void beforeMethod() {
System.setProperty("webdriver.chrome.driver", "F:\\selenium\\chromedriver.exe");
driver = new ChromeDriver();
} @AfterMethod
public void afterMethod() {
driver.quit();
} }

三十七、操作IFrame中的页面元素

被测试网页的HTML源码:

三十五被测试页面的HTML源码,只是需要更新如下页面的HTML源码:

修改frame_left.html源码:

frame_left.html

在frame_left.html同级目录下新增iframe.html文件:

iframe.html

Java语言版本的API实例代码:

 package test;

 import org.testng.annotations.Test;

 import org.testng.annotations.BeforeMethod;

 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;
import org.testng.Assert;
import org.testng.annotations.AfterMethod; public class ChormeOpen {
WebDriver driver; @Test
public void opentest() {
File file = new File("");
String url = file.getAbsolutePath() + "/html/" + "frameset.html";
driver.get(url);
driver.switchTo().frame("leftframe");
WebElement iframe = driver.findElement(By.tagName("iframe"));
driver.switchTo().frame(iframe);
WebElement p = driver.findElement(By.xpath("//p"));
Assert.assertEquals("这是iframe页面上的文字", p.getText());
driver.switchTo().defaultContent();
driver.switchTo().frame("middleframe");
try {
Thread.sleep(3000);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} @BeforeMethod
public void beforeMethod() {
System.setProperty("webdriver.chrome.driver", "F:\\selenium\\chromedriver.exe");
driver = new ChromeDriver();
} @AfterMethod
public void afterMethod() {
driver.quit();
} }

三十八、操作浏览器的Cookie

目的:能够遍历输出所有的Cookie的Key和value;能够删除指定的Cookie对象;能够删除所有的Cookie对象。

被测试网页的地址:

http:www.sogou.com

Java语言版本的API实例代码:

 package test;

 import org.testng.annotations.Test;

 import org.testng.annotations.BeforeMethod;
import java.util.Set;
import org.openqa.selenium.Cookie;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterMethod; public class ChormeOpen {
WebDriver driver;
String url = "http://www.sogou.com";
@Test
public void opentest() {
driver.get(url);
Set<Cookie> cookies = driver.manage().getCookies();
Cookie newCookie = new Cookie("cookieName","cookieValue");
System.out.println(String.format("Domain-> name -> value -> expiry -> path"));
for(Cookie cookie:cookies){
System.out.println(String.format("%s-> %s -> %s -> %s -> %s",
cookie.getDomain(),cookie.getName(),
cookie.getValue(),cookie.getExpiry(),cookie.getPath()));
}
//删除cookie的3种方法
//
driver.manage().deleteCookieNamed("CookieName"); //
driver.manage().deleteCookie(newCookie); //
driver.manage().deleteAllCookies(); try {
Thread.sleep(3000);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} @BeforeMethod
public void beforeMethod() {
System.setProperty("webdriver.chrome.driver", "F:\\selenium\\chromedriver.exe");
driver = new ChromeDriver();
} @AfterMethod
public void afterMethod() {
driver.quit();
} }

最新文章

  1. [转]CSS 类名的单词连字符:下划线还是横杠?
  2. android代码优化----ListView中自定义adapter的封装(ListView的模板写法)
  3. 1234[&#39;toString&#39;][&#39;length&#39;] 等于啥?
  4. git 在windows上 生成ssh公钥
  5. google python/c++ code style naming
  6. 本地连接速度100.0mbps变10.0mbps如何恢复
  7. iOS中json解析出现的null,nil,NSNumber的问题
  8. 解决phpmyadmin-1800秒超时链接失效问题
  9. 迪杰斯特拉算法c语言实现
  10. Tomcat剖析(五):Tomcat 容器
  11. 【转】人工智能(AI)资料大全
  12. 第一章 开发简单Java应用程序
  13. numpy pandas 索引注意事项
  14. Selenium库,Python精品教程!
  15. [逆向工程] 二进制拆弹Binary Bombs 快乐拆弹 详解
  16. PHP代码-数据爬取(a标签和a标签所对应的内容)
  17. [JUC-2]AbstractQueuedSynchronizer源码分析
  18. 题解-AtCoder-agc006C Rabbit Exercise
  19. 转:如何在Vue项目中使用vw实现移动端适配
  20. Spring Boot 项目实战(六)集成 Apollo

热门文章

  1. 帝国CMS7.2新增多图同时上传插件,上传多图效率更高
  2. Shell脚本中$0、$?、$!、$$、$*、$#、$@等的意义
  3. socket小实例
  4. UITextField 全属性
  5. ios 调用系统应用的方法 应用间跳转的方法
  6. Bitmap之安卓手机壁纸的设置
  7. mac 操作idea快捷键
  8. C++11新特性之二——std::bind std::function 高级用法
  9. x86 体系指令
  10. IE各浏览器HACK