簡體   English   中英

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

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM