2

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?

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
mya205
  • 93
  • 1
  • 11
  • 2
    This part of xpath seems incorrect - `By.xpath("//*[@id="curruserelt"]")` instead wrapping the string into double quotes wrap it in single quotes like this - `By.xpath("//*[@id='curruserelt']")` or do vice versa with external double quotes. – Dev Dec 22 '20 at 17:12
  • That worked thank you! But it keeps saying that 'findElement' is not defined, the forum didn't mention anything about defining findElement so I'm wondering how I would go about doing that. – mya205 Dec 22 '20 at 17:30
  • 1
    Off course you are mixing Java and Python `findElement` belongs to Java, `find_element_by_xpath()` is belongs to Python. – Dev Dec 22 '20 at 17:37
  • I've imported ```from selenium.webdriver.common.by import By``` but still get the ```find_element_by_xpath()``` is not defined in Python – mya205 Dec 22 '20 at 17:47

1 Answers1

2

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)
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Thank you! How would I go about doing this with javascript within Python? – mya205 Dec 22 '20 at 17:49
  • Couldn't get the ```find_element_by_xpath``` method to work in Python, no errors it just doesn't seem to input into the field on the page based on that ID I got from inspect element of the username field. – mya205 Dec 22 '20 at 19:36
  • @mya205 As I said, your desired element to send text would be some other element, at least not this element with `style` attribute set as `"display: none;"`. I can provide you the code but that wouldn't be among the best practices. – undetected Selenium Dec 22 '20 at 20:10
  • If you could that'd be really helpful because I'm quite lost with how to proceed. When I hit F12 and then click the username field that's what the inspect element showed me, what else should I look for because nothing else appears to be highlighted by the inspect element. – mya205 Dec 22 '20 at 20:46
  • 1
    @mya205 Checkout the updated answer and let me know the status. – undetected Selenium Dec 22 '20 at 21:22