【问题标题】:How to use Selenium for text input in python如何在 python 中使用 Selenium 进行文本输入
【发布时间】:2015-09-02 22:10:23
【问题描述】:

我正在尝试在 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)

【问题讨论】:

  • 该错误消息不是很有帮助。上面几行是否有类似“NoSuchElementException”的东西?另外,看看this answer 是否有帮助。特别是关于implicitly_wait的部分。
  • @PauloAlmeida 该元素已隐藏。你知道我让它可见吗?也许这个stackoverflow.com/questions/27244589/…
  • 试试this answer
  • 您要搜索的类型是否不止一个?如果你做driver.find_elements_by_name('geolocation').length 怎么办?它返回超过 1 吗? (我不知道python所以也许它不是.length?)

标签: python selenium


【解决方案1】:

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

field = driver.find_element_by_name('geolocation')

documentation 中查看替代方案。

【讨论】:

  • 感谢保罗的回答。我也试过了,应该包含它,但这也不起作用。
猜你喜欢
  • 2021-04-26
  • 2021-10-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-04
  • 2014-06-29
  • 2013-05-05
  • 2013-07-04
相关资源
最近更新 更多