第八章 Selenium Webdriver 有用的方法和属性

第八章 Selenium Webdriver 常用的方法和属性

1.如何取到元素上的文本
package usefulMethod;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.Select;

import java.util.List;

/**
 * @author 96510
 * @version 1.0
 * @date 2021/7/2
 */
public class GetTextDemo {

    WebDriver driver = new ChromeDriver();
    String url = "file:///E:/Chrome/8PracticePage.html";
    @Before
    public void setUp() throws Exception {
        driver.manage().window().maximize();
        driver.get(url);
        Thread.sleep(8);
    }

    @Test
    public void name() throws Exception{
        WebElement element = driver.findElement(By.cssSelector("#product > tbody > tr:nth-child(2) > td:nth-child(2)"));
//        WebElement element1 = driver.findElement(By.xpath()));
        System.out.println(element.getText());
    }

    @After
    public void tearDown() throws Exception {
            driver.quit();
    }

}
2.如何取到元素属性的值
package usefulMethod;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

/**
 * @author 96510
 * @version 1.0
 * @date 2021/7/2
 */
public class GetArrribute {

    WebDriver driver = new ChromeDriver();
    String url = "file:///E:/Chrome/8PracticePage.html";
    @Before
    public void setUp() throws Exception {
        driver.manage().window().maximize();
        driver.get(url);
        Thread.sleep(8);
    }

    @Test
    public void name() throws Exception{
        WebElement element = driver.findElement(By.id("name"));
        String type = element.getAttribute("type");
        System.out.println(type);
    }

    @After
    public void tearDown() throws Exception {
            driver.quit();
    }

}

3.封装一个查找单独元素的方法
package usefulMethod;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

/**
 * @author 96510
 * @version 1.0
 * @date 2021/7/2
 */
public class GenericMethodsDemo {
    WebDriver driver = new ChromeDriver();
    String url = "file:///E:/Chrome/8PracticePage.html";
    private GenericMethods gm;
    @Before
    public void setUp() throws Exception {
        driver.manage().window().maximize();
        driver.get(url);
        gm =new GenericMethods(driver);
        Thread.sleep(8);
    }

    @Test
    public void name() throws Exception{
        WebElement element = gm.getElement("name","id");
        String type = element.getAttribute("type");
        System.out.println(type);
    }

    @After
    public void tearDown() throws Exception {
        driver.quit();
    }
}
4.封装多个查找单独元素的方法
package usefulMethod;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

import javax.xml.bind.Element;
import java.util.List;

/**
 * @author 96510
 * @version 1.0
 * @date 2021/7/2
 */
public class GenericMethodsDemo {
    WebDriver driver = new ChromeDriver();
    String url = "file:///E:/Chrome/8PracticePage.html";
    private GenericMethods gm;
    @Before
    public void setUp() throws Exception {
        driver.manage().window().maximize();
        driver.get(url);
        gm =new GenericMethods(driver);
        Thread.sleep(8);
    }

//    @Test
//    public void name() throws Exception{
//        WebElement element = gm.getElement("name","id");
//        String type = element.getAttribute("type");
//        System.out.println(type);
//    }

    @Test
    public void testDemo() throws Exception{
        List<WebElement> element = gm.getElements("//input[@type='text']","xpath");
        int a= element.size();
//        for(WebElement element1:element){
//            System.out.println(element1.getText());
//        }
        System.out.println(a);
    }

    @After
    public void tearDown() throws Exception {
        driver.quit();
    }
}

4.封装检查元素是否在页面存在的方法
package usefulMethod;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

import java.util.List;

/**
 * @author 96510
 * @version 1.0
 * @date 2021/7/2
 */
public class GenericMethods {

    WebDriver driver;

    public GenericMethods(WebDriver driver) {
        this.driver = driver;
    }

    public WebElement getElement(String locator, String type) {
        type = type.toLowerCase(); //统一变成小写
        if (type.equals("id")) {
            System.out.println("用id查找元素" + locator);
            return this.driver.findElement(By.id(locator));
        } else if (type.equals("xpath")) {
            System.out.println("用xpath查找元素" + locator);
            return this.driver.findElement(By.xpath(locator));
        } else if (type.equals("name")) {
            System.out.println("用name查找元素: " + locator);
            return this.driver.findElement(By.name(locator));
        } else if (type.equals("css")) {
            System.out.println("用css查找元素: " + locator);
            return this.driver.findElement(By.cssSelector(locator));
        } else if (type.equals("classname")) {
            System.out.println("用classname查找元素: " + locator);
            return this.driver.findElement(By.className(locator));
        } else if (type.equals("tagname")) {
            System.out.println("用tagname查找元素: " + locator);
            return this.driver.findElement(By.tagName(locator));
        } else if (type.equals("linktext")) {
            System.out.println("用linktext查找元素: " + locator);
            return this.driver.findElement(By.linkText(locator));
        } else if (type.equals("partiallinktext")) {
            System.out.println("用partiallinktext查找元素: " + locator);
            return this.driver.findElement(By.partialLinkText(locator));
        } else {
            System.out.println("定位的路径不支持");
            return null;
        }

    }
    public List<WebElement> getElements(String locator, String type) {
        type = type.toLowerCase(); //统一变成小写
        if (type.equals("id")) {
            System.out.println("用id查找元素" + locator);
            return this.driver.findElements(By.id(locator));
        } else if (type.equals("xpath")) {
            System.out.println("用xpath查找元素" + locator);
            return this.driver.findElements(By.xpath(locator));
        } else if (type.equals("name")) {
            System.out.println("用name查找元素: " + locator);
            return this.driver.findElements(By.name(locator));
        } else if (type.equals("css")) {
            System.out.println("用css查找元素: " + locator);
            return this.driver.findElements(By.cssSelector(locator));
        } else if (type.equals("classname")) {
            System.out.println("用classname查找元素: " + locator);
            return this.driver.findElements(By.className(locator));
        } else if (type.equals("tagname")) {
            System.out.println("用tagname查找元素: " + locator);
            return this.driver.findElements(By.tagName(locator));
        } else if (type.equals("linktext")) {
            System.out.println("用linktext查找元素: " + locator);
            return this.driver.findElements(By.linkText(locator));
        } else if (type.equals("partiallinktext")) {
            System.out.println("用partiallinktext查找元素: " + locator);
            return this.driver.findElements(By.partialLinkText(locator));
        } else {
            System.out.println("定位的路径不支持");
            return null;
        }
    }
    public boolean isElementPresent(String locator, String type) {
        List<WebElement> elementList = getElements(locator, type);
        int size = elementList.size();
        if (size > 0) {
            return true;
        } else {
            return false;
        }
    }
}

