1

I have a password textbox that is something like this

<input class="blahblah" id="someId"  type="password"></input>

I am able to see this textbox in the browser and am able to manually insert password.

However when I test this ui using selenium, although it finds the element correctly, but when it tries to click the element, it throws an error "org.openqa.selenium.ElementNotVisibleException: Element is not currently visible and so may not be interacted with"

I did a check in code using

Boolean isDisplayed=el.isDisplayed();//false
Boolean isEnabled=el.isEnabled();//true

The isDisplayed came up false and isEnabled came up true. There is a 15 second delay added to give the page enough time to load (the page loads instantly). So adding a delay will not fix the problem.

I verified using firefox developer tools that the id it was finding was of the correct element.

Why does selenium think its invisible even if I am able to see it in the browser? Could it be that one of the parent elements has some style attribute that selenium doesn't like? Or is it a bug in the selenium driver?

I am using selenium driver for java version 2.45.0

developer747
  • 15,419
  • 26
  • 93
  • 147
  • 1
    Need more info here. Either share the URL to the page, or as much HTML as possible makes sense here. Have some thoughts, but need this first. Thanks. – alecxe Mar 25 '15 at 00:14
  • Its not a publicly accesible url. The page is generated using asp.net so has tons of garbage in it. (Old legacy code) – developer747 Mar 25 '15 at 00:22
  • Thanks, could you at least show several parents of the password input? Or, the complete form. – alecxe Mar 25 '15 at 00:22
  • I bet there is a nested input for password field. I had to deal with same garbage. Finally, had to set the password through `JavaScriptExecutor` – Saifur Mar 25 '15 at 00:33
  • Shooting blind with so little information, but I have sometimes seen problems where Selenium thinks an object is [scrolled off of the screen](http://stackoverflow.com/questions/3401343/scroll-element-into-view-with-selenium). – rutter Mar 25 '15 at 00:33

4 Answers4

3

The problem is that the desired input is really invisible due to the display: none being set on it's parent table:

<table title="Type a password."
       class="dxeTextBoxSys dxeTextBox_MyCompany  "
       id="ctl00_ctl00_MasterContent_MainContentPlaceHolder_ViewCredentials_TopicPanel1_credentialGrid_editnew_4_txtPassword_P_PB"
       style="width: 100%; border-collapse: collapse; display: none;"
       border="0" cellpadding="0" cellspacing="0">

Most likely, the table is becoming visible on a particular user action that you need to determine.

But, alternatively, you can make the table visible through javascript:

WebElement table = driver.findElement(By.id("ctl00_ctl00_MasterContent_MainContentPlaceHolder_ViewCredentials_TopicPanel1_credentialGrid_editnew_4_txtPassword_P_PB")); 
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("arguments[0].style.display = 'block';", table);

In case the above doesn't make any difference.

There is an another hidden password input that can be important:

<input value=""
       name="ctl00$ctl00$MasterContent$MainContentPlaceHolder$ViewCredentials$TopicPanel1$credentialGrid$editnew_4$txtPassword$P$PB$CVS"
       type="hidden"> 

You can try making it visible and sending keys to it:

WebElement password = driver.findElement(By.name("ctl00$ctl00$MasterContent$MainContentPlaceHolder$ViewCredentials$TopicPanel1$credentialGrid$editnew_4$txtPassword$P$PB$CVS")); 

JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("arguments[0].setAttribute('type', 'text');", password);

password.sendKeys("MyPassword");

In case the above doesn't work.

You can set the input value through javascript:

JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("document.getElementById('ctl00_ctl00_MasterContent_MainContentPlaceHolder_ViewCredentials_TopicPanel1_credentialGrid_editnew_4_txtPassword_P_PB_I').setAttribute('value', 'MyPassword');");
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • This is what worked. JavascriptExecutor jse = (JavascriptExecutor)driver; jse.executeScript("document.getElementById('ctl00_ctl00_MasterContent_MainContentPlaceHolder_ViewCredentials_TopicPanel1_credentialGrid_editnew_4_txtPassword_P_PB_I').setAttribute('value', 'MyPassword');"); – developer747 Mar 25 '15 at 22:13
1

Possibly, Selenium is going too fast through your DOM. Has happened with me several times and your element hasn't fully loaded into DOM.

I am more familiar with the PHP/PHPUnit libraries available for Selenium, but perhaps you can introduce a temporary wait with a command similar to waitForElementPresent.

Also, if you have control of the code, can you give a 'name' attribute to your input field as well? It could not hurt anything to do so.

Adam T
  • 675
  • 8
  • 22
1

Have a look at the DOM elements and verify that there is no parent element with a display: none etc, when i encountered an issue like this that was the problem.

Are you able to get information from the element by XPath? This was my work around.

Burrito
  • 517
  • 3
  • 8
1

I have faced this kind of issue many times. the first thing comes into my mind is probably the selector you are using is not unique or not returning THE element you are looking for. Since,

Boolean isDisplayed=el.isDisplayed();//false Boolean isEnabled=el.isEnabled();//true

does not return NoSuchElement exception I do not think it's a element load issue. A quick check can tell you what's going on

driver.findElements(By.cssSelector("the css")).size(); and see how many count it returns.

Saifur
  • 16,081
  • 6
  • 49
  • 73
  • When you say "the css" do you mean css of the textbox? Please note that it is nested inside many parent elements and many of those elements have their own css that this textbox inherits. – developer747 Mar 25 '15 at 00:25
  • Oh I am sorry, you can use id instead. It was just an example . Or use `[type='password']` as `cssSelector` – Saifur Mar 25 '15 at 00:26