zoukankan      html  css  js  c++  java
  • 章节十一、2-如何点击链接按钮和操作文本框

    以下操作基于该网站进行演示:https://piao.qunar.com/

    一、进入“去哪儿”网站,点击“登录”---》密码登录---》输入用户和密码,代码演示:

    package basicweb;
    
    import java.awt.TextField;
    import java.util.concurrent.TimeUnit;
    
    import org.junit.jupiter.api.AfterEach;
    import org.junit.jupiter.api.BeforeEach;
    import org.junit.jupiter.api.Test;
    import org.openqa.selenium.By;
    import org.openqa.selenium.Keys;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.chrome.ChromeDriver;
    import org.openqa.selenium.interactions.Actions;
    
    class BasicOperation {
    //    声明了一个webdriver类型的变量名,对象名为“driver”
        WebDriver driver;
    //    定义一个String类型的字符串,用于存放我们需要打开的url
        String baseUrl;
    
    //    将在执行test方法前需要进行的操作放在@BeforeEach注释的方法中
        @BeforeEach
        void setUp() throws Exception {
    //        将这个变量名对象的引用指向ChromeDriver,表示我们需要用谷歌浏览器来进行自动化操作
            driver = new ChromeDriver();
    //        指定我们需要打开的网站
            baseUrl = "https://piao.qunar.com/";
    //        隐式等待
    //        .implicitlyWait(时长, 时间单位);下面设置时间为“10秒”
    //        TimeUnit.SECONDS表示秒
            driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    //        浏览器窗口最大化
            driver.manage().window().maximize();
                }
    
        @Test
        void test() {
    //        打开网址
            driver.get(baseUrl);
    //        定位登录链接
            driver.findElement(By.id("__headerInfo_login__")).click();
            System.out.println("点击登录链接");
            
    //        点击“密码登录”
            driver.findElement(By.linkText("密码登录")).click();
            
    //        判断用户名输入框是否为空
            TextField tf = new TextField();
            if(tf.getText() != null && !"".equals(tf.getText().trim())) {
    //        如果不为空就清除文本框中的内容
            driver.findElement(By.name("username")).clear();}
    //        否则就输入“账户”
            else {
                driver.findElement(By.name("username")).sendKeys("1312222222");
            }
    
    //        判断密码输入框是否为空
            if(tf.getText() != null && !"".equals(tf.getText().trim())) {
    //        如果不为空就清除文本框中的内容
            driver.findElement(By.cssSelector(".psw")).clear();
            }
    //        否则就输入“密码”
            else {
    //因为去哪儿网的密码框有安全限制,因此不能直接使用.sendkeys方法输入密码
    //            new一个获得Form表单数据,并处理逻辑的类
                Actions action = new Actions(driver);
    //            声明一个WebElement类型的变量,用于存储密码框元素
                WebElement element = driver.findElement(By.cssSelector(".psw"));
    //            输入“密码”
                action.sendKeys(element,"111").perform();        
            }
        }
        
        @AfterEach
        void tearDown() throws Exception {
    //        强制等待5秒,一秒等于1000毫秒
            Thread.sleep(5000);
    //        在所有的test执行完成后退出浏览器
            driver.quit();
        }
    }

    二、因为去哪儿网的密码框有安全限制,因此不能直接使用.sendkeys方法输入密码

                Actions action = new Actions(driver);
    //            声明一个WebElement类型的变量,用于存储密码框元素
                WebElement element = driver.findElement(By.cssSelector(".psw"));
    //            输入“密码”
                action.sendKeys(element,"111").perform();   

    有不懂的同学加群“555191854”@我,群里有一群软测小伙伴。

  • 相关阅读:
    openlayers6聚合图(附源码下载)
    arcgis api 4.x for js地图加载第三方矢量切片
    leaflet读取tif像素值的两种实现方式(附源码下载)
    openlayers6热力图(附源码下载)
    cesium 3dtiles模型单体化点击高亮效果
    leaflet聚合图功能(附源码下载)
    openlayers6绘制扇形(附源码下载)
    【 Windows 10】神州网信政府版官方镜像
    Windows10 解决“装了 .NET Framework 4.5.2/4.6.1/4.7.1等等任何版本 或版本更高的更新”问题
    App.config/Web.config 中特殊字符的处理
  • 原文地址:https://www.cnblogs.com/luohuasheng/p/10717937.html
Copyright © 2011-2022 走看看