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

我目前正在尝试登录我的测试 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。



GCT1015
浏览 56回答 1
1回答

温温酱

对于您在 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 inputWebElement email = new WebDriverWait(wd, 10).until(ExpectedConditions.elementToBeClickable(By.name("identifier")));email.sendKeys("test@gmail.com");wd.findElement(By.id("identifierNext")).click();//wait password inputWebElement password = new WebDriverWait(wd, 10).until(ExpectedConditions.elementToBeClickable(By.name("password")));password.sendKeys("qwerty123");System.out.println("clicked");
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java