0

I am trying to use selenium in python to input a search into a website.

From the examples I have seen, the tag, class, id etc is targeted and a value is inserted using

inputElement.send_keys('example')

On a webpage I'm trying to target a search bar that takes a zip code, input a zip so that I can scrape the resulting page but I'm having trouble.

<form class="geoLoc" action="javascript:void(0);" data-id="d866e6d3-106a-468a-8428-9fce1c8c51b6" data-baseurl="">
<fieldset class="search">
    <div class="search-input-wrap">
        <input type="tel" name="geolocation" maxlength="5" data-mask="00000" placeholder="Update Your ZIP Code" autocomplete="off">
        <div class="cta geoloc-btn">
            <button type="submit"><span>GO&nbsp;&nbsp;></span></button>
        </div>
    </div>
</fieldset>

I use the following code to target the form:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

driver = webdriver.Firefox()
driver.get("http://example.com")

login_form = driver.find_element_by_class_name('search-input-wrap')

login_form.send_keys("55555")

Whether I use the solution above or Paulo's answer I get the following error

Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "/Users/fsauceda/anaconda/lib/python2.7/site-packages/selenium/webdriver/remote/webelement.py", line 322, in send_keys
        self._execute(Command.SEND_KEYS_TO_ELEMENT, {'value': typing})
      File "/Users/fsauceda/anaconda/lib/python2.7/site-packages/selenium/webdriver/remote/webelement.py", line 448, in _execute
        return self._parent.execute(command, params)
      File "/Users/fsauceda/anaconda/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 196, in execute
        self.error_handler.check_response(response)
      File "/Users/fsauceda/anaconda/lib/python2.7/site-packages/selenium/webdriver/remote/errorhandler.py", line 181, in check_response
        raise exception_class(message, screen, stacktrace)
    selenium.common.exceptions.ElementNotVisibleException: Message: Element is not currently visible and so may not be interacted with
    Stacktrace:
        at fxdriver.preconditions.visible (file:///var/folders/tc/12qc3v0j47x0t6djfmzpb9200000gn/T/tmpem5v0u/extensions/fxdriver@googlecode.com/components/command-processor.js:9982)
        at DelayedCommand.prototype.checkPreconditions_ (file:///var/folders/tc/12qc3v0j47x0t6djfmzpb9200000gn/T/tmpem5v0u/extensions/fxdriver@googlecode.com/components/command-processor.js:12626)
        at DelayedCommand.prototype.executeInternal_/h (file:///var/folders/tc/12qc3v0j47x0t6djfmzpb9200000gn/T/tmpem5v0u/extensions/fxdriver@googlecode.com/components/command-processor.js:12643)
        at DelayedCommand.prototype.executeInternal_ (file:///var/folders/tc/12qc3v0j47x0t6djfmzpb9200000gn/T/tmpem5v0u/extensions/fxdriver@googlecode.com/components/command-processor.js:12648)
        at DelayedCommand.prototype.execute/< (file:///var/folders/tc/12qc3v0j47x0t6djfmzpb9200000gn/T/tmpem5v0u/extensions/fxdriver@googlecode.com/components/command-processor.js:12590)
Community
  • 1
  • 1
DataTx
  • 1,839
  • 3
  • 26
  • 49
  • That error message is not very helpful. Is there something like "NoSuchElementException" a few lines above? Also, see if [this answer](http://stackoverflow.com/questions/20606775/selenium-webdriver-finding-element-in-next-link) helps. Particularly the part about `implicitly_wait`. – Paulo Almeida Sep 02 '15 at 22:27
  • @PauloAlmeida the element is hidden. Do you know I make it visible? Maybe this http://stackoverflow.com/questions/27244589/selecting-element-in-python-selenium – DataTx Sep 02 '15 at 22:31
  • Try [this answer](http://stackoverflow.com/questions/15049182/write-value-to-hidden-element-with-selenium-python-script) – Paulo Almeida Sep 02 '15 at 22:37
  • Is there more than one element of the type you are searching for? What if you do `driver.find_elements_by_name('geolocation').length`? Does it return more than 1? (I don't know python so maybe it's not .length?) – JeffC Sep 03 '15 at 00:57

1 Answers1

0

You are finding the div, not the input. You can do this:

field = driver.find_element_by_name('geolocation')

See alternatives in the documentation.

Paulo Almeida
  • 7,803
  • 28
  • 36
  • thanks for the answer Paulo. I tried that as well and should of included it, but that does not work either. – DataTx Sep 02 '15 at 22:20