-1

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.

nicolas
  • 11
  • 1
  • 1
  • 1

3 Answers3

0

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");
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
0

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 );");
Saifur
  • 16,081
  • 6
  • 49
  • 73
  • This is where Selenium is really doing a disservice to everyone by doing magic behind the scenes to make `setAttribute('value',...)` and `getAttribute('value',...)` magically work with the `value` *property* rather than the `value` *attribute*. People acquire bad habits from this nonsense. When working with the DOM itself, like you are doing here, you don't use `setAttribute/getAttribute` to work with an `input`'s value: you access it directly as a field of the DOM node (e.g. `el.value = 'blah'`, not `el.setAttribute('value', 'blah');`. – Louis Jan 27 '15 at 17:44
0
driver.ExecuteScript(string.Format("document.getElementById('cred-password-inputtext').value='{0}';",password));

This solved the problem for me

wogsland
  • 9,106
  • 19
  • 57
  • 93