我使用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();
}
}
答案 0 :(得分:0)
您需要启用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();
}
}
答案 1 :(得分:0)
登录表单是通过JS而不是常规HTML添加的。我试图在浏览器中禁用JS,我只能看到:
<div id="container"></div>
这意味着你所要做的就是:
为您的驱动程序启用JavaScript:driver.setJavascriptEnabled(true);
等到呈现“signin_email”:driver.until(ExpectedConditions.presenceOfElementLocated(By.id("signin_email")))
第二个项目符号将为您提供不同PC上的稳定测试。有时,在较弱的机器上,断言的执行速度可能比JS呈现的元素更快,从而导致随机失败。