【发布时间】:2014-12-09 09:38:12
【问题描述】:
我正在尝试将用户名和密码输入网页中的相应文本字段。首先,我通过 id 找到元素作为电子邮件并为其放置电子邮件,然后通过 id 找到元素作为密码并为其输入密码,但在填写文本框中的电子邮件字段之前,它会跳转到密码字段并完成任务,我需要等待填写电子邮件,然后跳转到密码字段。
我尝试了隐式和显式等待,但两者都不起作用请帮助我
我正在使用带有 selenium 2.44 的 Firefox
// Enter Email
driver.findElement(By.id("Email")).sendKeys("xxxxxxxx@gmail.com");
WebElement myDynamicElement = (new WebDriverWait(driver, 10)).until(ExpectedConditions.presenceOfElementLocated(By.id("Email")));
//driver.manage().timeouts().pageLoadTimeout(5, TimeUnit.SECONDS);
//Enter pwd
driver.findElement(By.id("Passwd")).sendKeys("xxxxx");
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
// Click the sign in button
driver.findElement(By.id("signIn")).click();
已编辑:这是我的完整代码
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class Sample {
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("https://www.google.com/");
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.manage().window().maximize();
driver.findElement(By.id("gbqfq")).sendKeys("gmail");
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.findElement(By.id("gbqfb")).click();
driver.findElement(By.xpath("//*[@id=\"rso\"]/div[2]/li[1]/div/h3/a[1]")).click();
// Click the sign in button
driver.findElement(By.id("gmail-sign-in")).click();
driver.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS);
// Enter Email
driver.findElement(By.id("Email")).sendKeys("xxx@gmail.com");
WebElement myDynamicElement = (new WebDriverWait(driver, 10)).until(ExpectedConditions.presenceOfElementLocated(By.id("Email")));
//Enter pwd
driver.findElement(By.id("Passwd")).sendKeys("@@@@xxx");
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
// Click the sign in button
driver.findElement(By.id("signIn")).click();
}
}
【问题讨论】:
-
你说,它完成了任务,没有进入电子邮件字段。从代码来看,它似乎是一个登录页面。那么,它是否会引发一些与 “请填写电子邮件字段” 或 “电子邮件字段不能为空” 或其他内容相关的错误?
-
设置 driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
-
@Subh 它会在电子邮件中填充一些字符。认为我的电子邮件是example@gmail.com,然后填写“考试”,其他部分进入密码字段,而不是等待完全填写电子邮件字段,
-
@Lakna:如果这是一个你想要自动化的公共网站,你能分享一下链接吗?
-
我正在尝试自动登录我的 gmail 帐户。如果您需要,我可以发送我的完整代码
标签: java selenium selenium-webdriver webdriver