How to enter password using Selenium webdriver if the password style is display:none

别来无恙 提交于 2019-12-14 03:27:24

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!