selenium

在使用selenium webdriver进行元素定位时,一般使用findElement或findElements方法结合By类返回的元素句柄来定位元素。其中By类的经常使用定位方式共八种css

1. By.name()html

假设咱们要测试的页面源码以下:程序员

<button id="gbqfba" aria-label="Google Search" name="btnK" class="gbqfba"><span id="gbqfsa">Google Search</span></button>

当咱们要用name属性来引用这个button并点击它时,代码以下:web

1 public class SearchButtonByName {
2         public static void main(String[] args){
3                WebDriver driver = new FirefoxDriver();
4                driver.get("http://www.forexample.com");
5                WebElement searchBox = driver.findElement(By.name("btnK"));
6                searchBox.click();
7         }
8 }

2. By.id()
 1 public class SearchButtonById {
 2 
 3         public static void main(String[] args){
 4 
 5             WebDriver driver = new FirefoxDriver();
 6 
 7             driver.get("http://www.forexample.com");
 8 
 9             WebElement searchBox = driver.findElement(By.id("gbqfba"));
10 
11             searchBox.click();
12 
13         }
14 
15 }

3. By.tagName()浏览器

该方法能够经过元素的标签名称来查找元素。该方法跟以前两个方法的区别是,这个方法搜索到的元素一般不止一个,因此通常建议结合使用findElements方法来使用。好比咱们如今要查找页面上有多少个button,就能够用button这个tagName来进行查找,代码以下:测试

public class SearchPageByTagName{

     public static void main(String[] args){

            WebDriver driver = new FirefoxDriver();
            driver.get("http://www.forexample.com");
            List<WebElement> buttons = driver.findElements(By.tagName("button"));

            System.out.println(buttons.size());  //打印出button的个数

    }

}                    
复制代码 4. By.className()

className属性是利用元素的css样式表所引用的伪类名称来进行元素查找的方法。对于任何HTML页面的元素来讲,通常程序员或页面设计师会给元素直接赋予一个样式属性或者利用css文件里的伪类来定义元素样式,使元素在页面上显示时可以更加美观。通常css样式表可能会长成下面这个样子:spa

 1 .buttonStyle{
 2 
 3     width: 50px;
 4 
 5     height: 50px;
 6 
 7     border-radius: 50%;
 8 
 9     margin: 0% 2%;
10 
11 }
复制代码

定义好后,就能够在页面元素中引用上述定义好的样式,以下:设计

1 <button name="sampleBtnName" id="sampleBtnId" class="buttonStyle">I'm Button</button>

 

若是此时咱们要经过className属性来查找该button并操做它的话,就可使用className属性了,代码以下:code

复制代码
 1 public class SearchElementsByClassName{
 2 
 3     public static void main(String[] args){
 4 
 5         WebDriver driver = new FirefoxDriver();
 6 
 7         driver.get("http://www.forexample.com");
 8 
 9         WebElement searchBox =  driver.findElement(By.className("buttonStyle"));
10 
11         searchBox.sendKeys("Hello, world");
12 
13     }
14 
15 }
复制代码
 

5. By.linkText()orm

 

这个方法比较直接,即经过超文本连接上的文字信息来定位元素,这种方式通常专门用于定位页面上的超文本连接。一般一个超文本连接会长成这个样子:

 
1 <a href="/intl/en/about.html">About Google</a>

 

咱们定位这个元素时,可使用下面的代码进行操做:

 
复制代码
 1 public class SearchElementsByLinkText{
 2 
 3     public static void main(String[] args){
 4 
 5         WebDriver driver = new FirefoxDriver();
 6 
 7         driver.get("http://www.forexample.com");
 8 
 9         WebElement aboutLink = driver.findElement(By.linkText("About Google"));
10 
11         aboutLink.click();
12 
13     }
14 
15 }
复制代码
6. By.partialLinkText()

这个方法是上一个方法的扩展。当你不能准确知道超连接上的文本信息或者只想经过一些关键字进行匹配时,可使用这个方法来经过部分连接文字进行匹配。代码以下:

 
复制代码
 1 public class SearchElementsByPartialLinkText{
 2 
 3     public static void main(String[] args){
 4 
 5         WebDriver driver = new FirefoxDriver();
 6 
 7         driver.get("http://www.forexample.com");
 8 
 9         WebElement aboutLink = driver.findElement(By.partialLinkText("About"));
10 
11         aboutLink.click();
12 
13     }
14 
15 }

复制代码
 

7. By.xpath()

 

这个方法是很是强大的元素查找方式,使用这种方法几乎能够定位到页面上的任意元素。在正式开始使用XPath进行定位前,咱们先了解下什么是XPath。XPath是XML Path的简称,因为HTML文档自己就是一个标准的XML页面,因此咱们可使用XPath的语法来定位页面元素。

 

假设咱们如今以图(2)所示HTML代码为例,要引用对应的对象,XPath语法以下:

 

 

 

图(2)

 

绝对路径写法(只有一种),写法以下:

 

引用页面上的form元素(即源码中的第3行):/html/body/form[1]

8. By.cssSelector()

ssSelector这种元素定位方式跟xpath比较相似,但执行速度较快,并且各类浏览器对它的支持都至关到位,因此功能也是蛮强大的。

 

下面是一些常见的cssSelector的定位方式:

 

定位id为flrs的div元素,能够写成:#flrs     注:至关于xpath语法的//div[@id=’flrs’]

 

定位id为flrs下的a元素,能够写成 #flrs > a  注:至关于xpath语法的//div[@id=’flrs’]/a

 

定位id为flrs下的href属性值为/forexample/about.html的元素,能够写成: #flrs > a[href=”/forexample/about.html”]

 

若是须要指定多个属性值时,能够逐一加在后面,如#flrs > input[name=”username”][type=”text”]。

 

 

 

明白基本语法后,咱们来尝试用cssSelector方式来引用图(3)中选中的那个input对象,代码以下:

 
WebElement password = driver.findElement(By.cssSelector("#J_login_form>dl>dt>input[id=’ J_password’]"));
相关文章
相关标签/搜索