Selenium(五)——webdriver 之操作页面元素

论坛 期权论坛 脚本     
匿名网站用户   2020-12-20 16:04   11   0

忘记是在哪篇文章看到的了,整理过来的,不能放出处了。

一、输入框(text field or textarea)

找到输入框元素:

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

将输入框清空:

element.clear();

在输入框中输入内容:

element.sendKeys(“test”);

获取输入框的文本内容:

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();

对下拉框进行操作时首先要定位到这个下拉框,new 一个Selcet对象,然后对它进行操作

例如:

以http://passport.51.com/reg2.5p这个页面为例。这个页面中有4个下拉框,下面演示4种选中下拉框选项的方法。

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.support.ui.Select;

public class SelectsStudy {

public static void main(String[] args) {

System.setProperty("webdriver.firefox.bin","D:\\Program Files\\Mozilla Firefox\\firefox.exe");

WebDriver dr = new FirefoxDriver();

dr.get("http://passport.51.com/reg2.5p");

//通过下拉列表中选项的索引选中第二项,即2011年

Select selectAge = new Select(dr.findElement(By.id("User_Age")));

selectAge.selectByIndex(2);//Select.selectByIndex

//通过下拉列表中的选项的value属性选中"上海"这一项

Select selectShen = new Select(dr.findElement(By.id("User_Shen")));

selectShen.selectByValue("上海");//Select.selectByValue

//通过下拉列表中选项的可见文本选 中"浦东"这一项

Select selectTown = new Select(dr.findElement(By.id("User_Town")));

selectTown.selectByVisibleText("浦东");//Select.selectByVisibleText

//这里只是想遍历一下下拉列表所有选项,用click进行选中选项

Select selectCity = new Select(dr.findElement(By.id("User_City")));

for(WebElement e : selectCity.getOptions())//Select.getOptions()

e.click();

}

}

三、单选项(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)

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);

十、拖拉(Drag andDrop)

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

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

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

例如:下面这个页面是一个演示拖放元素的页面,你可以把左右页面中的条目拖放到右边的div框中。

http://koyoz.com/demo/html/drag-drop/drag-drop.html

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 DragAndDrop {

public static void main(String[] args) {

System.setProperty("webdriver.firefox.bin","D:\\Program Files\\Mozilla Firefox\\firefox.exe");

WebDriver dr = new FirefoxDriver();

dr.get("http://koyoz.com/demo/html/drag-drop/drag-drop.html");

//首先new出要拖入的页面元素对象和目标对象,然后进行拖入。

WebElementelement = dr.findElement(By.id("item1"));

WebElementtarget = dr.findElement(By.id("drop"));

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

//利用循环把其它item也拖入

String id="item" ;

for(int i=2;i<=6;i++){

String item = id+i;

(new Actions(dr)).dragAndDrop(dr.findElement(By.id(item)), target).perform();

}

}

}

代码很简单,需要注意的是(new Actions(dr)).dragAndDrop(element, target).perform();这句话中,dragAndDrop(element, target)这个方法是定义了“点击element元素对象,然后保持住,直到拖到目标元素对象里面才松开”这一系列动作的Actions,如果你不调用perform()方法,这个Actions是不会执行的。

十一、导航 (Navigationand History)

打开一个新的页面:

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

通过历史导航返回原页面:

driver.navigate().forward();

driver.navigate().back();

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:1136255
帖子:227251
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP