0

I am using Selenium with Java, when I am trying to hit "forgot password" on the page I am testing, Selenium is inserting the inputs on the previous page and then going to the next page:

                driver.findElement(By.xpath("//input[@type='email']")).sendKeys("email");
        driver.findElement(By.xpath("//input[@type='password']")).sendKeys("password");
        driver.findElement(By.className("btn-primary")).click();// -> here it should take us to another page and insert below data, but it is inserting the data on the page and then executing this line and moving to the next page 
        
        System.out.println(driver.findElement(By.cssSelector("span.text-with-icon")).getText());

        driver.findElement(By.linkText("Forgot Password")).click();
        driver.findElement(By.xpath("//input[@type='email']")).sendKeys("email");

When I will use driver.get("https...") it is working fine and taking me to the next page, inserting the data in the proper orde, but I want to hit the URL with "forgot password" and insert data without giving the exact address.

Justyna
  • 1
  • 2

1 Answers1

0

What do you mean when you talk 'page'? If you talk about new tab you should do action on new tab after driver.findElement(By.className("btn-primary")).click();. Something like this:

    for (String winHandle : driver.getWindowHandles()) {
        driver.switchTo().window(winHandle);
    }

and then on new tab you should do next steps. If you mean some pop-up or alert window, you should work with driver.switchTo().alert() or something like that.

  • So what I mean is I am not going to the new tab, only when we are clicking on the "forgot password" button, we are moving to the next page - there is no extra window, it is not a popup only new page. I have tried with getWindowHandles but it is still not working.. – Justyna Nov 15 '22 at 17:26
  • When I remove the 2 last lines of code, it is working perfectly fine and taking me to the next page. When I am adding this 2 steps, it is performing them on the previous page and at the end taking to the next page - which is surprising as I though driver is reading line by line... – Justyna Nov 15 '22 at 18:31
  • I have used -> Thread.sleep(1000); after the click and now it is working fine now, however not sure if this is good way of fixing it. – Justyna Nov 15 '22 at 18:44
  • It's not good fix. I understand now. I think you should use wait. Selenium have many waits to use. Maybe in this case, solution will be something like `wait.until(ExpectedConditions.elementToBeClickable(andHereWebElement))` or `wait.until(element -> webElement.isDisplayed())` It's from FluentWait. Implementations looks like `this.wait = new FluentWait<>(driver) .withTimeout(Duration.ofSeconds(secTimeout)) .pollingEvery(Duration.ofMillis(500)) .withMessage("Oczekiwanie na element.") .ignoring(WebDriverException.class);` – Patryk Patryk Nov 17 '22 at 07:36