我使用 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/