Selenium常用API的使用--Java语言

目录

1.基本元素定位

1.1 id定位

1.2 name定位

1.3 tagName定位

1.4 className定位

1.5 LinkText()定位

2.cssSlector定位

2.1 根据tagName

2.2 根据ID

2.3 根据className(样式名)

2.4 css精确定位 

2.4.1 根据元素属性,属性名=属性值,id,class,等都可写成这种形式

2.4.2 多属性

3.xpath定位

3.1 绝对路径

3.2 相对路径

 4.元素操作API

4.1 click()

4.2 clear()

4.3 sendkeys(...)

4.4 getTagName()

4.5 getAttribute(属性名)

4.6 getText()

4.7 isDisplayed()


1.基本元素定位

1.1 id定位

package com.example.demo;

import org.junit.jupiter.api.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.firefox.FirefoxDriver;

public class ElementLocate {
    private static FirefoxDriver firefoxDriver;
    @Test
    public void testFindElementById(){
        openFirefox();
        //定位到百度的搜索框元素,并且输入数据(ID定位)---唯一的
        firefoxDriver.findElement(By.id("kw")).sendKeys("selenium");
    }

    public static void openFirefox() {
        firefoxDriver = new FirefoxDriver();
        firefoxDriver.get("https://www.baidu.com");
    }
}

1.2 name定位

package com.example.demo;

import org.junit.jupiter.api.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.firefox.FirefoxDriver;

public class ElementLocate {
    private static FirefoxDriver firefoxDriver;

    @Test
    public void testFindElementByName(){
        openFirefox();
        //定位到百度的搜索框元素,并且输入数据(Name定位)---重复
        firefoxDriver.findElement(By.name("wd")).sendKeys("selenium");
    }

    public static void openFirefox() {
        firefoxDriver = new FirefoxDriver();
        firefoxDriver.get("https://www.baidu.com");
    }
}

1.3 tagName定位

注意:会报错:element not visible

这是因为当tagName不唯一的时候,会定位到当前html页面的第一个所选属性

在通过TagName定位元素的时候,他不是定位到百度搜索框了,而是定位到html当中的第一个input元素,而第一个input元素是不可见的,所以会报这个错误

package com.example.demo;

import org.junit.jupiter.api.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.firefox.FirefoxDriver;

public class ElementLocate {
    private static FirefoxDriver firefoxDriver;
   
    @Test
    public void testFindElementByTagName(){
        openFirefox();
        //定位到百度的搜索框元素,并且输入数据(TagName定位)---找到的元素是会有多个--不推荐使用
        //会报错:element not visible,这是因为在通过TagName定位元素的时候,
        //他不是定位到百度搜索框了,而是定位到html当中的第一个input元素,第一个input元素是不可见的,
        //所以会报这个错误
        firefoxDriver.findElement(By.tagName("input")).sendKeys("selenium");
    }

    public static void openFirefox() {
        firefoxDriver = new FirefoxDriver();
        firefoxDriver.get("https://www.baidu.com");
    }
}

1.4 className定位

注意:当有多个className定位时候,需选择当前html页面全局唯一的那个className属性去定位

package com.example.demo;

import org.junit.jupiter.api.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.firefox.FirefoxDriver;

public class ElementLocate {
    private static FirefoxDriver firefoxDriver;

    @Test
    public void testFindElementByClassName(){
        openFirefox();
        //定位到百度的搜索框元素,并且输入数据(className定位)
        firefoxDriver.findElement(By.className("s_ipt")).sendKeys("selenium");
        //会报错:Compoud class names not permitted--->复合类名的问题(属性不止有一个)
        //解决办法:通过其中一个属性去定位,要求是该属性在当前html页面必须唯一
        firefoxDriver.findElement(By.className("bg s_btn")).click();
    }

    public static void openFirefox() {
        firefoxDriver = new FirefoxDriver();
        firefoxDriver.get("https://www.baidu.com");
    }
}

1.5 LinkText()定位

package com.example.demo;

import org.junit.jupiter.api.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.firefox.FirefoxDriver;

public class ElementLocate {
    private static FirefoxDriver firefoxDriver;

