简体   繁体   English

硒如何继续填充下一页的数据?

[英]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 页。

  • email电子邮件
  • password密码

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.

相关问题 继续属性到下一个请求的页面 - Continue with the attribute to the next requested page 单击“继续”按钮但未导航到同一页面上的下一步 selenium java - Continue button is clicked but does not navigate to the NEXT step on the same page in selenium java 如何用下一页“10 项”填充标题和网格布局 - How to fill title and grid layouts with the next page “10 items” Selenium Java-如果元素存在,请单击;如果找不到元素,则继续执行下一个命令 - Selenium Java - Click if element exists, continue with the next command if element not found 当硒中的下一个循环不存在数据时,如何打破“ while”条件 - How to break the 'while' condition when data is not present for the next loop in selenium 如何忽略异常并继续下一个值 - how to ignore the exception and continue to next value 在硒中,即使脚本失败也如何继续运行 - In selenium how to continue running a script even if it is failed 在Selenium(Java)中出现“ NoSuchElementException”之后如何继续执行? - How to continue execution after “NoSuchElementException” in Selenium (Java)? 继续表格到下一个PDF页面,并在两者之间打印文本 - Continue table to the next PDF page, and print text in between 在填写文本字段之前,跳到硒中的下一个文本字段 - before fill out the text field, jump to next text field in selenium
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM