Java+selenium学习日志(3):错误处理

    经过上篇文章的学习,我们掌握了selenium最基本的网页操作,但是在使用的过程中,我们可能经常遇到这样的问题:
这里写图片描述
    对于这样的问题,在selenium中,我们可以利用getText这个方法来解决。这个方法的用法也很简单:
String s=driver.findElement(By.id(“J_reg_tip_username”)).getText();利用这样一串代码,我们就可以获取到提示框的提示信息,接下来只需要判断s与提示框中的文本进行匹配,就可以编写接下来的解决方法了。这里我以phpwind的注册为例子:

System.out.println("请输入用户名:");
String username=inputNotnull(); //输入并判断输入不为空
SendById(driver, "J_reg_username", username);//把用户名填入输入框
/**
检测phpwind提示框的信息
*/      
driver.findElement(By.xpath("/html/body/div/div[2]/div/div[1]/div/form/div")).click();
driver.findElement(By.id("J_reg_username")).click();
driver.findElement(By.xpath("/html/body/div/div[2]/div/div[1]/div/form/div")).click();
/**
利用循环判断输入的信息是否正确
*/
while(true) {
        String s=driver.findElement(By.id("J_reg_tip_username")).getText();//获取提示信息
        if(s.equals("用户名已经存在,请选择其他用户名")||s.equals("用户名不能为空")
                ||s.equals("用户名含有非法字符,请选择其他用户名")
                ||s.equals("用户名长度错误,长度控制在3-15位,不能有空格")) {
            System.out.println(s+",请重新输入:");
            String reusername=inputNotnull();
            SendById(driver, "J_reg_username", reusername);
            driver.findElement(By.xpath("/html/body/div/div[2]/div/div[1]/div/form/div")).click();
            driver.findElement(By.id("J_reg_username")).click();
            driver.findElement(By.xpath("/html/body/div/div[2]/div/div[1]/div/form/div")).click();
        }else
            break;
    }

    通过以上代码,我们就可以完成对于用户名输入框的错误处理,其它的输入框的处理只要更换一下定位的元素以及判断内容即可。这样我们就可以完成用户的注册功能了。
    除了通过文本的形式来提示错误信息外,还有就是用弹窗的方式来提示用户。这里可以利用 driver.getWindowHandles();来来获取到所有弹出浏览器的窗口(专业名词叫句柄),然后使用swithcto.window(newwindow_handle)方法, 就可以定位到新的窗口,接下来的步骤跟上面的操作一样,获得弹出框的文本,然后对其进行判断即可。
    最后,附上完整的代码:

package Demo;

import java.util.Scanner;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;

