selenium webdriver学习(四)------------定位页面元素

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

  • 单个对象的定位方法
  • 多个对象的定位方法
  • 层级定位                        

定位单个元素

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

 

  •         By.className(className))    
  •         By.cssSelector(selector)       
  •         By.id(id)                     
  •         By.linkText(linkText)          
  •         By.name(name)             
  •         By.partialLinkText(linkText)
  •         By.tagName(name)       
  •         By.xpath(xpathExpression)  

注意:selenium-webdriver通过findElement()\findElements()等find方法调用"By"对象来定位和查询元素。By类只是提供查询的方式进行分类。findElement返回一个元素对象否则抛出异常,findElements返回符合条件的元素List,如果不存在符合条件的就返回一个空的list。

使用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
 
使用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 e = dr.findElement(By.name("passport_51_user"));

 

使用css属性定位

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

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

使用css定位

WebElement e1 = dr.findElement(By.cssSelector("#passport_51_user"));
 
使用其他方式定位

在定位link元素的时候,可以使用link和link_text属性;

另外还可以使用tag_name属性定位任意元素;

 

 

定位多个元素

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

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 FindElementsStudy {

	/**
	 * @author gongjf
	 */
	public static void main(String[] args) {
		WebDriver  driver = new FirefoxDriver();
		driver.get("http://www.51.com");
		
		//定位到所有<input>标签的元素,然后输出他们的id
		List<WebElement> element = driver.findElements(By.tagName("input"));
		for (WebElement e : element){
			System.out.println(e.getAttribute("id"));
		}
		
		driver.quit();
	}
}

输出结果:

passport_cookie_login
gourl
passport_login_from
passport_51_user
passport_51_password
passport_qq_login_2
btn_reg
passport_51_ishidden
passport_auto_login
 

上面的代码返回页面上所有input对象。很简单,没什么可说的。

 

层级定位

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

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

下面的代码演示了如何使用层级定位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());
	
	}
        
}

输出结果:

帐号:
密码:
隐身
下次自动登录                                                                                                                                             

 

定位页面元素over了,下次写一下对frame的处理。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值