简体   繁体   English

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

[英]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. 首先,我为电子邮件找到ID的元素,然后为它放置电子邮件,然后为密码找到ID的元素,然后为其输入密码,但是在填写文本框中的电子邮件字段之前,它会跳转到密码字段并完成任务,我需要等待填写电子邮件,然后跳转到密码字段。

I tried implicit and explicit wait, but both are not working please help me 我尝试了隐式和显式的等待,但都无法正常工作,请帮助我
I am using firefox with selenium 2.44 我正在使用硒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();

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(). 我的解决方法是先将web元素.click()重新放入,然后执行.sendkey()。 Hope this is helpful for you as well. 希望这对您也有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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