public class login {
    public static void main(String[] args) {
        String driverPath=System.getProperty("user.dir")+
                "/geckodriver-v0.21.0-win64/geckodriver.exe";//启动浏览器    
        System.setProperty("webdriver.gecko.driver",driverPath);
        WebDriver driver=new FirefoxDriver();
        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(8, TimeUnit.SECONDS);
        driver.get("http://localhost/phpwind/");

        driver.findElement(By.xpath("/html/body/div/div[2]/"
                + "div[3]/div[2]/div[1]/dd/a")).click();        //对网页进行操作
        /**
        对用户名输入框的处理
        */
        System.out.println("请输入用户名:");
        String username=inputNotnull();
        SendById(driver, "J_reg_username", username);
        driver.findElement(By.xpath("/html/body/div/div[2]/div/div[1]/div/form/div")).click();
        driver.findElement(By.id("J_reg_username")).click();
        driver.findElement(By.xpath("/html/body/div/div[2]/div/div[1]/div/form/div")).click();
        while(true) {
            String s=driver.findElement(By.id("J_reg_tip_username")).getText();
            if(s.equals("用户名已经存在,请选择其他用户名")||s.equals("用户名不能为空")
                    ||s.equals("用户名含有非法字符,请选择其他用户名")
                    ||s.equals("用户名长度错误,长度控制在3-15位,不能有空格")) {
                System.out.println(s+",请重新输入:");
                String reusername=inputNotnull();
                SendById(driver, "J_reg_username", reusername);
                driver.findElement(By.xpath("/html/body/div/div[2]/div/div[1]/div/form/div")).click();
                driver.findElement(By.id("J_reg_username")).click();
                driver.findElement(By.xpath("/html/body/div/div[2]/div/div[1]/div/form/div")).click();
            }else
                break;
        }

        /**
        对密码输入框的处理
        */
        System.out.println("请输入密码:");
        String password=inputNotnull();
        SendById(driver, "J_reg_password", password);
        driver.findElement(By.xpath("/html/body/div/div[2]/div/div[1]/div/form/div")).click();
        driver.findElement(By.id("J_reg_password")).click();
        driver.findElement(By.xpath("/html/body/div/div[2]/div/div[1]/div/form/div")).click();
        while(true) {
            String s=driver.findElement(By.id("J_reg_tip_password")).getText();
            if(s.equals("密码不能为空")||s.equals("密码长度错误,最小长度6个字,最大长度15个字")) {
                System.out.println(s+",请重新输入:");
                password=inputNotnull();
                SendById(driver, "J_reg_password", password);
                driver.findElement(By.xpath("/html/body/div/div[2]/div/div[1]/div/form/div")).click();
                driver.findElement(By.id("J_reg_password")).click();
                driver.findElement(By.xpath("/html/body/div/div[2]/div/div[1]/div/form/div")).click();
            }else
                break;
        }

        /**
        对确认密码输入框的处理
        */
        System.out.println("确认密码:");
        String repassword=inputNotnull();
        SendById(driver, "J_reg_repassword", repassword);
        while(true) {
            if(!password.equals(repassword)) {
                System.out.println("两次输入的密码不一致。请重新输入:");
                repassword=inputNotnull();
                SendById(driver, "J_reg_repassword", repassword);
            }else
                break;
        }

        /**
        对邮箱输入框的处理
        */
        System.out.println("请输入邮箱:");
        String email=inputNotnull();
        SendById(driver, "J_reg_email", email);
        driver.findElement(By.xpath("/html/body/div/div[2]/div/div[1]/div/form/div")).click();
        driver.findElement(By.id("J_reg_email")).click();
        driver.findElement(By.xpath("/html/body/div/div[2]/div/div[1]/div/form/div")).click();
        while(true) {
            String s=driver.findElement(By.id("J_reg_tip_email")).getText();
            if(s.equals("邮箱不能为空")||s.equals("该邮箱地址已经被注册")) {
                System.out.println(s+",请重新输入:");
                String reusername=inputNotnull();
                SendById(driver, "J_reg_email", reusername);
                driver.findElement(By.xpath("/html/body/div/div[2]/div/div[1]/div/form/div")).click();
                driver.findElement(By.id("J_reg_email")).click();
                driver.findElement(By.xpath("/html/body/div/div[2]/div/div[1]/div/form/div")).click();
            }else
                break;
        }

        driver.findElement(By.xpath("/html/body/div/div[2]/div/div[1]/"
                + "div/form/div/dl[5]/dd/button")).click();

        driver.findElement(By.id("J_head_user_a")).click();
        driver.findElement(By.linkText("退出")).click();
        System.out.println("完成注册");
        driver.close(); 
    }

    /**通过xpath添加内容
    */
        public static void SendByXpath(WebDriver driver,String xpath,String message) {
            driver.findElement(By.xpath(xpath)).clear();
            driver.findElement(By.xpath(xpath)).sendKeys(message);
        }
        /**通过id添加内容
        */
        public static void SendById(WebDriver driver,String id,String message) {
            driver.findElement(By.id(id)).clear();
            driver.findElement(By.id(id)).sendKeys(message);
        }
        /**通过id选择内容
        */
        public static void SelectById(WebDriver driver,String id,String Text) {
            Select id_Select=new Select(driver.findElement(By.id(id)));
            id_Select.selectByVisibleText(Text);
        }
    /**
    向文本框输入内容,并判断输入不能为空
    */
    public static String inputNotnull() {
        String s;
        String n;
        Scanner in=new Scanner(System.in);
        n=in.nextLine();
        if(n.isEmpty()==true) {
            System.out.println("内容不能为空,请重新输入:");
            Scanner in1=new Scanner(System.in);
            s=in1.nextLine();
            return s;
        }
        return n;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值