Search code examples
c#seleniumelementhidden

c# Selenium Webdriver - Element Hidden - "Element is not currently visible and so may not be interacted with"


I have an issue regarding a textbox that I can't seem to get selenium to click and enter text. It's a password box for a website. The Username part is fine but for whatever reason I cant send text to the password box. I get the error message "Element is not currently visible and so may not be interacted with"

I have tried several different methods for locating the textbox such as XPath name,contains, id etc but nothing will seem to work. Any ideas? I have also tried a wait and an waitforelement. Also I have returned all the element names "tbPassword" to see if there is a conflict and it only returns 1.

Here is my code:

driver.Navigate().GoToUrl("http://www.paddypower.com/bet");

        IWebElement clickUsername = driver.FindElement(By.XPath("//input[@name='tbUsername']"));
        clickUsername.Click();
        clickUsername.SendKeys("MyUsername");


        IWebElement clickPassword = driver.FindElement(By.XPath("//input[@name='tbPassword']"));
        clickPassword.Click();
        clickPassword.SendKeys("Mypassword");

Solution

  • There is a duplicate password input that is actually visible:

    <div class="inputbg">
        THIS IS VISIBLE => <input id="dummypassword" class="input" type="text" onfocus="this.style.display='none';document.getElementById('pw').style.display='inline';document.getElementById('pw').focus()" value="Password" tabindex="2" name="dummypassword">
        THIS IS INVISIBLE => <input id="pw" class="input" type="password" onblur="if(this.value==''){this.style.display='none';document.getElementById('dummypassword').style.display='inline'}" onkeypress="if (event.keyCode==13)fmLogin.submit();" value="" style="display: none;" tabindex="2" name="tbPassword" autocomplete="off">
    </div>
    

    After you click the visible "dummy" input, it becomes invisible and the "tbPassword" input is becoming visible instead. Follow this behavior in your code:

    IWebElement clickPassword = driver.FindElement(By.Id("dummypassword"));
    clickPassword.Click();
    
    IWebElement realPasswordInput = driver.FindElement(By.XPath("//input[@name='tbPassword']"));
    realPasswordInput.SendKeys("Mypassword");