What is the best way to check the contents of the text on the login page that appears a few seconds after the button is clicked

Currently, I am in the process of learning Selenium Webdriver and working on a code to populate a registration form for rediff.com. When opting for a rediffmail ID, there is a validation button that checks if the entered ID is available or not. If the chosen ID is indeed available, I would like the code to automatically complete the remaining fields on the page. However, if the ID is unavailable, the process should halt and prompt the user to select a new ID. I have managed to develop some code to achieve this functionality, but I believe there might be a better approach. Therefore, seeking advice from experts on the matter. Below is the code snippet. Appreciate any assistance provided.

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();
    }
}

Answer №1

Hello and welcome to Stack Overflow.

To enhance the efficiency of your script, it is recommended to utilize relative xpath instead of absolute xpath for locating elements. Additionally, there is no need to store the message and then compare it for validation purposes. You can simply check if an element containing the text

Yippie! The ID you've chosen is available.
is present, which in itself serves as validation.

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

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Using Regular Expressions in JavaScript for Filtering with Angular.js

I am looking to create a custom filter in Angular.js. When an object has a name == null, and I add "u" to the filter->, it results in the return of the object where name == null because re.test(null)=true. However, other characters are returning false. ...

What is the best method for incorporating information into an existing object when it is currently empty?

When it comes to data removal, my method involves locating the element using findIndex and marking it as a null value in isInArray. However, if no such data exists, how can I add it to an empty element starting from the first one? For instance, if the fi ...

terminate the express middleware and return a custom HTTP status code

Is it possible to use custom middleware to return a 404 or 401 error to the user and prevent other handlers from running? I tried implementing the following code: function SomeMiddleware(req, res, next) { if(user.notRealOrSomething) { throw new Htt ...

Unable to implement new ecmascript decorators within typescript version 2.4.2

Check out this example code: function enumerable(value: boolean) { return function (target: any, propertyKey: string, descriptor: PropertyDescriptor) { descriptor.enumerable = value; }; } class A { @enumerable(false) a: number = 1 b: number ...

Positioned footer without predetermined height

I am having trouble with positioning the footer so that it always stays at the bottom of the page. When there is not enough content on the page, the footer does not reach the bottom as desired. Additionally, the footer does not have a fixed height. Below i ...

The jquery datetimepicker function cannot be located or is not functioning as intended

<link href="css/bootstrap-datetimepicker.min.css" rel="stylesheet" /> <script src="Js/bootstrap-datetimepicker.min.js"></script> <script src="Js/bootstrap-datetimepicker.he.js"></script> ...

Creating a HTML5 canvas animation of an emoticon winking to add a fun touch to your

Currently, I am facing a challenge in animating an emoticon that was initially sketched on canvas. While following a tutorial to implement draw and clear animations using frames, I haven't been able to achieve the desired outcome. With 6 frames of the ...

Struggling with error management while using react-redux during the SignUp process

https://i.sstatic.net/OD2fl.pngWithin my component code, I handle user sign up and error checking. Initially, the error value is null. let error = useSelector((state) => state.authReducer.error); const checkErrorLoading = () => { ...

Leveraging Selenium in Python for extracting the "data-label" attribute

I have a table with multiple data-label elements containing various names. I attempted to retrieve these values using the driver.get_elements_by_css_selector method, but it was unsuccessful. Here is the HTML structure from which I need to extract data: < ...

The NGRX state in Angular is not being properly saved by the local storage

Currently, I am utilizing NGRX for state management within my Angular application. While NGRX is functioning correctly, I have encountered an issue with using local storage to persist the NGRX state. Upon refreshing the browser, the NGRX data reverts back ...

Using JavaScript to assign one object to another object

I am facing an issue where I am trying to assign the local variable UgcItems to uploadedItems, but when I attempt to return the value, it shows as undefined. If I place the console.log inside the .getJSON function, then I get the expected value. However, t ...

What is the best way to showcase two SVG clocks on a single webpage?

The issue arises when combining the desktop and mobile versions of the clock script. The first clock works fine on its own, but upon duplicating another set of scripts for the second clock, it encounters display problems. I have appropriately labeled the c ...

Constantly showing false values in AngularJS Firebase array objects

Encountering an issue with retrieving data from Firebase. When viewing console.log($scope.statusbaca), it shows 2 queries, true and false. However, in the app it only displays false. Apologies for any language barriers as English is not my first language. ...

Encountering a Problem with ContentEditable in AngularJS

Here is the code snippet I am working with: <!doctype html> <html ng-app="flyerGen"> <head> <script src="http://code.angularjs.org/1.0.5/angular.min.js"></script> <script> angular.module('flyerGen', []).direct ...

Issue with undetected_chromedriver.v2 Proxy Auth Plugin ZIP causing malfunction

Recently, I made the switch from using proxy_auth_plugin.zip proxy with Selenium Chrome Webdriver to undetected_chromedriver.v2. The latter offers better security features compared to the traditional Selenium Chrome Webdriver. ~ I've encountered iss ...

Tips on altering a predetermined input text value using JavaScript

I have a question about using JavaScript. I am currently developing a tax calculation system. function calculateTax(){ var invoiceValue = document.getElementById("invoicevalue"); var ppn = document.getElementById("ppn"); var pph = document.get ...

Issues with functionality arise when cloning HTML divs using JQuery

VIDEO I created a feature where clicking a button allows users to duplicate a note div. However, the copied note does not function like the original - it's not draggable and changing the color of the copied note affects the original note's color. ...

Select a different radio button to overwrite the currently checked one

I am facing a situation where I have two radio buttons. The default checked state is one of them, but I want to change that and make the other one checked by default instead. Here is the HTML code: <div class="gui-radio"> <input type="radio" v ...

What causes the output of Math.pow(a, b) to be NaN when the value of a is negative and b is not a whole number?

After observing different JavaScript behaviors, I noticed the following: > Math.pow(4, 2) 16 > Math.pow(4, 2.1) 18.37917367995256 > Math.pow(4, 0.5) 2 > Math.pow(-4, 2) 16 > Math.pow(-4, 2.1) NaN > Math.pow(-4, 0.5) NaN Why does using a ...

Is there a way to show new chat messages instantly without needing to refresh the entire page?

Creating a real-time chat application presents the challenge of displaying new messages instantly without requiring a page reload. Exploring different methods like reloading only the chat message container or treating new messages as individual chatMessage ...