简体   繁体   中英

before fill out the text field, jump to next text field in selenium

I'm trying to put username and password to appropriate text field in a web page. first I find the element by id for email and put the email for it and then find the element by id for password and put the password for it, but before fill out the email field in text box it jump to the password field and complete the task, I needed to wait for fill the email and after that jump to the password field.

I tried implicit and explicit wait, but both are not working please help me
I am using firefox with 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();

Edited: here is my complete code

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

        }

}

做一件事,首先输入电子邮件字段,然后在电子邮件字段中获取文本,这将为您提供您输入的内容,如果确实如此,请转到密码文本框。

Although your code works fine when I tried it at my end, still I've modified the existing code. Please see if that works for you:

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

I had a similiar situation. My application had a postback script that was causing the text box to jump to another location. My work around was to .click() the webelement first to get back into it and then do a .sendkey(). Hope this is helpful for you as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM