Java - 尝试使用 selenium webdriver (htmlunit) 登录网站时出现错误

标签 java selenium htmlunit htmlunit-driver

我使用 Indeed.com 的实际凭据尝试了以下代码, 我收到一个错误: 线程“main”中出现异常 org.openqa.selenium.NoSuchElementException:无法使用//*[@id="signin_email"] 定位节点

当我使用 By.id 而不是 By.xpath 时,我收到类似的错误,知道发生了什么吗?

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


public class Testing {

public static void main(String[] args) {

                WebDriver driver = new HtmlUnitDriver();

                driver.get("https://secure.indeed.com/account/login?service=my&hl=en_CA&co=CA");
                driver.findElement(By.xpath("//*[@id=\"signin_email\"]")).sendKeys("notworking@gmail.com");
                driver.findElement(By.xpath("//*[@id=\"signin_password\"]")).sendKeys("needHelp");
                driver.findElement(By.xpath("//*[@id=\"loginform\"]/button")).click();  
                driver.quit(); 

 } 
}

最佳答案

您需要启用java脚本,请参阅更新的代码。

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


public class Testing {

public static void main(String[] args) {

                HtmlUnitDriver driver = new HtmlUnitDriver();
                driver.setJavascriptEnabled(true);
                driver.get("https://secure.indeed.com/account/login?service=my&hl=en_CA&co=CA");
                driver.findElement(By.xpath("//*[@id=\"signin_email\"]")).sendKeys("notworking@gmail.com");
                driver.findElement(By.xpath("//*[@id=\"signin_password\"]")).sendKeys("needHelp");
                driver.findElement(By.xpath("//*[@id=\"loginform\"]/button")).click();  
                driver.quit(); 

 } 
}

关于Java - 尝试使用 selenium webdriver (htmlunit) 登录网站时出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45526487/

相关文章:

java - HTMLUnit : different code from when view source in Chrome

java - 如何在java中使用Jaunt或Jsoup或HtmlUnit登录linkedIn?

java - 在 Intellij 中的 java 中使用 HtmlUnit 时出现 IncompleteClassChangeError

java - 创建文件的最简单方法是什么?

java - 构造 DTO :s with Querydsl and ConstructorExpression. create()

java - 发送电子邮件的问题 sendgrid

java - 多个@Test、Selenium WebDriver 之间的 Get() 和 Set()

java - Bouncy CaSTLe API 线程安全吗?

java - 我需要帮助在 selenium Java 中查找 Web 元素

java - Selenium - 保存网站,包括所有图像、css、dom