package usefulMethod;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

import javax.xml.bind.Element;
import javax.xml.bind.SchemaOutputResolver;
import java.util.List;

/**
 * @author 96510
 * @version 1.0
 * @date 2021/7/2
 */
public class GenericMethodsDemo {
    WebDriver driver = new ChromeDriver();
    String url = "file:///E:/Chrome/8PracticePage.html";
    private GenericMethods gm;
    @Before
    public void setUp() throws Exception {
        driver.manage().window().maximize();
        driver.get(url);
        gm =new GenericMethods(driver);
        Thread.sleep(8);
    }

    @Test
    public void getElement() throws Exception{
        WebElement element = gm.getElement("name","id");
        String type = element.getAttribute("type");
        System.out.println(type);
    }

    @Test
    public void getElementsDemo() throws Exception{
        List<WebElement> element = gm.getElements("//input[@type='text']","xpath");
        int a= element.size();
        System.out.println(a);
    }

    @Test
    public void isDisplayDemo() throws Exception{
        boolean a = gm.isElementPresent("//input[@type='text']","xpath");
        if(a){
            System.out.println("元素存在");
        }else {
            System.out.println("元素不存在");
        }
    }

    @After
    public void tearDown() throws Exception {
        driver.quit();
    }
}

5.通用方法-代码重构
package usefulMethod;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 96510
 * @version 1.0
 * @date 2021/7/2
 */
public class NewGenericMethods {

    WebDriver driver;

    public NewGenericMethods(WebDriver driver) {
        this.driver = driver;
    }

    public WebElement getElement(String locator, String type) {
        type = type.toLowerCase();
        if (type.equals("id")) {
            System.out.println("用id查找元素:" + locator);
            return this.driver.findElement(By.id(locator));
        } else if (type.equals("name")) {
            System.out.println("用name查找元素: " + locator);
            return this.driver.findElement(By.name(locator));
        } else if (type.equals("xpath")) {
            System.out.println("用xpath查找元素: " + locator);
            return this.driver.findElement(By.xpath(locator));
        } else if (type.equals("css")) {
            System.out.println("用css查找元素: " + locator);
            return this.driver.findElement(By.cssSelector(locator));
        } else if (type.equals("classname")) {
            System.out.println("用classname查找元素: " + locator);
            return this.driver.findElement(By.className(locator));
        } else if (type.equals("tagname")) {
            System.out.println("用tagname查找元素: " + locator);
            return this.driver.findElement(By.tagName(locator));
        } else if (type.equals("linktext")) {
            System.out.println("用linktext查找元素: " + locator);
            return this.driver.findElement(By.linkText(locator));
        } else if (type.equals("partiallinktext")) {
            System.out.println("用partiallinktext查找元素: " + locator);
            return this.driver.findElement(By.partialLinkText(locator));
        } else {
            System.out.println("定位的类型不支持");
            return null;
        }
    }

    public List<WebElement> getElementList(String locator, String type) {
        type = type.toLowerCase();
        List<WebElement> elementList = new ArrayList<WebElement>();
        if (type.equals("id")) {
            elementList = this.driver.findElements(By.id(locator));
        } else if (type.equals("name")) {
            elementList = this.driver.findElements(By.name(locator));
        } else if (type.equals("xpath")) {
            elementList = this.driver.findElements(By.xpath(locator));
        } else if (type.equals("css")) {
            elementList = this.driver.findElements(By.cssSelector(locator));
        } else if (type.equals("classname")) {
            elementList = this.driver.findElements(By.className(locator));
        } else if (type.equals("tagname")) {
            elementList = this.driver.findElements(By.tagName(locator));
        } else if (type.equals("linktext")) {
            elementList = this.driver.findElements(By.linkText(locator));
        } else if (type.equals("partiallinktext")) {
            elementList = this.driver.findElements(By.partialLinkText(locator));
        } else {
            System.out.println("定位的类型不支持");
        }
        if (elementList.isEmpty()) {
            System.out.println("用 " + type + ": " + locator + " 没有找到元素");

        } else {
            System.out.println("元素用  " + type + ": " + locator + " 找到了");
        }
        return elementList;
    }

    public boolean isElementPresent(String locator, String type) {
        List<WebElement> elementList = getElementList(locator, type);

        int size = elementList.size();

        if (size > 0) {
            return true;
        } else {
            return false;
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值