    @Test
    public void testFindElementByLinkText(){
        openFirefox();
        //定位“新闻”元素,并且点击(LinkText定位)--->超链接完整文本
        firefoxDriver.findElement(By.linkText("新闻")).click();
        //定位“新闻”元素,并且点击(partialLinkText)--->超链接部分文本
        firefoxDriver.findElement(By.partialLinkText("闻")).click();
    }

    public static void openFirefox() {
        firefoxDriver = new FirefoxDriver();
        firefoxDriver.get("https://www.baidu.com");
    }
}

2.cssSlector定位

2.1 根据tagName

package com.example.demo;

import org.junit.jupiter.api.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.firefox.FirefoxDriver;

public class ElementLocate {
    private static FirefoxDriver firefoxDriver;

    @Test
    public void testFindElementByCssSelector(){
        //cssSlector定位
        openFirefox();
        //(1)通过tagName定位--->不推荐
        //会报错,具体原因和上述tagName定位一致,这里不再赘述
        firefoxDriver.findElement(By.cssSelector("input"));
    }

    public static void openFirefox() {
        firefoxDriver = new FirefoxDriver();
        firefoxDriver.get("https://www.baidu.com");
    }
}

2.2 根据ID

package com.example.demo;

import org.junit.jupiter.api.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.firefox.FirefoxDriver;

public class ElementLocate {
    private static FirefoxDriver firefoxDriver;

    @Test
    public void testFindElementByCssSelector(){
        //cssSlector定位
        openFirefox();
        
        //(2)通过id定位,注意:需加#
        firefoxDriver.findElement(By.cssSelector("#kw")).sendKeys("selenium");
    }

    public static void openFirefox() {
        firefoxDriver = new FirefoxDriver();
        firefoxDriver.get("https://www.baidu.com");
    }
}

2.3 根据className(样式名)

package com.example.demo;

import org.junit.jupiter.api.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.firefox.FirefoxDriver;

public class ElementLocate {
    private static FirefoxDriver firefoxDriver;

    @Test
    public void testFindElementByCssSelector(){
        //cssSlector定位
        openFirefox();
        //(3)通过className定位,注意:需加.
        //如果属性不唯一,需给每个属性前面都加.
        firefoxDriver.findElement(By.cssSelector(".s_ipt")).sendKeys("selenium");
    }

    public static void openFirefox() {
        firefoxDriver = new FirefoxDriver();
        firefoxDriver.get("https://www.baidu.com");
    }
}

2.4 css精确定位 

2.4.1 根据元素属性,属性名=属性值,id,class,等都可写成这种形式

语法:By.cssSelector("标签名[属性名='属性值']");

如:By.cssSelector("input[name='xxx']");

举个例子:定位百度搜索框,我们可以通过属性名=属性值精确定位

    @Test
    public void testCssSelector(){
        openFirefox();
        firefoxDriver.findElement(By.cssSelector("input[maxlength='255']")).sendKeys("selenium");
    }

2.4.2 多属性

语法:By.cssSelector("标签名[属性1='属性值'][属性2]='属性值']");

举个例子:同样是定位百度搜索框,我们可以通过两个属性来精确定位

    @Test
    public void testCssSelector(){
        openFirefox();
        
        firefoxDriver.findElement(By.cssSelector("input[maxlength='255'][autocomplete='off']")).sendKeys("selenium");
    }

3.xpath定位

  • xpath其实就是一个path(路径),一个描述页面元素位置信息的路径,相当于元素的坐标
  • xpath基于XML文档树状结构,是XML路径语言,用来查询XML文档中的节点

3.1 绝对路径

从根开始找--/(根目录)/html/body/div[2]/div/form/div[5]/button

缺点:一旦页面结构发生变化(比如重新设计时,路径少了两节),该路径也随之失效,必须重新写,不推荐使用

3.2 相对路径

xpath相对路径:只要不是/开始的,就是相对路径//*[@id="kw"]

路径解释:

  • //匹配指定节点,不考虑他们位置(/则表示绝对路径,从根下开始)
  • *通配符,匹配任意节点元素
  • @选取属性
  • []属性判断条件表达

相对定位优点:灵活、方便、耦合性低

举个例子:定位百度搜索框

    @Test
    public void testFindElementByXpath(){
        openFirefox();
        firefoxDriver.findElement(By.xpath("//input[@maxlength='255' and @autocomplete='off']")).sendKeys("selenium");
    }

