简体   繁体   中英

How to send the login credentials using HtmlUnitDriver and Selenium Java

I'm tring to send the login credentials within the username and password field https://www.vignanits.ac.in/server/moodle/login/index.php which needed to be automated using HtmlUnitDriver but facing NoSuchElementException.

Code trials:

package leela;

import org.openqa.selenium.By;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;

import com.gargoylesoftware.htmlunit.WebClient;

public class LeelaHtmlUnit {

    public static void main(String[] args) throws InterruptedException {
        
        HtmlUnitDriver driver = new HtmlUnitDriver(true);
        driver.get("https://www.vignanits.ac.in/server/moodle/login/index.php");
        System.out.println(driver.getCurrentUrl()+driver.getTitle()); 
        driver.findElement(By.id("username")).sendKeys("xyz");
        driver.findElement(By.id("password")).sendKeys("xyz");
        driver.findElement(By.id("loginbtn")).click();
        System.out.println(driver.getCurrentUrl()+driver.getTitle());
    }

}

enter code here

Ideally, to use you need to download htmlunit-driver-2.42.0-jar-with-dependencies.jar the latest release as of today.

The below code block works perfect at my end:

import org.openqa.selenium.By;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class HtmlUnitDriverDemo {

    public static void main(String[] args) {

        HtmlUnitDriver driver = new HtmlUnitDriver();
        driver.get("https://www.vignanits.ac.in/server/moodle/login/index.php");
        System.out.println(driver.getCurrentUrl()+driver.getTitle()); 
        new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.id("username")));
        driver.findElement(By.id("password")).sendKeys("xyz");
        driver.findElement(By.id("loginbtn")).click();
    }
}

Console Output:

https://www.vignanits.ac.in/server/moodle/login/index.phpVIGNAN MOODLE: Log in to the site

Reference

You can find a detailed discussion on NoSuchElementException in:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM