/*
* 多种元素定位方式
*/
package com.sfwork;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class PageObjectModel {
public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub
//设置webdriver路径
System.setProperty("webdriver.chrome.drvier",
"C:\\Program Files (x86)\\Google\\Chrome\\Application\\"
+ "chromedriver.exe");
//创建webdriver对象,控制浏览器
WebDriver driver = new ChromeDriver();
//打开浏览器
driver.navigate().to("http://www.baidu.com");
//浏览器最大化
driver.manage().window().maximize();
//声明一个web元素
WebElement input;
WebElement link;
//通过ID定位百度输入框
// input = driver.findElement(By.id("kw"));
//1、通过name定位元素
// input = driver.findElement(By.name("wd"));
//2、通过XPath定位元素
// input = driver.findElement(By.xpath("//*[@id='kw']"));
//3、通过cssSelector定位元素
input = driver.findElement(By.cssSelector("#kw"));
//4、通过classname定位元素
// input = driver.findElement(By.className("s_ipt"));
//5、通过TagName定位元素
//通过input属性查找的元素有多个,所以要放到集合里,并且findElements要用复数
List<WebElement> tagname = driver.findElements(By.tagName("input"));
System.out.println(tagname.size());
for(int i = 0; i<tagname.size(); i++){
System.out.println(tagname.get(i));
if(i == 7){
tagname.get(i).sendKeys("selenium");
}
}
//得到输入框的name
// String str1 = input.getAttribute("Name");
// System.out.println(str1);
// //输入开源力量
// input.sendKeys("开源力量");
//6、通过LinkText定位链接
// link = driver.findElement(By.linkText("新闻"));
// link.click();
//7、通过partialLinkText定位元素 ,模糊定位,如果有多个,自动匹配到第一个
// link = driver.findElement(By.partialLinkText("地"));
// link.click();
//让线程休眠3秒
Thread.sleep(3000);
//关闭driver
driver.close();
}
}