2_Selenium对象识别
上一篇 / 下一篇 2016-02-04 10:19:05 / 个人分类:Selenium Java
更好格式可去这里查看:
http://www.cnblogs.com/lizitest/p/5135538.html
1 准备工作
firebug和firepath
我们使用xpath进行元素定位,所以需要安装firefox的两个插件,帮助编写xpath
html知识
在编写xpath的时候,需要查看html代码,所以需要理解html知识,可以到http://www.w3school.com.cn/去自学
xpath知识
也可以到http://www.w3school.com.cn/学习
2 一般对象识别及操作代码
复制代码
package com.selenium.test;
import java.util.List;
import java.util.Set;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
public class Test2 {
public static void main(String[] args) throws InterruptedException {
WebDriver driver = new FirefoxDriver(); //启动火狐浏览器
driver.manage().window().maximize(); //最大化浏览器
driver.navigate().to("http://www.baidu.com/"); //导航到百度
//登录 - 链接
WebElement linkLogin = driver.findElement(By.xpath("//div[@id='u1']/a[text()='登录']"));
linkLogin.click();
//等待2秒
Thread.sleep(3000);
//用户名、密码 - 输入框
WebElement textUsername = driver.findElement(By.xpath("//input[@id='TANGRAM__PSP_8__userName']"));
textUsername.clear();
textUsername.sendKeys("栗子测试");
WebElement textPassword = driver.findElement(By.xpath("//input[@id='TANGRAM__PSP_8__password']"));
textPassword.clear();
textPassword.sendKeys("2472471982");
//登录 - 按钮
WebElement buttonLogin = driver.findElement(By.xpath("//input[@id='TANGRAM__PSP_8__submit']"));
buttonLogin.click();
//等待2秒
Thread.sleep(3000);
//悬停
Actions action = new Actions(driver);
WebElement linkMe = driver.findElement(By.xpath("//a[@id='s_username_top']/span"));
action.moveToElement(linkMe).perform();
//账号设置 - 链接
WebElement linkSeniorSearch = driver.findElement(By.xpath("//div[@id='s_user_name_menu']/div/a[3]"));
linkSeniorSearch.click();
//账号设置 - 窗口跳转
String firstWindowHandle = driver.getWindowHandle(); //获取第一个窗口句柄
Set<String> towHandles = driver.getWindowHandles();
for (String handle : towHandles) { //遍历所有窗口句柄
System.out.println("+++" + handle);
driver.switchTo().window(handle); //切换两次,切换到第二个窗口
}
//修改资料 - 链接
WebElement linkModifyData = driver.findElement(By.xpath("//div[@id='content']//a[text()='修改资料']"));
linkModifyData.click();
//修改资料 - 窗口跳转
Set<String> threeHandles = driver.getWindowHandles(); //获取三个窗口句柄
threeHandles.removeAll(towHandles); //移除原来的两个句柄
String thirdWindowHandle = threeHandles.iterator().next(); //剩下一个句柄
driver.switchTo().window(thirdWindowHandle); //切换到第三个窗口
//性别 - 单选(被看做一组)
List<WebElement> radiosGender = driver.findElements(By.xpath("//input[@name='passport_sex']")); //定位所有单选按钮
radiosGender.get(1).click(); //index从0开始
//血型 - 此下拉框非Select,只是样式像
WebElement divBlood= driver.findElement(By.xpath("//div[@id='cussel1000002']/div"));
divBlood.click();
WebElement linkBlood= driver.findElement(By.xpath("//div[@id='cussel1000002']//a[text()='AB']"));
linkBlood.click();
//保存 - 按钮
WebElement buttonSaveBasic = driver.findElement(By.xpath("//form[@id='profile']/child::input"));
buttonSaveBasic.click();
//详细资料 - 链接
WebElement linkDetailedInfo = driver.findElement(By.xpath("//div[@id='content']//a[text()='详细资料']"));
linkDetailedInfo.click();
//等待1秒
Thread.sleep(1000);
//性格 - 多选框(被看做一组)
List<WebElement> checkboxCharacter = driver.findElements(By.xpath("//form[@id='profile']/table//tr[@class='passport_character']//input"));
WebElement element = checkboxCharacter.get(6);
element.click();
//保存 - 按钮
WebElement buttonSaveDetailedInfo = driver.findElement(By.xpath("//form[@id='profile']/child::input"));
buttonSaveDetailedInfo.click();
//关闭当前窗口,回到第一个窗口
driver.close();
driver.switchTo().window(firstWindowHandle);
}
}
复制代码
3 其他对象操作及代码
下拉框
WebElement selectElement = driver.findElement(By.xpath("//td[@id='adv-setting-4']/select")); //先定位下拉框
Select select = new Select(selectElement);
select.selectByVisibleText("最近一天");
上传文件
WebElement file = driver.findElement(By.xpath("//div[@id='uploadfile']/input"));
file.sendKeys("C:\\test.txt");
JS
JavascriptExecutor js2 = (JavascriptExecutor)driver;
js2.executeScript("alert('栗子测试,QQ:2472471982')");
Alert alert = driver.switchTo().alert(); //切换到弹出窗
alert.accept();
JS属性设置
JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("document.getElementById('saveButton').firstElementChild.disabled = false");
iframe
WebElement iframe. = driver.findElement(By.xpath("//iframe[@name='contect']"));
driver.switchTo().frame(iframe);
WebElement text = driver.findElement(By.xpath("//input[@id='username']"));
text.clear();
text.sendKeys("栗子测试,QQ:2472471982");
弹出框
WebElement link = driver.findElement(By.xpath("//div[@id='alert']/input"));
link.click();
Alert alert = driver.switchTo().alert(); //切换到弹出窗
alert.accept();
后退
driver.navigate().back();
相关阅读:
- 安装的seleniumIDE在firefox工具栏看不到图标怎么办 (zhuruize, 2016-1-06)
- Selenium2操作浏览器的Cookies (zaza9084, 2016-3-10)
- 使用PHPUnit+Selenium进行自动化测试 (zaza9084, 2016-3-28)
- C#中通过Selenium定位<a>标签的问题 (zaza9084, 2016-3-31)
- Selenium生成Report的利器-ExtentReports (zaza9084, 2016-1-20)
- Selenium启动firefox、ie、chrome各浏览器方法 (zaza9084, 2016-1-25)
- Selenium&Webdriver远程测试和多线程并发测试 (zaza9084, 2016-1-28)
- 使用extentreports生存自动化测试的测试报告 (clarkar, 2016-1-29)
- 第一个UI脚本-Python+Selenium (zaza9084, 2016-2-02)
- 1_Selenium环境搭建 (lirenlong_9990, 2016-2-02)
TAG: 元素操作 Selenium selenium xpath XPATH 定位