Nikunj Aggarwal
Nikunj Aggarwal

Reputation: 825

In Selenium how to check that on clicking an element we have actually moved to another page?

The scenarios is when you click on submit button on page A, it redirects you to Page B most of the time but there are some conditions when it will take back you to Page A without going to Page B.

Also, there can be frontend validation errors which sometimes prevent the submit button to take control to the next page.

So I exactly want to verify that when I clicked on submit button it has taken me to Page B or if page A is displayed again it is not because of validation errors but it is the fresh page came after redirection.

Upvotes: 1

Views: 905

Answers (3)

Jatin gangwani
Jatin gangwani

Reputation: 11

1.Check for the change in the url of the page.
2.Compare the content of the page before and after clicking the submit button.

Upvotes: 0

Pradeep hebbar
Pradeep hebbar

Reputation: 2267

There are two ways to make sure that we have successfully navigated to page B after clicking submit button in page A:

  1. By checking the existence of any locator(Usually title of the page or buttons) of page B after clicking submit button on page A.(recommended) *Selection of locator should works in all circumstances
  2. Check for change in URL.(Less preferable )

Upvotes: 1

NarendraR
NarendraR

Reputation: 7708

Make sure your URL get changes after click on submit button If Yes the you can use below code

String currentPage=driver.getCurrentUrl();

driver.findElement(By.yourLocatorForSubmitButton).click();

String newPage = driver.getCurrentUrl();
if(currentPage.equals(newPage))
{
    System.out.println("On Same Page");
    // Here you perform your further action if there is some validation error then get those validation and fullfill the condition
}
else
{
    //carry on your further action comes on new page
}

Upvotes: 1

Related Questions