关闭

selenium2笔记

81人阅读 评论(0) 收藏 举报
如何查找元素
By ID
WebElement element = driver.findElement(By.id("passwd-id"));
By Name
WebElement element = driver.findElement(By.name("passwd"));
By XPATH
WebElement element =driver.findElement(By.xpath("//input[@id='passwd-id']"));
By Class Name
List<WebElement>cheeses = driver.findElements(By.className("cheese"));
WebElement cheese =driver.findElement(By.linkText("cheese"));


如何对页面元素进行操作

输入框(text field or textarea):

在输入框中输入内容:

element.sendKeys(“test”);
将输入框清空:
element.clear();
获取输入框的文本内容:
element.getText();



下拉选择框(Select):
Select select = new Select(driver.findElement(By.id("select")));
选择对应的选择项:
select.selectByVisibleText(“mediaAgencyA”);
select.selectByValue(“MA_ID_001”);
不选择对应的选择项:
select.deselectAll();
select.deselectByValue(“MA_ID_001”);
select.deselectByVisibleText(“mediaAgencyA”);
或者获取选择项的值:
select.getAllSelectedOptions();
select.getFirstSelectedOption();




单选项(Radio Button):
WebElement bookMode =driver.findElement(By.id("BookMode"));
选择某个单选项:
bookMode.click();
清空某个单选项:
bookMode.clear();
判断某个单选项是否已经被选择:
bookMode.isSelected();


多选项(checkbox):
WebElement checkbox =driver.findElement(By.id("myCheckbox."));
checkbox.click(); 
checkbox.clear();
checkbox.isSelected();
checkbox.isEnabled();


按钮(button):
WebElement saveButton = driver.findElement(By.id("save"));
点击按钮:
saveButton.click();
判断按钮是否enable:
saveButton.isEnabled ();


左右选择框:
Select lang = new Select(driver.findElement(By.id("languages")));
lang.selectByVisibleText(“English”);
WebElement addLanguage =driver.findElement(By.id("addButton"));
addLanguage.click();


弹出对话框(Popup dialogs):
Alert alert = driver.switchTo().alert();
alert.accept();
alert.dismiss();
alert.getText();


表单(Form):
WebElement approve = driver.findElement(By.id("approve"));
approve.click();
approve.submit();//只适合于表单的提交


上传文件 (Upload File):
WebElement adFileUpload = driver.findElement(By.id("WAP-upload"));
String filePath = "C:\test\\uploadfile\\media_ads\\test.jpg";
adFileUpload.sendKeys(filePath);


Windows 和 Frames之间的切换:
一般来说,登录后建议是先:
driver.switchTo().defaultContent();
切换到某个frame:
driver.switchTo().frame("leftFrame");
从一个frame切换到另一个frame:
driver.switchTo().frame("mainFrame");
切换到某个window:
driver.switchTo().window("windowName");


拖拉(Drag andDrop):
WebElement element =driver.findElement(By.name("source"));
WebElement target = driver.findElement(By.name("target"));
(new Actions(driver)).dragAndDrop(element, target).perform();


导航 (Navigationand History):
打开一个新的页面:
driver.navigate().to("http://www.example.com");
通过历史导航返回原页面: 
driver.navigate().forward();
driver.navigate().back();
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:550次
    • 积分:20
    • 等级:
    • 排名:千里之外
    • 原创:1篇
    • 转载:4篇
    • 译文:0篇
    • 评论:0条
    文章存档