第七章 Selenium Webdriver驱动页面元素实现

Selenium Webdriver驱动页面元素实现

1.如何点击链接按钮和操作文本框

package testDemo;

import org.junit.*;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

import java.util.concurrent.TimeUnit;

/**
 * @author 96510
 * @version 1.0
 * @date 2021/6/29
 */
public class NavigateingBetweenPages {
    WebDriver driver;
    String url;

    @Before
    public void setUp() throws Exception {
        driver = new ChromeDriver();
        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
    }

    @Test
    public void name() {
        url = "http://www.baidu.com";
        driver.get(url);
        driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        String title = driver.getTitle();
        System.out.println("当前的标题为:" + title);
        String currenturl = driver.getCurrentUrl();
        System.out.println("当前的url为:" + currenturl);


        String urlToNavigate = "http://news.baidu.com/";
        driver.navigate().to(urlToNavigate);
        title = driver.getTitle();
        System.out.println("当前的标题为:" + title);
        currenturl = driver.getCurrentUrl();
        System.out.println("当前的url为:" + currenturl);

        driver.navigate().back();
        title = driver.getTitle();
        System.out.println("当前的标题为:" + title);
        currenturl = driver.getCurrentUrl();
        System.out.println("当前的url为:" + currenturl);

        driver.navigate().forward();
        title = driver.getTitle();
        System.out.println("当前的标题为:" + title);
        currenturl = driver.getCurrentUrl();
        System.out.println("当前的url为:" + currenturl);


        driver.navigate().refresh();
        title = driver.getTitle();
        System.out.println("当前的标题为:" + title);
        currenturl = driver.getCurrentUrl();
        System.out.println("当前的url为:" + currenturl);
    }

    @After
    public void tearDown() throws Exception {
        System.out.println("Test执行后");
    }
}

2.页面元素的状态

package testDemo;

import org.junit.*;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

import java.util.concurrent.TimeUnit;

/**
 * @author 96510
 * @version 1.0
 * @date 2021/6/29
 */
public class ElementState {
    WebDriver driver;
    String url;

    @Before
    public void setUp() throws Exception {
        driver = new ChromeDriver();
        url= "http://www.baidu.com";
        driver.manage().window().maximize();
        driver.get(url);
        driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
    }

    @Test
    public void name() throws InterruptedException {
        WebElement e1 = driver.findElement(By.xpath("//a[@class='title-content c-link c-font-medium c-line-clamp1']"));
        if(e1.isEnabled()){//如果可以使用
            e1.click();
        }
        Thread.sleep(3);
        System.out.println("Test执行");
    }

    @After
    public void tearDown() throws Exception {
        System.out.println("Test执行后");
    }


}

3.单选框和复选框

区别单选框:

单选框type:radio

复选框type:checkbox

练习网址:https://courses.letskodeit.com/practice

单选/多选

package jssd;

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;

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

    WebDriver driver = new ChromeDriver();
    String url = "https://courses.letskodeit.com/practice";
    @Before
    public void setUp() throws Exception {
        driver.manage().window().maximize();
        driver.get(url);
        Thread.sleep(3);
    }

    @Test
    public void name() throws Exception{
        WebElement Element1 = driver.findElement(By.id("bmwradio"));
        Element1.click();
        WebElement Element2 = driver.findElement(By.id("benzradio"));
        Element2.click();
        WebElement Element3 = driver.findElement(By.id("bmwcheck"));
        Element3.click();
        WebElement Element4 = driver.findElement(By.id("benzcheck"));
        Element4.click();
        if(Element1.isSelected()){
            System.out.println("E1已经被选中了");
        }
        if(Element2.isSelected()){
            System.out.println("E2已经被选中了");
        }
        if(Element3.isSelected()){
            System.out.println("E3已经被选中了");
        }
        if(Element4.isSelected()){
            System.out.println("E4已经被选中了");
        }
        Thread.sleep(5);
    }

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

}

4.操作集合里面的Web元素(遍历所有的单选框)

package jssd;

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 java.util.List;
import java.util.concurrent.TimeUnit;

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

    WebDriver driver = new ChromeDriver();
    String url = "https://courses.letskodeit.com/practice";
    @Before
    public void setUp() throws Exception {
        driver.manage().window().maximize();
        driver.get(url);
        Thread.sleep(8);
    }

    @Test
    public void name() throws Exception{
        List<WebElement> Elements = driver.findElements(By.xpath("//input[contains(@type,'radio')and contains(@name,'cars')]"));
        boolean isCheck =false;
        int a = Elements.size();
        System.out.println(a);
        for (int i =0;i<a;i++){
            isCheck =Elements.get(i).isSelected();{
                if (!isCheck){
                    Elements.get(i).click();
//                    Thread.sleep(2);
                    driver.manage().timeouts().implicitlyWait(2, TimeUnit.SECONDS);
                    System.out.println("勾选了第"+i+"个");
                }
            }
        }
    }

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

}

5.操作下拉列表元素

针对select标签的下拉列表

package jssd;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.WebDriver;
import javax.xml.bind.Element;
import java.util.List;
import java.util.concurrent.TimeUnit;

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

    WebDriver driver = new ChromeDriver();
    String url = "https://www.ctrip.com/?sid=155952&allianceid=4897&ouid=index";
    @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("J_roomCountList"));
        Select sle =new Select(element);
        sle.selectByIndex(0);
        System.out.println("选中了一间");
        Thread.sleep(2000);
        sle.selectByValue("2");
        System.out.println("选中了二间");
        Thread.sleep(2000);
        sle.selectByVisibleText("3间");
        System.out.println("选中了三间");
        Thread.sleep(2000);
        List<WebElement> list = sle.getOptions();
        int a = list.size();
        for(int i =0;i<a;i++){
            String b =list.get(i).getText();
            System.out.println(b);
        }
    }

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

}

6.操作多选列表框

package jssd;

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

    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("multiple-select-example"));
        Select sle =new Select(element);
        sle.selectByIndex(0);
        System.out.println("选中了苹果");
        Thread.sleep(2000);
        sle.selectByValue("orange");
        System.out.println("选中了桔子");
        Thread.sleep(2000);
        sle.selectByVisibleText("桃子");
        System.out.println("选中了桃子");
        Thread.sleep(2000);
        sle.deselectByVisibleText("桃子");
        System.out.println("取消选中桃子");
        sle.deselectByIndex(0);
        System.out.println("取消选中苹果");
        Thread.sleep(2);
        List<WebElement> list = sle.getOptions();
        int a = list.size();
        for(int i =0;i<a;i++){
            String b =list.get(i).getText();
            System.out.println(b);
        }
    }

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

}

7.操作隐藏元素

package jssd;

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

    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("displayed-text"));
        if (element.isDisplayed()){
            element.sendKeys("测试");
            WebElement element1 = driver.findElement(By.id("hide-textbox"));
            Thread.sleep(3000);
            element1.click();
        }
        WebElement element2 = driver.findElement(By.id("displayed-text"));
        if(!element2.isDisplayed()){
            System.out.println("输入框是隐藏的");
            Thread.sleep(3000);
        }
    }

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值