为了账号安全,请及时绑定邮箱和手机立即绑定

使用 java 的 Gmail 密码字段 Selenium WebDriver 元素不可交互

使用 java 的 Gmail 密码字段 Selenium WebDriver 元素不可交互

GCT1015 2023-09-20 16:16:01
我目前正在尝试登录我的测试 Gmail 邮箱。登录正常,但对于密码字段我总是得到:ElementNotInteractableException:元素不可交互。我使用了不同的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,其中aria-hidden="true"有WebElement:<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 向该字段发送数据?我想在 selenium 上尝试 setAttribute JavaScriptexecutor setAttribute value但我以前从未使用过 JS。
查看完整描述

1 回答

?
温温酱

TA贡献1752条经验 获得超4个赞

对于您在 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");


查看完整回答
反对 回复 2023-09-20
  • 1 回答
  • 0 关注
  • 53 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信