文章目录
1.元素定位与操作
查找元素方法:
driver.findElement(By arg0);
其中写用来定位的API,常见用来定位的API有六种,接下来一一介绍:
1.1 By.id
根据id来获取元素,返回单个元素,因为id值一般是唯一的。
//获得id=kw的标签并且赋值给input变量
WebElement input=driver.findElement(By.id("kw"));
1.2 By.name
根据元素的name属性来获得元素,所以可能返回元素集合
WebElement input=driver.findElement(By.name("kw"));
1.3 By.tagName
根据标签名来获取元素,可能返回一个元素集合
List<WebElement> list =driver.findElements(By.tagName("input"));
//得到第八个元素
list.get(7);
1.4 By.className
根据元素的样式class来获取元素,可能返回一个元素集合
List<WebElement> list =driver.findElements(By.className("input"));
list.get(7);
1.4 By.LinkText
根据超链接的文本值来获取元素
//根据完整文本内容
//WebElement element=driver.findElement(By.linkText("新闻"));
//根据部分文本内容定位
WebElement element1=driver.findElement(By.partialLinkText("新"));
element1.click();
1.5 By.cssSelector
- 通过标签名称和其className进行查找