1

The mentioned URL in the code takes 5 seconds to display the login screen and in order to enter the details on the login page, I have implemented fluent wait for 10 seconds in my code. Even though the wait is correctly mentioned, for some reason this wait is not obeyed and i am always displayed with org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element:

CODE:

public class FluentWaitDemo {

    public static void main(String[] args) throws InterruptedException 
    {

        WebDriverManager.chromedriver().setup();
        WebDriver driver = new ChromeDriver();
        driver.manage().window().maximize();
        driver.get("https://app.hubspot.com/login");
        By email = By.xpath("//input[@type='email']");
        WebElement userId = FluentWaitForElement(driver, email);
        userId.sendKeys("*******@gmail.com");
        driver.close();
    }

    public static WebElement FluentWaitForElement(WebDriver driver, By locator)
    {
        Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
                              .withTimeout(Duration.ofSeconds(10))
                              .pollingEvery(Duration.ofSeconds(2))
                              .ignoring(NoSuchElementException.class);

        return wait.until(ExpectedConditions.presenceOfElementLocated(locator));
    }
}

Error:

Exception in thread "main" org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//input[@type='email']"}
  (Session info: chrome=83.0.4103.97)
For documentation on this error, please visit: https://www.seleniumhq.org/exceptions/no_such_element.html
DebanjanB
  • 118,661
  • 30
  • 168
  • 217
Bimlesh
  • 209
  • 1
  • 3
  • 17
  • if it takes 10 seconds to get that, the wait is working correctly. I would just use a webdriverwait though... it's a fluent-wait, too but is already setup with what you need... ignoring, polling, etc... – pcalkins Jun 10 '20 at 19:01
  • Any idea why the above code is still throwing no such element exception when everything is correctly mentioned. – Bimlesh Jun 10 '20 at 19:32
  • it'll ignore for 10 seconds, does it happen before the 10 seconds is up or after? – pcalkins Jun 10 '20 at 20:19
  • No such element exception is thrown after 10 seconds. Ideally my code should suppress this message but still seeing no such element exception and because of this... I am unable to enter the email address on the login page. This scenario is perfectly handled by Webdriverwait but i want to understand what is wrong with my code having fluent wait. – Bimlesh Jun 10 '20 at 22:00
  • assuming same XPATH, not sure what's happening... webdriverwait does set polling to 1/2 second though, so it's possible that at 8.5-9.5 seconds the option appears?? I would also try/catch the wait.until call to see if that's throwing timeout. – pcalkins Jun 10 '20 at 22:17

1 Answers1

0

To send a character sequence within the Email address field you have to induce WebDriverWait for the elementToBeClickable() and you can use either of the following Locator Strategies:

  • Using cssSelector:

    driver.get("https://app.hubspot.com/login");
    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("input#username"))).sendKeys("Bimlesh@gmail.com");
    
  • Using xpath:

    driver.get("https://app.hubspot.com/login");
    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@id='username']"))).sendKeys("Bimlesh@gmail.com");
    

Browser Snapshot:

Hubsopt


Reference

You can find a couple of detailed discussions on NoSuchElementException in:

DebanjanB
  • 118,661
  • 30
  • 168
  • 217