seleniumselenium-chromedriverhtmlunit-driver

HTMLUnitDriver returning error in Selenium Automation Testing which ChromeDriver doesn't


Aim: To use headless option for selenium testing of login page.(HTMLUnitDriver preferable)

I am trying to automate a login to a site using HTMLUnitDriver.

When I sendKeys to an element, it throws an error.

HtmlUnitDriver driver = new HtmlUnitDriver();
driver.manage().timeouts().implicitlyWait(2, TimeUnit.SECONDS);
driver.get("https://bigData/login.jsp");
WebDriverWait usernameWait = new WebDriverWait(driver, 3);
usernameWait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//input[@id=\"username\"]")));
driver.findElement(By.xpath("//input[@id=\"username\"]")).sendKeys("admin");

Error:

Exception in thread "main" org.openqa.selenium.ElementNotInteractableException: You may only interact with visible elements

I tried the same with ChromeDriver. It works fine! It didn't throw this exception. But I could not use the headless option in it.

ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.addArguments("--headless");
WebDriver driver = new ChromeDriver(chromeOptions);

returns,

Exception in thread "main" org.openqa.selenium.TimeoutException: Expected condition failed: waiting for presence of element located by: By.xpath: //input[@id="username"] (tried for 3 second(s) with 500 milliseconds interval)

Works fine, only when chromeOptions is not defined while initializing chromeDriver.

ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.addArguments("--headless");
WebDriver driver = new ChromeDriver();

Please guide me what could be these scenarios/suggest an alternative?


Solution

  • As per the documentation, ElementNotInteractableException is the W3C exception which is thrown to indicate that although an element is present on the DOM TREE, it is not in a state that can be interacted with.

    Code you can try out is :

    HtmlUnitDriver driver = new HtmlUnitDriver(BrowserVersion.CHROME ,true);
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    driver.get("https://bigData/login.jsp");
    WebDriverWait usernameWait = new WebDriverWait(driver, 30);
    usernameWait.until(ExpectedConditions.visibiltyOfElementLocated(By.xpath("//input[@id=\"username\"]")));
    usernameWait.until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@id=\"username\"]")));
    driver.findElement(By.xpath("//input[@id=\"username\"]")).sendKeys("admin");  
    

    try out this code and let me know the status.