Selenium webdriver Java 元素操作

本来这些东西网上一搜一大堆,但是本着收集的精神,整理一份放着吧!哈!哈!哈!

1. 输入框(text field or textarea)

WebElement element = driver.findElement(By.id("passwd-id"));

element.sendKeys(“test”);

element.clear(); 

element.getText();

2. 下拉选择框(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();

3.单选项(Radio Button) 

WebElement bookMode =driver.findElement(By.id("BookMode"));

bookMode.click();

bookMode.clear(); 

bookMode.isSelected(); 

4. 多选项(checkbox) 

WebElement checkbox =driver.findElement(By.id("myCheckbox."));

checkbox.click();

checkbox.clear(); 

checkbox.isSelected();

checkbox.isEnabled(); 

5. 按钮(button) 

找到按钮元素:

WebElement saveButton = driver.findElement(By.id("save"));

saveButton.click();

saveButton.isEnabled (); 

6. 左右选择框

也就是左边是可供选择项,选择后移动到右边的框中,反之亦然。例如:

Select lang = new Select(driver.findElement(By.id("languages")));

lang.selectByVisibleText(“English”);

WebElement addLanguage =driver.findElement(By.id("addButton"));

addLanguage.click(); 

7. 弹出对话框(Popup Dialogs)

Alert alert = driver.switchTo().alert();

alert.accept();

alert.dismiss();

alert.getText(); 

8. 表单(Form)

Form中的元素的操作和其它的元素操作一样,对元素操作完成后对表单的提交可以:

WebElement approve = driver.findElement(By.id("approve"));

approve.click();

approve.submit();

9. 上传文件(Upload File) 

WebElement adFileUpload = driver.findElement(By.id("WAP-upload"));

String filePath = "C:\test\\uploadfile\\media_ads\\test.jpg";

adFileUpload.sendKeys(filePath); 

10. Windows 和 Frames 的切换

一般来说,登录后建议是先:

driver.switchTo().defaultContent();

driver.switchTo().frame("leftFrame");

driver.switchTo().window("windowName");

11. 拖拽(Drag and Drop)

WebElement element =driver.findElement(By.name("source"));

WebElement target = driver.findElement(By.name("target"));

(new Actions(driver)).dragAndDrop(element, target).perform();

12. 导航(Navigation and History)

driver.navigate().to("http://www.example.com");

driver.navigate().forward();

driver.navigate().back();

posted @ 2015-07-11 16:34  微微微笑  阅读(373)  评论(0编辑  收藏  举报