webdriver中操作元素跟selenium 1.0 不一样,例如selenium1.0中点击事件selenium.Click("id=lb"),是说点击id属性值为“lb”的页面元素,
而webdriver中得先去定位页面元素才能去操作页面元素,例如
driver.findElement(ById. id(
"lb"
)).Click();
1.Click()
Click()为鼠标单击事件。
例子以百度首页,点击登录为操作:
import org.junit.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.By.ById;
import org.openqa.selenium.chrome.ChromeDriver;
public class ElementOperate {
/**
* 通过ID定位,并点击登录标签
*
*/
@Test
public void byID() throws InterruptedException {
// 设置chromedriver的路径,根据你具体存放位置来设置路径
System. setProperty("webdriver.chrome.driver", "C:\\holmosconf\\driverServers\\chromedriver.exe" );
// 启动Chrome浏览器
WebDriver driver = new ChromeDriver();
// get方式打开百度首页
driver.get( "http://www.baidu.com" );
// 定位“登陆”标签
WebElement baiduLogin = driver.findElement(ById. id("lb"));
// click为点击操作
baiduLogin.click();
// 为了看效果,等待3S
Thread. sleep(3000);
// 结束测试
driver.quit();
}
}
2.SendKeys(“字符串”)
SendKeys()为文本框写入文本。
例子:以百度文本框输入“selenium”为例子
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.By.ById;
import org.openqa.selenium.chrome.ChromeDriver;
public class ElementOperate {
/**
* 通过ID定位百度首页文本框,并输入关键字
*/
@Test
public void sendKeys() throws InterruptedException {
// 设置chromedriver的路径,根据你具体存放位置来设置路径
System. setProperty("webdriver.chrome.driver", "C:\\holmosconf\\driverServers\\chromedriver.exe" );
// 启动Chrome浏览器
WebDriver driver = new ChromeDriver();
// get方式打开百度首页
driver.get( "http://www.baidu.com" );
// 定位百度搜索框
WebElement baiduTextBox=driver.findElement(By. id("kw"));
// 搜索文本框输入“selenium”
baiduTextBox.sendKeys( "selenium");
// 为了看效果,等待3S
Thread. sleep(3000);
// 结束测试
driver.quit();
}
}
3.submit()
提交表单,提交的是定位元素所在的from表单,测试的时候习惯还是用点击的方式去提交表单,所有这个方法很少使用。
例子:百度邮箱账号注册页面,当只输入账号密码未能输入验证码直接提交表单,会给验证码提示
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.By.ById;
import org.openqa.selenium.chrome.ChromeDriver;
public class ElementOperate {
/**
* sumit例子,以百度邮箱注册账号为例
* @throws InterruptedException
*/
@Test
public void sumitTest() throws InterruptedException {
// 设置chromedriver的路径,根据你具体存放位置来设置路径
System. setProperty("webdriver.chrome.driver", "C:\\holmosconf\\driverServers\\chromedriver.exe" );
// 启动Chrome浏览器
WebDriver driver = new ChromeDriver();
// get方式打开百度邮箱注册页面
driver.get( "https://passport.baidu.com/v2/?reg®Type=1&tpl=mn&u=http%3A%2F%2Fwww.baidu.com%2F#mail" );
// 定位邮箱文本框
WebElement baiduEmailTextBox = driver.findElement(By.id("TANGRAM__4__email"));
// 邮箱文本框输入“123123@qq.com”
baiduEmailTextBox.sendKeys( "123123@qq.com");
// 定位密码文本框,并输入“123asdf”
driver.findElement(By. id("TANGRAM__4__password")).sendKeys( "123asdf");
// sumit提交表单
baiduEmailTextBox.submit();
// 为了看效果,等待5S
Thread. sleep(5000);
// 结束测试
driver.quit();
}
}
4.clean()
clean()方法为清除文本框数据。
例子:百度搜索框,输入后等待3S做清除数据操作。
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.By.ById;
import org.openqa.selenium.chrome.ChromeDriver;
public class ElementOperate {
/**
* 通过百度首页搜索文本框输入关键字
* 再清除关键字
*/
@Test
public void cleanTest () throws InterruptedException {
// 设置chromedriver的路径,根据你具体存放位置来设置路径
System. setProperty("webdriver.chrome.driver", "C:\\holmosconf\\driverServers\\chromedriver.exe" );
// 启动Chrome浏览器
WebDriver driver = new ChromeDriver();
// get方式打开百度首页
driver.get( "http://www.baidu.com" );
// 定位百度搜索框
WebElement baiduTextBox = driver.findElement(By. id("kw"));
// 搜索文本框输入“selenium”
baiduTextBox.sendKeys( "selenium");
// 为了看效果,等待3S
Thread. sleep(3000);
// 做清除操作
baiduTextBox.clear();
// 为了看效果,等待3S
Thread. sleep(3000);
// 结束测试
driver.quit();
}
}
5.下拉文本框选取有三种方法:
1)
selectByIndex(
) 根据索引来选取,从0开始
2)
selectByValue(
) 根据属性value的属性值来选取
3)
selectByVisibleText(
)根据标签之间的Text值,也就是页面显示的值
例子:百度搜索设置页面,选取每页显示的搜索结果数
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.By.ById;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.Select;
public class ElementOperate {
/**
* 百度搜索设置页面,选取每页显示搜索结果数目
* @throws InterruptedException
*/
@Test
public void selectTest() throws InterruptedException {
// 设置chromedriver的路径,根据你具体存放位置来设置路径
System. setProperty( "webdriver.chrome.driver", "C:\\holmosconf\\driverServers\\chromedriver.exe" );
// 启动Chrome浏览器
WebDriver driver = new ChromeDriver();
// 打开百度的搜索设置页面
driver.get( "http://www.baidu.com/gaoji/preferences.html" );
// 定位到搜索结果显示条目数的下拉选项框
Select select = new Select(driver.findElement(By.id( "nr" )));
// 等待5S看结果
Thread. sleep(5000);
// 用selectByIndex()方法选择第二个下拉框值(每页显示20条)
select.selectByIndex(1);
// 等待5S看结果
Thread. sleep(5000);
// 用selectByValue()方法选择第3个下拉框值(每页显示50条)
select.selectByValue( "50" );
// 等待5S看结果
Thread. sleep(5000);
// 用selectByVisibleText()方法选择第4个下拉框值(每页显示100条)
select.selectByVisibleText( "每页显示100条" );
// 等待5S看结果
Thread. sleep(5000);
// 结束测试
driver.quit();
}
}
6.getText()
getText()用于获取某个元素的文本值。
例子:取百度首页“搜索设置”四个字,查看HTML源码:
<a name="tj_setting" href="http://www.baidu.com/gaoji/preferences.html">搜索设置</a>
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class ElementOperate {
/**
* 百度首页,获取“百度设置”文本
*/
@Test
public void getTextTest(){
// 设置chromedriver的路径,根据你具体存放位置来设置路径
System. setProperty( "webdriver.chrome.driver", "C:\\holmosconf\\driverServers\\chromedriver.exe" );
// 启动Chrome浏览器
WebDriver driver = new ChromeDriver();
// get方式打开百度首页
driver.get( "http://www.baidu.com" );
// 定位"搜索设置"标签
WebElement baidu = driver.findElement(By. name( "tj_setting"));
// 获取文本,并打印出来
System. out .println(baidu.getText());
// 结束测试
driver.quit();
}
}
运行结束可以查看控制台发现,打印输出了:搜索设置
7.getAttribute()
getAttribute()获取某个元素的某一个属性值:
例子:百度首页的“百度一下”按钮的html,我们想获取Value的属性值“百度一下”
<input type="submit" οnmοuseοut="this.className='bg s_btn'" οnmοusedοwn="this.className='bg s_btn s_btn_h'" class="bg s_btn" id="su" value="百度一下">
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class ElementOperate {
/**
* 百度首页,获取“百度一下”按钮的Value属性值
*/
@Test
public void getAttributeTest (){
// 设置chromedriver的路径,根据你具体存放位置来设置路径
System. setProperty( "webdriver.chrome.driver", "C:\\holmosconf\\driverServers\\chromedriver.exe" );
// 启动Chrome浏览器
WebDriver driver = new ChromeDriver();
// get方式打开百度首页
driver.get( "http://www.baidu.com" );
// 通过ID定位"百度一下"按钮
WebElement baidu = driver.findElement(By. id( "su"));
// 获取value属性值,并打印出来
System. out .println(baidu.getAttribute( "value"));
// 结束测试
driver.quit();
}
}
运行结束直接查看控制台输出,发现打印出“百度一下”文本。