【问题标题】:before fill out the text field, jump to next text field in selenium在填写文本字段之前,跳转到 selenium 中的下一个文本字段
【发布时间】: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


【解决方案1】:

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

【讨论】:

  • 未完全输入邮箱,在完成邮箱前进入密码栏。所以当我在电子邮件字段上收到文本时,它总是错误的
  • 正如你所说的电子邮件ID被输入到密码文本框中,这意味着,密码文本框元素ID与电子邮件ID文本框相同,或者它可能在运行时发生变化,有时它会发生元素ID变化动态地,当您执行时,检查 webdriver 窗口上的元素 id 并验证它们是否正确
【解决方案2】:

虽然我在最后尝试时您的代码运行良好,但我仍然修改了现有代码。请看看这是否适合你:

     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】:

    我也有类似的情况。我的应用程序有一个回发脚本导致文本框跳转到另一个位置。我的解决方法是首先 .click() webelement 重新进入它,然后执行 .sendkey()。希望这对您也有帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-12-01
      • 2021-10-09
      • 2022-07-31
      • 1970-01-01
      • 1970-01-01
      • 2021-09-23
      • 1970-01-01
      相关资源
      最近更新 更多