如何在python中使用Selenium进行文本输入
我正在尝试使用python中的selenium将搜索输入到网站中 从我看到的示例中,标记、类、id等是有目标的,并且使用如何在python中使用Selenium进行文本输入,python,selenium,Python,Selenium,我正在尝试使用python中的selenium将搜索输入到网站中 从我看到的示例中,标记、类、id等是有目标的,并且使用 inputElement.send_keys('example') 在一个网页上,我试图定位一个带有邮政编码的搜索栏,输入一个邮政编码,这样我就可以刮取结果页面,但我遇到了麻烦 <form class="geoLoc" action="javascript:void(0);" data-id="d866e6d3-106a-468a-8428-9fce1c8c51b6"
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 ></span></button>
</div>
</div>
</fieldset>
无论我使用上面的解决方案还是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)
您正在查找div,而不是输入。您可以这样做:
field = driver.find_element_by_name('geolocation')
请参阅中的备选方案。谢谢您的回答,保罗。我也试过了,当然应该包括在内,但也不起作用。那个错误消息不是很有帮助。上面几行是否有类似NoTouchElementException的内容?另外,看看是否有帮助。特别是关于隐式等待的部分。@PauloAlmeida元素是隐藏的。你知道我让它可见吗?可能您正在搜索的类型有多个元素?如果您使用驱动程序。按名称“地理位置”查找元素。长度?它返回的值是否超过1?我不懂python,所以可能不是。长度?
field = driver.find_element_by_name('geolocation')