在填写文本字段之前,跳转到selenium中的下一个文本字段

我正在尝试将用户名和密码放入网页中的相应文本字段 . 首先我通过id找到电子邮件的元素并为其添加电子邮件,然后通过id找到密码的元素并为其输入密码,但在填写文本框中的电子邮件字段之前,它会跳转到密码字段并完成任务,我需要等待填写电子邮件,然后跳转到密码字段 .

我尝试了隐式和显式等待,但两者都不起作用请帮助我
我使用firefox与selenium 2.44

// 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();

        }

}

回答(3)

3 years ago

做一件事,首先进入电子邮件字段,然后在电子邮件字段上获取文本,它将为您提供您输入的内容,如果是,则转到密码文本框 .

3 years ago

Although your code works fine when I tried it at my end, 我仍然修改了现有的代码 . 请查看这是否适合您:

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();

3 years ago

我有一个类似的情况 . 我的应用程序有一个回发脚本,导致文本框跳转到另一个位置 . 我的工作是先.click()首先回到webelement然后再做一个.sendkey() . 希望这对你也有帮助 .