如何在python中使用Selenium进行文本输入

时间:2015-09-02 22:10:23

标签: python selenium

我正在尝试在python中使用selenium来搜索网站。

从我看到的examples开始,标记,类,ID等被定位,并使用

插入一个值
inputElement.send_keys('example')

在网页上,我试图定位一个带有邮政编码的搜索栏,输入一个拉链,以便我可以抓取结果页面,但我遇到了麻烦。

<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>

我使用以下代码来定位表单:

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")

无论我使用上述解决方案还是Paulo的答案,我都会收到以下错误

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)

1 个答案:

答案 0 :(得分:0)

您找到的是div,而不是input。你可以这样做:

field = driver.find_element_by_name('geolocation')

请参阅documentation中的备选方案。