簡體   English   中英

如何使用 HtmlUnitDriver 和 Selenium Java 發送登錄憑據

[英]How to send the login credentials using HtmlUnitDriver and Selenium Java

我正在嘗試在用戶名和密碼字段https://www.vignanits.ac.in/server/moodle/login/index.php中發送登錄憑據,這需要使用 HtmlUnitDriver 自動化,但面臨 NoSuchElementException。

代碼試驗:

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());
    }

}

在此處輸入代碼

理想情況下,要使用 ,您需要下載htmlunit-driver-2.42.0-jar-with-dependencies.jar最新版本

下面的代碼塊在我的最后工作完美:

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();
    }
}

控制台 Output:

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

參考

您可以在以下位置找到關於NoSuchElementException的詳細討論:

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM