一、获取测试对象的内容
1.获取当前网页标题:String title=driver.getTitle();
2.获取当前网页的URL:String url=driver.getCurrentUrl();
3.获取元素的文本值,比如链接,纯文本等:String text=driver.findElement(By location).getText();
4.获取元素指定属性的值:String attribute=driver.findElement(By location).getAttribute("属性"); //这里的参数可以是class、name等任意属性
5.获取元素输入框内的文本值:String attribute=driver.findElement(By location).getAttribute("value");
6.获取元素标签名称:String tagName=driver.findElement(By location).getTagName();
二、获取测试对象的状态
1.是否显示:使用element.is_displayed()方法。

2.是否存在:使用find_element_by_xxx()方法,捕获其抛出的异常, 如果存在异常的话则可以确定该元素不存在。

3.是否被选中:一般判断表单元素,如radio或checkbox是否被选中,使用element.is_selected()方法(返回true代表已被选中,返回false代表未被选中)。

4.是否有效:即是否为灰化状态,使用element.is_enabled()方法(可用于判断button/checkbox/radio是否置灰)。

网友评论