问题
The new JS enabled page of http://mail.google.com is making trouble to get ID of password input field. However, I've navigated from email ID page with its ID - identifierID .
Help me to get ID of password field.
Thanks in advance...
回答1:
Please find the updated code:
Code:
driver.manage().window().maximize();
driver.get("http://www.gmail.com");
WebElement elementid = driver.findElement(By.id("identifierId"));
elementid.sendKeys("");
WebElement elementnxt = driver.findElement(By.id("identifierNext"));
elementnxt.click();
WebDriverWait wait = new WebDriverWait(driver, 100);
wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//input[@type='password']")));
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//input[@type='password']")));
WebElement elementpwd = driver.findElement(By.xpath("//input[@type='password']"));
elementpwd.sendKeys("123");
I have validated it on my end. Let me know if this doesn't work.
回答2:
Instead if Id use name attribute to get access of the field.
document.getElementsByName("password")[0];
It will give you password input element.
<input type="password" class="whsOnd zHQkBf" jsname="YPqjbf" autocomplete="current-password" spellcheck="false" tabindex="0" aria-label="Enter your password" name="password" autocapitalize="off" autocorrect="off" dir="ltr" data-initial-dir="ltr" data-initial-value="">
回答3:
Use a combination input element and the div having Inner text
<div jsname="YRMmle" class="AxOyFc snByac" aria-hidden="true">Enter your password</div> .
Also on clicking the correct element, the parent element changes which makes you to find the element again and send the value.
You can use CssSelector or Xpath to attain this easily. But you Google will definitely be changing this so often.
回答4:
Try to set the value of the field using JavaScript
as
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("var elements = document.getElementsByName('password');elements[0].setAttribute('value', '123');");
来源:https://stackoverflow.com/questions/44738306/how-to-get-password-input-field-id-of-gmail-wiht-selenium-webdriver