[英]before fill out the text field, jump to next text field in selenium
我正在尝试将用户名和密码放入网页中的相应文本字段。 首先,我为电子邮件找到ID的元素,然后为它放置电子邮件,然后为密码找到ID的元素,然后为其输入密码,但是在填写文本框中的电子邮件字段之前,它会跳转到密码字段并完成任务,我需要等待填写电子邮件,然后跳转到密码字段。
我尝试了隐式和显式的等待,但都无法正常工作,请帮助我
我正在使用硒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();
}
}
做一件事,首先输入电子邮件字段,然后在电子邮件字段中获取文本,这将为您提供您输入的内容,如果确实如此,请转到密码文本框。
尽管您的代码在最终尝试时可以正常运行,但我仍然修改了现有代码。 请查看是否适合您:
WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.get("https://www.google.com/");
driver.manage().window().maximize();
driver.findElement(By.id("gbqfq")).sendKeys("gmail");
driver.findElement(By.id("gbqfb")).click();
driver.findElement(By.xpath("//a[.='Gmail - Google']")).click(); //Modified with a relative xpath
/*This part of clicking on Sign In Button wasn't coming up for me,
So, I've commented it. Please Uncomment, if this page is coming up for you*/
// 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("testing@gmail.com");
//Enter pwd
driver.findElement(By.id("Passwd")).sendKeys("tesing&*78");
// Click the sign in button
driver.findElement(By.id("signIn")).click();
我有类似的情况。 我的应用程序有一个回发脚本,该脚本导致文本框跳到另一个位置。 我的解决方法是先将web元素.click()重新放入,然后执行.sendkey()。 希望这对您也有帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.