如何继续按硒填充下一页中的数据? [英] How to continue to fill the data in next page by selenium?

查看:86
本文介绍了如何继续按硒填充下一页中的数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过Selenium登录.整个过程分为2页.

I want to login by Selenium . It is divided the process into 2 pages.

  • 电子邮件
  • 密码

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

Now I can input the key in first page .Then I should go next page (input password and click submit key).

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

However , If I just add 4 keys codes in one class ,it cannot complete the second page key input (password and submit )

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

I guess some code is missing between first page key input and second page key input.

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
}

推荐答案

在设置此元素为:-

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

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

Or set an explicit wait. An explicit waits is code you define to wait for a certain condition to occur before proceeding further in the code. In your case, it is the visibility of the password input field.

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

说明:硒找不到元素的原因是因为密码输入字段的id最初是Passwd-hidden.单击下一步"按钮后,Google首先验证输入的电子邮件地址,然后显示密码输入字段(将ID从Passwd-hidden更改为Passwd).因此,当密码字段仍处于隐藏状态(即Google仍在验证电子邮件ID)时,您的网络驱动程序将开始搜索ID为Passwd且仍处于隐藏状态的密码输入字段.因此,您应该等待它变得可见.

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.

这篇关于如何继续按硒填充下一页中的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