xpath也可以通过文本值定位

举个例子:定位新闻这个超链接

    @Test
    public void testFindElementByXpath(){
        openFirefox();
        firefoxDriver.findElement(By.xpath("//a[text()='新闻']")).click();
    }

也可以匹配部分文本值,还是定位新闻超链接

    @Test
    public void testFindElementByXpath(){
        openFirefox();
        firefoxDriver.findElement(By.xpath("//a[contains(text(),'新')]")).click();
    }

 4.元素操作API

4.1 click()

触发当前元素的点击事件

4.2 clear()

清空内容

4.3 sendkeys(...)

往文本框一类元素中写入内容(按键操作)

package com.example.demo;

import org.junit.jupiter.api.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.firefox.FirefoxDriver;

public class ElementOperate {
    private static FirefoxDriver firefoxDriver;

    @Test
    public void testSendKeysAndClear() throws InterruptedException {
        openFirefox();
        firefoxDriver.findElement(By.id("kw")).sendKeys("selenium");
        //让当前线程等待3s
        Thread.sleep(3000);
        firefoxDriver.findElement(By.id("kw")).clear();
    }

    public static void openFirefox() {
        firefoxDriver = new FirefoxDriver();
        firefoxDriver.get("https://www.baidu.com");
    }
}

4.4 getTagName()

获取元素的标签名

4.5 getAttribute(属性名)

根据属性名获取元素属性值

4.6 getText()

获取当前元素的文本值

4.7 isDisplayed()

查看元素是否显示

package com.example.demo;

import org.junit.jupiter.api.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class ElementOperate {
    private static FirefoxDriver firefoxDriver;

    @Test
    public void testSendKeysAndClear() throws InterruptedException {
        openFirefox();
        firefoxDriver.findElement(By.id("kw")).sendKeys("selenium");
        //让当前线程等待3s
        Thread.sleep(3000);
        firefoxDriver.findElement(By.id("kw")).clear();
    }
    @Test
    public void testOthers(){
        openFirefox();
        WebElement webElement = firefoxDriver.findElement(By.id("kw"));
        System.out.println("元素标签名:" + webElement.getTagName());
        System.out.println("元素maxlength属性:" + webElement.getAttribute("maxlength"));
        WebElement webElement1 = firefoxDriver.findElement(By.xpath("//a[text()='hao123']"));
        System.out.println("元素文本值:" + webElement1.getText());
        System.out.println("元素是否显示:"+webElement1.isDisplayed());
    }

    public static void openFirefox() {
        firefoxDriver = new FirefoxDriver();
        firefoxDriver.get("https://www.baidu.com");
    }
}

  • 5
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
Selenium WebDriver 是一种用于自动化Web应用程序测试的工具,它提供了一系列的API,用于操作浏览器来模拟用户的行为。它是基于Java编写的,因此可以使用Java语言来编写测试脚本。 Selenium WebDriver API 提供了一些常用方法来控制浏览器,如打开网址、在输入框中输入文本、点击按钮等。这些方法可以让我们对页面上的元素进行定位和操作,以模拟用户在浏览器中的行为。 使用 Selenium WebDriver API,我们可以使用多种定位方式来找到页面上的元素,如通过id、name、class等属性进行定位,也可以通过XPath和CSS选择器来定位。定位到元素后,我们可以对其执行一系列的操作,如获取元素的文本、获取元素的属性、点击元素等。 Selenium WebDriver API 还提供了一些常用的操作方法,如切换窗口、切换到iframe、执行JavaScript等。这些方法可以帮助我们处理一些特殊的页面场景,如弹窗、嵌套的iframe等。 使用 Selenium WebDriver API,我们可以编写可维护和可扩展的测试脚本。通过使用Java语言的面向对象特性,我们可以将测试代码进行模块化,使其更加易读和易于维护。此外,Selenium WebDriver 还支持多种浏览器,如Chrome、Firefox等,我们可以轻松地在不同的浏览器上运行测试脚本。 总之,Selenium WebDriver API 提供了丰富的功能和强大的灵活性,使得我们可以编写高效和可靠的自动化测试脚本。无论是对于新手还是有经验的测试人员来说,掌握这些API是非常重要的。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值