Selenium: Input into a text field with style="display: none;"

Viewed 651

I've been trying to get selenium to input my username and password into a site and have seen various methods mentioned on other forums none of which have worked for me. Here is what is returned upon inspecting the "username" field on the site:

<input type="text" id="curruserelt" value="" aria-hidden="true" style="display: none;" class="">

I tried using the ID method but to no avail, I am trying this xpath method:

username = "ajusingt121"
element_enter = findElement(By.xpath("//*[@id="curruserelt"]")).sendKeys(username);
element_enter.findElement(By.xpath("/html/body/input[1]")).sendKeys(username);

But for whatever reason it keeps returning invalid syntax error at the xpath id part.

Whats the best way to tackle this type of form and input data?

1 Answers

Selenium won't be able to send a character sequence in an <input> field with style attribute set as "display: none;".

However, you tagged but your code block is in

In Python you use:

driver.find_element_by_xpath("//*[@id="curruserelt"]").send_keys(username)

In Java you use:

driver.findElement(By.xpath("//*[@id="curruserelt"]")).sendKeys(username);

Note: However there are JavaScript hacks which can help you to achieve the same.


Update

To send a character sequence to the <input> field with you have to remove the style attribute which is set as "display: none;" and you can use either of the following Locator Strategies:

  • Using css_selector:

    username = "ajusingt121"
    element = driver.find_element_by_css_selector("input#curruserelt[type='text']")
    driver.execute_script("arguments[0].removeAttribute('style')", element)
    driver.find_element_by_css_selector("input#curruserelt[type='text']").send_keys(username)
    
  • Using xpath:

    username = "ajusingt121"
    element = driver.find_element_by_xpath("//input[@id='curruserelt' and @type='text']")
    driver.execute_script("arguments[0].removeAttribute('style')", element)
    driver.find_element_by_xpath("//input[@id='curruserelt' and @type='text']").send_keys(username)
    
Related