最近学习selenium 2 ,以下代码用于实现的基本的功能,其中navigation.to("http://www.baidu.com");在我练习的时候经常更换地址,完成一个小功能我就对代码进行注释,不能直接运行。
基于Eclipse Java EE IDE for Web Developers.
Version: Mars.2 Release (4.5.2)
package ST;
import org.openqa.selenium.*;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriver.Navigation;
import org.openqa.selenium.firefox.*;
public class test2{
public static void main(String[] args)
{
WebDriver driver = new FirefoxDriver();
Navigation navigation = driver.navigate();
navigation.to("http://www.baidu.com");
/*navigation.to("http://www.google.com.hk");
try{
Thread.sleep(3000);
}
catch(InterruptedException e){
e.printStackTrace();
}
//页面后退
navigation.back();
try{
Thread.sleep(3000);
}
catch(InterruptedException e){
e.printStackTrace();
}
//页面前进
navigation.forward();
//页面刷新
navigation.refresh();*/
//定位至百度文本框,在文本框输入find it
/*WebElement baiduTextBox = driver.findElement(By.id("kw"));
baiduTextBox.sendKeys("find it");*/
//清除文本框中的find it
/*try{
Thread.sleep(3000);
}
catch(InterruptedException e){
e.printStackTrace();
}
baiduTextBox.clear();*/
//精确查询
WebElement baiduLogin = driver.findElement(By.linkText("登录"));
baiduLogin.click();
//模糊查询
/*WebElement baiduLogin = driver.findElement(By.partialLinkText("录"));
baiduLogin.click();*/
//使用class定位
/*try{
Thread.sleep(3000);
}
catch(InterruptedException e){
e.printStackTrace();
}
WebElement tieba = driver.findElement(By.className("senior-search-link"));
tieba.click();
*/
//selenium 2中没有Check/Uncheck方法,用click事件来勾选或者取消选中
/*WebElement remember = driver.findElement(By.id("TANGRAM__PSP_3__memberPass"));
remember.click();*/
//以百度贴吧->高级搜素->下拉框为例,选中按相关性排序
/*WebElement select = driver.findElement(By.name("sm"));
String targetText = "按相关性排序";
java.util.List<WebElement> options = select.findElements(By.tagName("option"));
for (int i = 0; i < options.size(); i++){
if(options.get(i).getText().equals(targetText))
{
options.get(i).click();
}
}*/
//以百度的登录为例
/*WebElement username = driver.findElement(By.id("TANGRAM__PSP_8__userName"));
username.sendKeys(" your username");
WebElement password = driver.findElement(By.id("TANGRAM__PSP_8__password"));
password.sendKeys(" your password");
//可以采取上诉的找到提交按钮单击登录,也可以对html文件中的form的任意元素进行提交也可以登录成功
password.submit();*/
}
}