WebDriver拾级而上·之四 操作页面元素

本文详细介绍了使用Selenium进行Web自动化测试时常见的元素交互方法,包括输入框、下拉选择框、单选与多选框等操作技巧,并提供了丰富的代码示例。
一、输入框(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")));
<wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><br style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px">//选择对应的选择项:</span><br style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px">select.selectByVisibleText(“mediaAgencyA”);</span><br style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px">或</span><br style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px">select.selectByValue(“MA_ID_001”);</span><br style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><br style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px">//不选择对应的选择项:</span><br style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px">select.deselectAll();</span><br style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px">select.deselectByValue(“MA_ID_001”);</span><br style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px">select.deselectByVisibleText(“mediaAgencyA”);</span><br style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px">或者获取选择项的值:</span><br style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px">select.getAllSelectedOptions();</span><br style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px">select.getFirstSelectedOption();</span><br style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><br style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="word-wrap:normal; word-break:normal; line-height:21px; font-family:simsun; font-size:14px; color:rgb(255,0,0)">对下拉框进行操作时首先要定位到这个下拉框,new 一个Selcet对象,然后对它进行操作</span><br style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><br style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px">例如:</span><br style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px">以http://passport.51.com/reg2.5p这个页面为例。这个页面中有4个下拉框,下面演示4种选中下拉框选项的方法。</span><br style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px">import org.openqa.selenium.By;</span><br style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px">import org.openqa.selenium.WebDriver;</span><br style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px">import org.openqa.selenium.WebElement;</span><br style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px">import org.openqa.selenium.firefox.FirefoxDriver;</span><br style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px">import org.openqa.selenium.support.ui.Select;</span><br style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><br style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px">public class SelectsStudy {</span><br style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px">public static void main(String[] args) {</span><br style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px">System.setProperty("webdriver.firefox.bin","D:\\Program Files\\Mozilla Firefox\\firefox.exe");</span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><br style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px">WebDriver dr = new FirefoxDriver();</span><br style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px">dr.get("http://passport.51.com/reg2.5p");</span><br style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><br style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px">//通过</span><span style="word-wrap:normal; word-break:normal; line-height:21px; font-family:simsun; font-size:14px; color:rgb(255,0,0)">下拉列表中选项的索引</span><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px">选中第二项,即2011年</span><br style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px">Select selectAge = new Select(dr.findElement(By.id("User_Age")));</span><br style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px">selectAge.</span><span style="word-wrap:normal; word-break:normal; line-height:21px; font-family:simsun; font-size:14px; color:rgb(255,0,0)">selectByIndex</span><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px">(2);//</span><span style="word-wrap:normal; word-break:normal; line-height:21px; font-family:simsun; font-size:14px; color:rgb(255,0,0)">Select.selectByIndex</span><br style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><br style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px">//通过</span><span style="word-wrap:normal; word-break:normal; line-height:21px; font-family:simsun; font-size:14px; color:rgb(255,0,0)">下拉列表中的选项的value属性</span><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px">选中"上海"这一项</span><br style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px">Select selectShen = new Select(dr.findElement(By.id("User_Shen")));</span><br style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px">selectShen.</span><span style="word-wrap:normal; word-break:normal; line-height:21px; font-family:simsun; font-size:14px; color:rgb(255,0,0)">selectByValue</span><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px">("上海");//</span><span style="word-wrap:normal; word-break:normal; line-height:21px; font-family:simsun; font-size:14px; color:rgb(255,0,0)">Select.selectByValue</span><br style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><br style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px">//通过</span><span style="word-wrap:normal; word-break:normal; line-height:21px; font-family:simsun; font-size:14px; color:rgb(255,0,0)">下拉列表中选项的可见文本</span><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px">选 中"浦东"这一项</span><br style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px">Select selectTown = new Select(dr.findElement(By.id("User_Town")));</span><br style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px">selectTown.</span><span style="word-wrap:normal; word-break:normal; line-height:21px; font-family:simsun; font-size:14px; color:rgb(255,0,0)">selectByVisibleText</span><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px">("浦东");</span><span style="word-wrap:normal; word-break:normal; line-height:21px; font-family:simsun; font-size:14px; color:rgb(255,0,0)">Select.selectByVisibleText</span><br style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><br style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px">//这里只是想遍历一下下拉列表所有选项,用click进行选中选项</span><br style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px">Select selectCity = new Select(dr.findElement(By.id("User_City")));</span><br style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px">for(WebElement e : selectCity.</span><span style="word-wrap:normal; word-break:normal; line-height:21px; font-family:simsun; font-size:14px; color:rgb(255,0,0)">getOptions</span><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px">())//</span><span style="word-wrap:normal; word-break:normal; line-height:21px; font-family:simsun; font-size:14px; color:rgb(255,0,0)">Select.getOptions()</span><br style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px">e.click();</span><br style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px">}</span><br style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px">}</span><br style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><br style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="word-wrap:normal; word-break:normal; line-height:21px; font-family:simsun; font-size:14px">三、单选项(Radio Button)<br> //找到单选框元素:<br> WebElement bookMode =driver.findElement(By.id("BookMode"));<br> //选择某个单选项:<br> bookMode.click();<br> //清空某个单选项:<br> bookMode.clear();<br> //判断某个单选项是否已经被选择:<br> bookMode.isSelected();<br><br> 四、多选项(checkbox)<br> //多选项的操作和单选的差不多:<br> WebElement checkbox =driver.findElement(By.id("myCheckbox."));<br> checkbox.click();<br> checkbox.clear();<br> checkbox.isSelected();<br> checkbox.isEnabled();<br><br> 五、按钮(button)<br> //找到按钮元素:<br> WebElement saveButton = driver.findElement(By.id("save"));<br> //点击按钮:<br> saveButton.click();<br> //判断按钮是否enable:<br> saveButton.isEnabled ();<br><br> 六、左右选择框<br> 也就是左边是可供选择项,选择后移动到右边的框中,反之亦然。<br> 例如:<br> Select lang = new Select(driver.findElement(By.id("languages")));<br> lang.selectByVisibleText(“English”);<br> WebElement addLanguage =driver.findElement(By.id("addButton"));<br> addLanguage.click();<br><br> 七、弹出对话框(Popup dialogs)<br> Alert alert = driver.switchTo().alert();<br> alert.accept();<br> alert.dismiss();<br> alert.getText();<br><br> 八、表单(Form)<br> Form中的元素的操作和其它的元素操作一样,对元素操作完成后对表单的提交可以:<br> WebElement approve = driver.findElement(By.id("approve"));<br> approve.click();<br> 或<br> approve.submit();//只适合于表单的提交<br><br> 九、上传文件 (Upload File)<br> 上传文件的元素操作:<br> WebElement adFileUpload = driver.findElement(By.id("WAP-upload"));<br> String filePath = "C:\test\\uploadfile\\media_ads\\test.jpg";<br> adFileUpload.sendKeys(filePath);<br><br><br> 十、拖拉(Drag andDrop)<br> WebElement element =driver.findElement(By.name("source"));<br> WebElement target = driver.findElement(By.name("target"));<br><span style="word-wrap:normal; word-break:normal; line-height:21px; color:rgb(255,0,0)">(new Actions(driver)).dragAndDrop(element, target).perform();</span><br><br> 例如:下面这个页面是一个演示拖放元素的页面,你可以把左右页面中的条目拖放到右边的div框中。<br> http://koyoz.com/demo/html/drag-drop/drag-drop.html<br><br> import org.openqa.selenium.By;<br> import org.openqa.selenium.WebDriver;<br> import org.openqa.selenium.WebElement;<br> import org.openqa.selenium.firefox.FirefoxDriver;<br> import org.openqa.selenium.interactions.Actions;<br><br> public class DragAndDrop {<br><wbr><wbr><wbr>public static void main(String[] args) {<br><wbr><wbr><wbr><wbr><wbr><wbr>System.setProperty("webdriver.firefox.bin","D:\\Program Files\\Mozilla Firefox\\firefox.exe");<wbr><br><wbr><wbr><wbr><wbr><wbr><wbr>WebDriver dr = new FirefoxDriver();<br><wbr><wbr><wbr><wbr><wbr><wbr>dr.get("http://koyoz.com/demo/html/drag-drop/drag-drop.html");<br><wbr><wbr><wbr><wbr><wbr><wbr><br><wbr><wbr><wbr><wbr><wbr><wbr>//首先new出要拖入的页面元素对象和目标对象,然后进行拖入。<br><wbr><wbr><wbr><wbr><wbr><wbr>WebElement<wbr>element = dr.findElement(By.id("item1"));<br><wbr><wbr><wbr><wbr><wbr><wbr>WebElement<wbr>target = dr.findElement(By.id("drop"));<br><wbr><wbr><wbr><wbr><wbr><wbr>(new Actions(dr)).<span style="word-wrap:normal; word-break:normal; line-height:21px; color:rgb(255,0,0)">dragAndDrop</span>(element, target).perform();<br><wbr><wbr><wbr><wbr><wbr><wbr><br><wbr><wbr><wbr><wbr><wbr><wbr>//利用循环把其它item也拖入<br><wbr><wbr><wbr><wbr><wbr><wbr>String id="item" ;<br><wbr><wbr><wbr><wbr><wbr><wbr>for(int i=2;i&lt;=6;i++){<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>String item = id+i;<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>(new Actions(dr)).dragAndDrop(dr.findElement(By.id(item)), target).perform();<br><wbr><wbr><wbr><wbr><wbr><wbr>}<br><wbr><wbr><wbr>}<br> }<br> 代码很简单,需要注意的是(new Actions(dr)).dragAndDrop(element, target).perform();这句话中,<span style="word-wrap:normal; word-break:normal; line-height:21px; color:rgb(255,0,0)">dragAndDrop(element, target)这个方法是定义了“点击element元素对象,然后保持住,直到拖到目标元素对象里面才松开”这一系列动作的Actions,如果你不调用perform()方法,这个Actions是不会执行的。</span><br><br><br> 十一、导航 (Navigationand History)<br> //打开一个新的页面:<br><wbr>driver.navigate().to("http://www.example.com");<br> //通过历史导航返回原页面:<br> driver.navigate().forward();<br> driver.navigate().back();</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值