亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用 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("[email protected]");            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("[email protected]");

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 關注
  • 104 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號