Selenium WebDrive学习(二)

it2023-05-19  73

Selenium定位页面元素

selenium-webdriver提供了强大的元素定位方法,支持以下三种方法:单个对象的定位方法使用id属性定位使用name属性定位使用ClassName进行定位使用css属性定位使用LinkText查找元素使用PartialLinkText 查找元素使用TagName查找元素 多个对象的定位方法层级定位

selenium-webdriver提供了强大的元素定位方法,支持以下三种方法:

写在最前面:Selenium-webdriver使用WebDriver对象的findElement函数调用"By"对象来定位和查询元素,定位的页面元素需要使用WebElement对象来存储,以便后续使用。 By类只是提供查询的方式进行分类。findElement返回一个元素对象否则抛出异常,findElements返回符合条件的元素List,如果不存在符合条件的就返回一个空的list。比较绕口举个栗子吧!

public static void main(String[] args) { //下面这个是用来调用findElement方法的WebDrive对象 WebDriver driver = new FirefoxDriver(); driver.get("http://www.51.com"); //下面这是用来储存的WebElement对象 WebElement element = driver.findElement(By.className("username")); System.out.println(element.getTagName()); }

单个对象的定位方法

在定位单个元素时,Selenium-webdriver提示了如下一些方法对元素进行定位:

使用id属性定位

51.com首页的帐号输入框的html代码如下:

<input id="passport_51_user" type="text" value="" tabindex="1" title="用户名/彩虹号/邮箱" name="passport_51_user">

在下面的例子中我们用id定位这个输入框,并输出其title,借此也可以验证代码是否工作正常。

import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; public class ByUserId { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub WebDriver dr = new FirefoxDriver(); dr.get("http://www.51.com"); WebElement element = dr.findElement(By.id("passport_51_user")); System.out.println(element.getAttribute("title")); } }

输出结果:

用户名/彩虹号/邮箱

使用name属性定位

51.com首页的帐号输入框的html代码如下:

<input id="passport_51_user" type="text" value="" tabindex="1" title="用户名/彩虹号/邮箱" name="passport_51_user">

使用name定位

WebElement element = driver.findElement(By.name("passport_51_user"));

使用ClassName进行定位

当所定位的元素具有class属性的时候我们可以通过classname来定位该元素。

下面的例子定位了51.com首页上Class为"username"的li。

import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.By; public class ByClassName { public static void main(String[] args) { WebDriver driver = new FirefoxDriver(); driver.get("http://www.51.com"); WebElement element = driver.findElement(By.className("username")); System.out.println(element.getTagName()); } }

输出结果:

li

使用css属性定位

51.com首页的帐号输入框的html代码如下:

<input id="passport_51_user" type="text" value="" tabindex="1" title="用户名/彩虹号/邮箱" name="passport_51_user">

使用css定位

WebElement element= driver.findElement(By.cssSelector("#passport_51_user"));

使用LinkText查找元素

直接通过超链接上的文字信息来定位元素html代码如下:

<a href="https://passport.baidu.com/v2/?login&amp;tpl=mn&amp;u=http%3A%2F%2Fwww.baidu.com%2F" name="tj_login" class="lb" οnclick="return false;">登录</a>

使用LinkText查找元素

WebDriver driver = new FirefoxDriver(); driver.get("http://www.baidu.com"); WebElement loginLink = driver.findElement(By.linkText("登录")); loginLink.click();

使用PartialLinkText 查找元素

此方法是上一个方法的加强版,当你只想用一些关键字匹配的时候,可以使用这个方法,通过部分超链接文字来定位元素。HTML 代码如下:

<a href="https://passport.baidu.com/v2/?login&amp;tpl=mn&amp;u=http%3A%2F%2Fwww.baidu.com%2F" name="tj_login" class="lb" οnclick="return false;">登录</a>

使用PartialLinkText 查找元素,此时我想定位的关键字为 “登”

WebDriver driver = new FirefoxDriver(); driver.get("http://www.baidu.com"); WebElement loginLink = driver.findElement(By.partialLinkText("登")); loginLink.click();

注意:用PartialLinkTex方法定位时,可能会引起的问题是,当你的页面中不知一个超链接包含 “登” 时, findElement 方法只会返回第一个查找到的元素,而不会返回所有符合条件的元素。

如果你想要获得所有符合条件的元素,还是只能用findElements方法(多一个s)

使用TagName查找元素

通过tagName来搜索元素的时候,会返回多个元素. 因此需要使用findElements();

WebDriver driver = new FirefoxDriver(); driver.get("http://www.cnblogs.com"); List<WebElement> buttons = driver.findElements(By.tagName("div")); System.out.println("Button:" + buttons.size());

注意: 如果使用tagName, 要注意很多HTML元素的tagName是相同的,

比如单选框,复选框, 文本框,密码框.这些元素标签都是input. 此时单靠tagName无法精确获取我们想要的元素, 还需要结合type属性,才能过滤出我们要的元素

多个对象的定位方法

上面提到 findElements() 方法可以返回一个符合条件的元素List组。看下面例子:

WebDriver driver = new FirefoxDriver(); driver.get("http://www.cnblogs.com"); List<WebElement> buttons = driver.findElements(By.tagName("input")); for (WebElement webElement : buttons) { if (webElement.getAttribute("type").equals("text")) { System.out.println("input text is :" + webElement.getText()); } }

层级定位

层级定位的思想是先定位父元素,然后再从父元素中精确定位出其我们需要选取的子元素。

层级定位一般的应用场景是无法直接定位到需要选取的元素,但是其父元素比较容易定位,通过定位父元素再遍历其子元素选择需要的目标元素,或者需要定位某个元素下所有的子元素。

下面的代码演示了如何使用层级定位class为"login"的div,然后再取得它下面的所有label,并打印出他们的文本。

import java.io.File; import java.util.List; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxBinary; import org.openqa.selenium.firefox.FirefoxDriver; public class LayerLocator { /** * @author gongjf */ public static void main(String[] args) { WebDriver driver = new FirefoxDriver(); driver.get("http://www.51.com"); //定位class为"login"的div,然后再取得它下面的所有label,并打印出他们的值 WebElement element = driver.findElement(By.className("login")); List<WebElement> el = element.findElements(By.tagName("label")); for(WebElement e : el) System.out.println(e.getText()); } }

输出结果:

帐号: 密码: 隐身 下次自动登录
最新回复(0)