无法找到定位硒3.4

时间:2017-05-14 07:33:37

标签: html selenium element

这很奇怪,我试图自动化我的测试,但我不能,我使用selenium 3.4,使用firefox 53。 我没有看到我的测试代码有任何问题 运行此测试后,无法找到元素:#email

public class TC001_LoginWithInvalidCredentials {

WebDriver   driver ;
@BeforeTest
public void setUp(){
    System.setProperty("webdriver.gecko.driver",System.getProperty("user.dir")+"/Drivers/geckodriver.exe");
    driver = new FirefoxDriver();
    driver.get("http://automationpractice.com/index.php?");
}
@Test
public void LoginWithInvalidCredentials(){
    driver.findElement(By.xpath(".//*[@id='header']/div[2]/div/div/nav/div[1]/a")).click();
    driver.findElement(By.id("email")).sendKeys("test@gmail.com");
    driver.findElement(By.id("passwd")).sendKeys("passwrd");
    driver.findElement(By.xpath(".//*[@id='SubmitLogin']")).click();

    Assert.assertEquals(driver.findElement
            (By.xpath(".//*[@id='center_column']/div[1]/ol/li")).getText(), "Authentication failed.");

}

@AfterTest
public void endTest(){
    driver.close();

}

}

<div class="form_content clearfix">
<div class="form-group form-error">
<label for="email">Email address</label>
<input id="email" class="is_required validate account_input form-control" data-validate="isEmail" name="email" value="" type="text">
</div>
<div class="form-group">
<label for="passwd">Password</label>
<span>
<input id="passwd" class="is_required validate account_input form-control" data-validate="isPasswd" name="passwd" value="" type="password">
</span>
</div>
<p class="lost_password form-group">
<a href="http://automationpractice.com/index.php?controller=password" title="Recover your forgotten password" rel="nofollow">Forgot your password?</a>
</p>
<p class="submit">
<input class="hidden" name="back" value="my-account" type="hidden">
<button id="SubmitLogin" class="button btn btn-default button-medium" type="submit" name="SubmitLogin">

1 个答案:

答案 0 :(得分:0)

尝试等到DOM中的元素出现并可点击:

WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.elementToBeClickable(By.linkText("Sign in"))).click();
wait.until(ExpectedConditions.elementToBeClickable(By.id("email"))).sendKeys("test@gmail.com");