[英]How to continue to fill the data in next page by selenium?
I want to login by Selenium
.我想通过
Selenium
登录。 It is divided the process into 2 pages.整个过程分为 2 页。
Now I can input the key in first page .Then I should go next page (input password and click submit key).现在我可以在第一页输入密钥。然后我应该进入下一页(输入密码并单击提交密钥)。
However , If I just add 4 keys codes in one class ,it cannot complete the second page key input (password and submit )但是,如果我只在一个类中添加4个键码,则无法完成第二页键输入(密码和提交)
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
}
Try setting an implicit wait of maybe 10 seconds before finding this element as :-在找到此元素之前,尝试设置可能 10 秒的隐式等待:-
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
Explanation: The reason selenium can't find the element is because the id
of the password input field is initially Passwd-hidden
.解释: selenium 找不到元素的原因是因为密码输入字段的
id
最初是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
).单击“下一步”按钮后,Google 首先验证输入的电子邮件地址,然后显示密码输入字段(通过将 id 从
Passwd-hidden
更改为Passwd
)。 So, when the password field is still hidden (ie Google is still verifying the email id), your webdriver starts searching for the password input field with id Passwd
which is still hidden.因此,当密码字段仍处于隐藏状态时(即 Google 仍在验证电子邮件 ID),您的网络驱动程序将开始搜索仍处于隐藏状态的 id
Passwd
的密码输入字段。 And hence, you should wait until it becomes visible.因此,您应该等到它变得可见。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.