使用 java 的 Gmail 密码字段 Selenium WebDriver 元素不可交互
element not interactable for the Gmail password field Selenium WebDriver using java
我目前正在尝试登录我的测试 Gmail 邮箱。登录没问题,但对于密码字段,我总是得到一个:
ElementNotInteractableException: element not interactable.
我使用了不同的 xpath's
/ id's
(它们非常明确)但没有帮助。
代码很简单:
public class OpenGmail {
public static void main(String[] args){
System.setProperty ("webdriver.chrome.driver", "C:\Chromedriver\chromedriver_win32\chromedriver.exe");
WebDriver wd = new ChromeDriver();
try {
wd.get("https://mail.google.com/mail/u/0/h/1pq68r75kzvdr/?v%3Dlui");
wd.findElement(By.xpath("//input[@type='email']")).sendKeys("test@gmail.com");
wd.findElement(By.id("identifierNext")).click();
//Variant1
wd.findElement(By.xpath("//input[@type='password']")).sendKeys("qwerty123");
//Variant2
wd.findElement(By.id("password")).sendKeys("qwerty123");
System.out.println("clicked");
wd.findElement(By.xpath("//input[@class='whsOnd zHQkBf']")).sendKeys("qwerty123");
}catch (Exception e){
System.out.println(e);
}
}
}
我试着分析了 html 并且 WebElement
中有 aria-hidden="true"
:
<input type="password" class="whsOnd zHQkBf" jsname="YPqjbf" autocomplete="current-password" spellcheck="false" tabindex="0" aria-label="Enter your password" name="password" autocapitalize="off" dir="ltr" data-initial-dir="ltr" data-initial-value="">
<div jsname="YRMmle" class="AxOyFc snByac" aria-hidden="true">Enter your password</div>
我理解 WebElement
被 WebDriver
隐藏了吗?
是否可以通过JS等向该字段发送数据?我想尝试 setAttribute JavaScriptexecutor setAttribute value on selenium 但我以前从未使用过 JS。
对于您在 gmail 登录页面中输入的密码,您可以使用此定位器:By.name("password")
看来您需要等待此元素。首先,导入以下内容:
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
并尝试下面的代码:
wd.get("https://mail.google.com/mail/u/0/h/1pq68r75kzvdr/?v%3Dlui");
//wait email input
WebElement email = new WebDriverWait(wd, 10).until(ExpectedConditions.elementToBeClickable(By.name("identifier")));
email.sendKeys("test@gmail.com");
wd.findElement(By.id("identifierNext")).click();
//wait password input
WebElement password = new WebDriverWait(wd, 10).until(ExpectedConditions.elementToBeClickable(By.name("password")));
password.sendKeys("qwerty123");
System.out.println("clicked");
我目前正在尝试登录我的测试 Gmail 邮箱。登录没问题,但对于密码字段,我总是得到一个:
ElementNotInteractableException: element not interactable.
我使用了不同的 xpath's
/ id's
(它们非常明确)但没有帮助。
代码很简单:
public class OpenGmail {
public static void main(String[] args){
System.setProperty ("webdriver.chrome.driver", "C:\Chromedriver\chromedriver_win32\chromedriver.exe");
WebDriver wd = new ChromeDriver();
try {
wd.get("https://mail.google.com/mail/u/0/h/1pq68r75kzvdr/?v%3Dlui");
wd.findElement(By.xpath("//input[@type='email']")).sendKeys("test@gmail.com");
wd.findElement(By.id("identifierNext")).click();
//Variant1
wd.findElement(By.xpath("//input[@type='password']")).sendKeys("qwerty123");
//Variant2
wd.findElement(By.id("password")).sendKeys("qwerty123");
System.out.println("clicked");
wd.findElement(By.xpath("//input[@class='whsOnd zHQkBf']")).sendKeys("qwerty123");
}catch (Exception e){
System.out.println(e);
}
}
}
我试着分析了 html 并且 WebElement
中有 aria-hidden="true"
:
<input type="password" class="whsOnd zHQkBf" jsname="YPqjbf" autocomplete="current-password" spellcheck="false" tabindex="0" aria-label="Enter your password" name="password" autocapitalize="off" dir="ltr" data-initial-dir="ltr" data-initial-value="">
<div jsname="YRMmle" class="AxOyFc snByac" aria-hidden="true">Enter your password</div>
我理解 WebElement
被 WebDriver
隐藏了吗?
是否可以通过JS等向该字段发送数据?我想尝试 setAttribute JavaScriptexecutor setAttribute value on selenium 但我以前从未使用过 JS。
对于您在 gmail 登录页面中输入的密码,您可以使用此定位器:By.name("password")
看来您需要等待此元素。首先,导入以下内容:
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
并尝试下面的代码:
wd.get("https://mail.google.com/mail/u/0/h/1pq68r75kzvdr/?v%3Dlui");
//wait email input
WebElement email = new WebDriverWait(wd, 10).until(ExpectedConditions.elementToBeClickable(By.name("identifier")));
email.sendKeys("test@gmail.com");
wd.findElement(By.id("identifierNext")).click();
//wait password input
WebElement password = new WebDriverWait(wd, 10).until(ExpectedConditions.elementToBeClickable(By.name("password")));
password.sendKeys("qwerty123");
System.out.println("clicked");