问题
I have a page with login and password (betmarathon[d.o.t]com). I want to login to the site using selenium webdriver. Selenium enters the login correctly, but i have problems with entering password. I get "Element not visible" exception.
My code for selenium looks like this:
driver.findElement(By.id("auth_login")).sendKeys("MY-USERNAME");
driver.findElement(By.id("auth_login_password")).sendKeys("MY-PASSWORD");
Html code of the page looks like this:
<div class="user">
<input id="auth_login" class="empty" type="text" maxlength="40" rel="Login:" name="login" tabindex="1">
</div>
<div class="pass">
<input id="auth_login_password" type="password" regex="^.{6,}$" maxlength="100" rel="Password:" name="login_password" tabindex="2" style="display: none;">
<input class="undefined empty" type="text" value="Password:" tabindex="2" style="display: inline;">
</div>
You can see that there are 2 inputs for password, the first one is not visible and the second is visible. I should enter the password in the first input. After I click on the box manually, the html code changes and the first input for password becomes visible (display:inline) and the second changes to display:none. But how can I do it with selenium webdriver?
Thanks a lot in advance.
回答1:
Click the second password input
and then send keys to the first one:
driver.findElement(By.xpath("//div[@class='pass']/input[last()]")).click();
driver.findElement(By.id("auth_login_password")).sendKeys("MY-PASSWORD");
回答2:
Possible answer could be javascript
as well. You can directly execute javascript
on a hidden element and set the attribute.
WebDriver driver;
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("document.getElementById('auth_login_password').setAttribute('value', val );");
回答3:
driver.ExecuteScript(string.Format("document.getElementById('cred-password-inputtext').value='{0}';",password));
This solved the problem for me
来源:https://stackoverflow.com/questions/28176498/how-to-enter-password-using-selenium-webdriver-if-the-password-style-is-display