使用Selenium网络驱动程序登录Gmail失败.显示找不到密码的元素 [英] Gmail login fail using Selenium webdriver. Showing element not found for password
问题描述
public static void main(String[] args){
System.setProperty("webdriver.chrome.driver","E:/softwares/chromedriver_win32/chromedriver.exe");
WebDriver gmail= new ChromeDriver();
gmail.get("https://www.gmail.co.in");
gmail.findElement(By.id("Email")).sendKeys("abcd");
gmail.findElement(By.id("next")).click();
gmail.findElement(By.id("Passwd")).sendKeys("xyz");
推荐答案
尝试设置大约10秒的隐式等待.
Try setting an implicit wait of maybe 10 seconds.
gmail.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
或设置一个明确的等待.显式等待是您定义的代码,用于等待特定条件发生后再继续执行代码.在您的情况下,这是密码输入字段的可见性. (感谢 ainlolcat 的
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. (Thanks to ainlolcat's comment)
WebDriverWait wait = new WebDriverWait(gmail, 10);
WebElement element = wait.until(
ExpectedConditions.visibilityOfElementLocated(By.id("Passwd")));
gmail.findElement(By.id("Passwd")).sendKeys("xyz");
说明:硒找不到元素的原因是因为密码输入字段的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, an exception is thrown.
这篇关于使用Selenium网络驱动程序登录Gmail失败.显示找不到密码的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!