转自:http://blog.sina.com.cn/s/blog_6966650401012a7q.html

一、输入框(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();//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出要拖入的页面元素对象和目标对象,然后进行拖入。
WebElement element = dr.findElement(By.id("item1"));
WebElement target = dr.findElement(By.id("drop"));
(new Actions(dr)).dragAndDrop(element, target).perform(); //利用循环把其它item也拖入
String id="item" ;
for(int i=;i<=;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();
十二、获取页面CSS属性
.获取文字颜色
dr.findElement(By.id("tooltip")).getCssValue("color")
.获取文字字号
dr.findElement(By.tagName("h3")).getCssValue("font")

Selenium操作页面元素的更多相关文章

  1. [Selenium] 操作页面元素等待时间

    WebDriver 在操作页面元素等待时间时,提供2种等待方式:一个为显式等待,一个为隐式等待,其区别在于: 1)显式等待:明确地告诉 WebDriver 按照特定的条件进行等待,条件未达到就一直等待 ...

  2. selenium常用命令--操作页面元素及获取元素内容整理

    selenium常用命令之操作页面元素及获取元素内容的事件整理 例子:  /**id <input type="text" id="phone" name ...

  3. Selenium with Python 005 - 调用js操作页面元素

    WebDriver提供了execute_script()方法来执行JavaScript方法,格式如 driver.execute_script(script,*args) 执行js一般有两种场景,一是 ...

  4. Java&Selenium调用JS实现高亮被操作页面元素高亮

    Java&Selenium调用JS实现高亮被操作页面元素高亮 /* * the method of invoking js to do something * * @author daviey ...

  5. selenium定位页面元素的一件趣事

    PS:本博客selenium分类不会记载selenium打开浏览器,定位元素,操作页面元素,切换到iframe,处理alter.confirm和prompt对话框这些在网上随处可见的信息:本博客此分类 ...

  6. 使用javaScript操作页面元素

    from selenium import webdriver import time import unittest from selenium.common.exceptions import We ...

  7. F12修改html进行本地js操作页面元素

    F12修改html进行本地js操作页面元素

  8. 利用 jQuery 操作页面元素的方法,实现电商网站购物车页面商品数量的增加和减少操作,要求单项价格和总价随着数量的改变而改变

    查看本章节 查看作业目录 需求说明: 利用 jQuery 操作页面元素的方法,实现电商网站购物车页面商品数量的增加和减少操作,要求单项价格和总价随着数量的改变而改变 当用户单击"+" ...

  9. 使用 jQuery 操作页面元素的方法,实现浏览大图片的效果,在页面上插入一幅小图片,当鼠标悬停到小图片上时,在小图片的右侧出现与之相对应的大图片

    查看本章节 查看作业目录 需求说明: 使用 jQuery 操作页面元素的方法,实现浏览大图片的效果,在页面上插入一幅小图片,当鼠标悬停到小图片上时,在小图片的右侧出现与之相对应的大图片 实现思路: 在 ...

随机推荐

  1. Java反射深入浅出(一)

    在JVM中对一个类实例的创建,有两种方式,一种是编译时,一种是运行时.两种方式在开发过程中都是十分重要的.在Java中无时无刻无处不在的Java对象,实例化的过程也就变得尤为引人瞩目.我们经常用new ...

  2. grunt安装、配置、在webstrom中使用

    1.全局范围安装 Grunt命令行(CLI) npm install -g grunt-cli Grunt CLI的任务很简单:调用与Gruntfile在同一目录中 Grunt.这样带来的好处是,允许 ...

  3. The type XXX cannot be resolved. It is indirectly referenced from required .class files错误.....

    遇到The type XXX cannot be resolved. It is indirectly referenced from required .class files错误.....,查找的 ...

  4. Android View坐标Left, Right, Top, Bottom

    Android View坐标Left, Right, Top, Bottom 1.分析说明Left, Right, Top, Bottom View中对于该字段说明如下,相对父布局的的位置 相对父布局 ...

  5. session 存储方式

    Session 的存储方式 在 php.ini 文件中,进行配置. 涉及配置参数: - session.save_handler - session.save_path 注意:这两个参数可以在 PHP ...

  6. I7-5775C之所以被Intel跳过,是因为本身有太多BUG

    说起I7-5775C,第五代酷睿处理器,可能大多数人都没有使用过,也并不清楚他有什么样的特性. 在2015年6月份,我在日本亚马逊买了一个I7-5775C,从此噩梦就开始了(现在已经换了I7-5820 ...

  7. 解决jQuery ajax跨域问题,Google、IE、Firefox亲测有效

    直接上最后的结果吧 JS: $.ajax({ type: "GET", async: false, crossDomain: true, url: "www.test.c ...

  8. redmine设置

    接上篇. 1.redmine新版本已经比较强大了,可以定制所有字段(含标准字段和自定义字段)的读写属性.这里为了避免字段过多影响用户感受,希望增加功能将不相关的字段屏蔽,下载插件Workflow Hi ...

  9. php之XML转数组函数的方法

    <?/** * xml2array() will convert the given XML text to an array in the XML structure. * Link: htt ...

  10. 连续最短路算法(Successive Shortest Path)(最小费用最大流)

    #include <cstdio> #include <cstring> #include <queue> #include <vector> #inc ...