元素无法使用Java的Gmail密码字段Selenium WebDriver进行交互

时间:2019-10-02 21:02:27

标签: java selenium-webdriver gmail

我当前正在尝试登录我的测试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将数据发送到此字段?我想尝试使用setAttribute JavaScriptexecutor setAttribute value on selenium,但以前从未使用过JS。

1 个答案:

答案 0 :(得分: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 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");