小编典典

如何继续按selenium填充下一页中的数据?

selenium

我要登录Selenium。整个过程分为2页。

  • 电子邮件
  • 密码

现在我可以在第一页中输入密钥,然后我应该进入下一页(输入密码并单击提交密钥)。

但是,如果我仅在一个类中添加4个按键代码,则无法完成第二页的按键输入(密码和Submit)

我猜第一页按键输入和第二页按键输入之间缺少一些代码。

public class Selenium {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {



WebDriver driver;
System.setProperty("webdriver.gecko.driver", "C:\\Users\\Downloads\\geckodriver-v0.10.0-win64\\wires.exe");
driver =new FirefoxDriver();


driver.get("https://mail.google.com");
driver.findElement(By.id("Email")).sendKeys("yourEmailId");//first page
driver.findElement(By.id("next")).click();//first page

driver.findElement(By.id("Passwd")).sendKeys("yourPassword");//next page
driver.findElement(By.id("signIn")).click();//next page
}

driver.get("https://mail.google.com");
driver.findElement(By.id("Email")).sendKeys("yourEmailId");//first page
driver.findElement(By.id("next")).click();//first page


/* What code should I add here?  */


driver.findElement(By.id("Passwd")).sendKeys("yourPassword");//next page
driver.findElement(By.id("signIn")).click();//next page
}

阅读 390

收藏
2020-06-26

共1个答案

小编典典

尝试将隐式等待时间设置为大约10秒,然后再将该元素查找为:-

driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.findElement(By.id("Passwd")).sendKeys("yourPassword");
driver.findElement(By.id("signIn")).click();

或设置一个明确的等待。显式等待是您定义的代码,用于等待特定条件发生后再继续执行代码。您的情况就是密码输入字段的可见性。

WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("Passwd")));
element.sendKeys("yourPassword");

//Now click on sign in button 
driver.findElement(By.id("signIn")).click();//next page

Explanation: The reason selenium can’t find the element is because the
id of the password input field is initially Passwd-hidden. After you click
on the “Next” button, Google first verifies the email address entered and then
shows the password input field (by changing the id from Passwd-hidden to
Passwd). So, when the password field is still hidden (i.e. Google is still
verifying the email id), your webdriver starts searching for the password
input field with id Passwd which is still hidden. And hence, you should wait
until it becomes visible.

2020-06-26