1

I Need to verify title of a webpage after login is done. Using the Chrome Driver for selenium the title of page after login is coming correct but not firefox driver. Rest of the code remains same, just the driver was changed from chrome to firefox.

public void verifyLoginPage()
{
    String homepage= "Guru99 Bank Manager HomePage";

    if (driver.getTitle().equals(homepage))
    {
        System.out.println("Its the correct Homepage after Login");
    }
    else
    {
        System.out.println("Page after login is not the intended one");
    }
}

With Chrome driver this code returns " Its the correct Homepage after Login" and with firefox driver this code returns " Page after login is not the intended one" , as getTitle returns the title of login page, not after login.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
vishal suri
  • 79
  • 1
  • 7

2 Answers2

1

Can you please try to execute below code and let's know if it worked.

Once you have opened chrome or Firefox driver, after that please add below code and see -

driver.manage().timeouts().implicitlyWait(10
driver.navigate().to("http://sitename.com");
String actualTitle = driver.getTitle();
driver.manage().window().maximize();
String expectedTitle = "page title to be verified";
if(actualTitle.equalsIgnoreCase(expectedTitle))
System.out.println("Title Matched");
else
System.out.println("Title didn't match");
driver.close();
driver.quit();
TheSociety
  • 1,936
  • 2
  • 8
  • 20
  • The porblem is i cannot specify static url here in code i.e driver.navigate().to("http://sitename.com");", it should come from driver.getcurrenturl() afterlogin is done. which work fine in chrome. I tried adding your suggestion with " driver.navigate().to(driver.getCurrentUrl());" , but again it works in chrome only. :( – vishal suri Mar 31 '19 at 09:28
  • Does not Firefox return same url as u get in chrome using driver.getcurrenturl() – TheSociety Mar 31 '19 at 09:34
  • with driver.getcurrenturl(), the value is different in both chrome and firefox. Although login is happening correctly with firefox as well. But somehow the latest page title is not retrieved. to be more clear, Below is what i am doing: 1. Initialize driver chrome or firefox. 2. Login to base url 3. validate the title of url after login. so with firefox login happens but the gettitle() or getcurrenturl() gives data of previous page. – vishal suri Mar 31 '19 at 09:58
1

Different browser renders the HTML DOM in a different way. You can find a relevant discussion in Chrome & Firefox on Windows vs Linux (selenium). At this point it is worth to be mentioned that:

  • ChromeDriver / Chrome is based on OSS dialect.
  • GeckoDriver / Firefox is based on W3C dialect.

It seems in your usecase:

  • While using ChromeDriver / Chrome, the Page Title is already rendered within the DOM Tree by the time document.readyState equals to complete is attained.

  • But while using GeckoDriver / Firefox, the Page Title is not rendered within the DOM Tree by the time document.readyState equals to complete is attained.

Solution

You need to induce WebDriverWait for the title to contain and you can use the following solution:

public void verifyLoginPage()
{
    new WebDriverWait(driver, 20).until(ExpectedConditions.titleContains("Guru99");
    String homepage= "Guru99 Bank Manager HomePage";
    if (driver.getTitle().equalsIgnoreCase(homepage))
        System.out.println("Its the correct Homepage after Login");
    else
        System.out.println("Page after login is not the intended one");
}
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • I had initially tried implicit wait of 20 seconds, but was not working. With explicit wait from your suggestion (20s), first time it worked and failed second time. After i have increased explicit wait for 40s, its behaving as expected in all runs. Thanks – vishal suri Apr 01 '19 at 20:49