简体   繁体   中英

How can I compare a text on login page that loads after few seconds of clicking the button?

I am learning Selenium Webdriver and am writing a code to fill a registration form for rediff.com. When selecting a rediffmail ID there is a validation button which checks the availability of the ID entered and gives the result if the ID chosen is available to use. I want to compare that text and if the ID is available then it automatically fills the rest of the page but if it is not available then it stops and gives a message to select new ID. I was able to achieve somewhat of the code but I do not think it is the best approach hence asking the experts. Please suggest I have included my code below. Thanks in advance for the help.

public void fillformredifflocators() {  
    try {
        invokebrowser("http://register.rediff.com/register/register.php?FormName=user_details");
        driver.findElement(By.xpath("/html/body/center/form/div/table[2]/tbody/tr[3]/td[3]/input")).sendKeys("Rediff User");
        driver.findElement(By.xpath("/html/body/center/form/div/table[2]/tbody/tr[7]/td[3]/input[1]")).sendKeys("abcd540");
        driver.findElement(By.className("btn_checkavail")).click();
        driver.manage().timeouts().pageLoadTimeout(100, TimeUnit.SECONDS);
        String expectedMessage = "Yippie! The ID you've chosen is available.";
        String Message = driver.findElement(By.xpath("//b[contains(text(),\"Yippie! The ID you've chosen is available.\")]")).getText();
        Assert.assertEquals(Message, expectedMessage);
         if (expectedMessage.equals(Message))
             {
             System.out.println("Congrats ! Your chosen id can be used");
             }
             else
             {
             System.out.println("Please use a different id as the chosen id is taken");
             }

        driver.findElement(By.xpath("/html[1]/body[1]/center[1]/form[1]/div[1]/table[2]/tbody[1]/tr[9]/td[3]/input[1]")).sendKeys("password123");
    } catch (Exception e) {

        e.printStackTrace();
    }
}

Welcome to SO.

You have to improve the locators used in the script. Try to use the relative xpath rather absolute xpath. And you don't have to store the message and then compare for the validation here. Simply check if element with Yippie! The ID you've chosen is available. Yippie! The ID you've chosen is available. text present, that it self can be considered as validation.

if (driver.findElements(By.xpath("//b[contains(text(),\"Yippie! The ID you've chosen is available.\")]")).size()>0){
    System.out.println("Congrats ! Your chosen id can be used");
}else{
    System.out.println("Please use a different id as the chosen id is taken");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM