2_Selenium对象识别
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();