0

I'm new to Python and Selenium and am struggling with testing a form for my company website.

I'm trying to enter text into a field. Here is the html:

<input class="form-control" data-val="true" data-val-length="Must be between 2 and 35 characters" data-val-length-max="35" data-val-length-min="2" data-val-requiredif="*First Name is required" data-val-requiredif-dependentproperty="IsMobile" data-val-requiredif-desiredvalue="False" id="FirstName-guide-request-ed9e64ebe3b245a28c9caaabbcd47b95" maxlength="35" name="FirstName" type="text" value="">

Here is the code I use to try to enter the text:

driver = webdriver.Firefox(executable_path = r'C:\Users\jajacobs\Downloads\geckodriver.exe')

driver.get("https://www.graphicproducts.com/guides/5s-system/")
driver.execute_script("window.scrollTo(0, 1000);")

driver.find_element_by_name('FirstName').send_keys('test', Keys.ENTER)

driver.close()

I get the following error:

---------------------------------------------------------------------------
ElementNotInteractableException           Traceback (most recent call last)
<ipython-input-58-6b71ac82d9ce> in <module>
     10 
     11 timeout = 20
---> 12 driver.find_element_by_name('FirstName').send_keys('test', Keys.ENTER)
     13 
     14 driver.close()

~\AppData\Local\Continuum\anaconda3\lib\site-packages\selenium\webdriver\remote\webelement.py in send_keys(self, *value)
    477         self._execute(Command.SEND_KEYS_TO_ELEMENT,
    478                       {'text': "".join(keys_to_typing(value)),
--> 479                        'value': keys_to_typing(value)})
    480 
    481     # RenderedWebElement Items

~\AppData\Local\Continuum\anaconda3\lib\site-packages\selenium\webdriver\remote\webelement.py in _execute(self, command, params)
    631             params = {}
    632         params['id'] = self._id
--> 633         return self._parent.execute(command, params)
    634 
    635     def find_element(self, by=By.ID, value=None):

~\AppData\Local\Continuum\anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py in execute(self, driver_command, params)
    319         response = self.command_executor.execute(driver_command, params)
    320         if response:
--> 321             self.error_handler.check_response(response)
    322             response['value'] = self._unwrap_value(
    323                 response.get('value', None))

~\AppData\Local\Continuum\anaconda3\lib\site-packages\selenium\webdriver\remote\errorhandler.py in check_response(self, response)
    240                 alert_text = value['alert'].get('text')
    241             raise exception_class(message, screen, stacktrace, alert_text)
--> 242         raise exception_class(message, screen, stacktrace)
    243 
    244     def _value_or_default(self, obj, key, default):

ElementNotInteractableException: Message: Element <input id="FirstName-guide-request-d277b94a662f45e686bf443012505716" class="form-control" name="FirstName" type="text"> is not reachable by keyboard

There is a search box at the top of the page that I can enter text into just fine with the same code, but modified with a different Name. Here is the html:

<input id="GlobalSearchMobile" name="q" class="srch-term form-control" type="text" autocomplete="off" placeholder="Search by Keyword or SKU">

And here is the code I use to enter text into that search box:

driver = webdriver.Firefox(executable_path = r'C:\Users\jajacobs\Downloads\geckodriver.exe')

driver.get("https://www.graphicproducts.com/guides/5s-system/")
driver.execute_script("window.scrollTo(0, 1000);")

driver.find_element_by_name('q').send_keys('test', Keys.ENTER)

driver.close()

So, basically I cannot figure out why the second set of code works for the search box at the top of the page, while the first set of code does not work for the form in the middle of the page.

I'm happy to supply more html if that is help.

I'm using Python 3.7 in a Jupyter Notebook launch with Anaconda on a Windows 10 machine.

Thank you in advance!

Jarod Jacobs
  • 21
  • 1
  • 3
  • I forgot to mention that the page I'm trying to interact with has an exit intent pop-up that appears if the mouse moves off the screen, but when Selenium opens the FireFox window I do not see that pop-up so am assuming (maybe incorrectly) that that is not impacting anything. – Jarod Jacobs Jan 16 '19 at 22:50
  • Also, I've tried finding the element by Xpath, partial Xpath, id, and class, with the same result. – Jarod Jacobs Jan 16 '19 at 22:52
  • 1
    The reason why, is that you are sending 2 keys at once ... sendkeys() can take just one parameter ... so try instead ( 'test' + Keys.Enter ) and if it doesnt help, try to send Only the 'test' and then send to the same an enter key – StyleZ Jan 16 '19 at 23:09
  • StyleZ, Thank you for you suggestion! I deleted the second parameter - Keys.Enter - but got the same error. I believe send_keys() can take two parameters as it works with both when I use it for the search box at the top of the page. Either way, deleting the second parameter didn't do the trick. Thanks though! – Jarod Jacobs Jan 16 '19 at 23:15

1 Answers1

0

If you look at the html of the webpage, there are two elements with name field value of "FirstName". (You can search for the string '//*[@name="FirstName"]' in developer options to check.)

So you can use something like this to get the element which you are looking for:

driver.find_elements_by_name('FirstName')[1].send_keys('test', Keys.ENTER)

Here, the find_elements.. method returns list of all the elements which have same locator instead of the first from top like find_element.. method, and the element which you are searching for comes second from top so we index the result of find_elements.. with index '1'.

BTW, I tried this in Chrome, but it should work on Firefox as well.

Kamal
  • 2,384
  • 1
  • 13
  • 25