如何通过Selenium和Python将文本发送到搜索字段 [英] How to send text to the search field through Selenium and Python
问题描述
我正在尝试使用Python中的Selenium将参数传递给HTML代码中的搜索文本:
I am trying to pass arguments to a search text in a HTML code using Selenium in Python:
我正在处理以下HTML代码:
I am working on the following HTML code:
</form><form class="search-box-inner">
<div class="input-container">
<input type="text" class="typeahead search-box-input left" autocomplete="off" placeholder="Search ratings, research, analysts, and more..." maxlength="900"></div>
</form>
该代码没有ID或名称.它仅按类具有元素.
The code does not have id or name. It has elements by class only.
我尝试了以下
self.driver.find_elements_by_class_name('search-box-inner').send_keys("HSBC Bank plc")
但是出现以下错误:
AttributeError: 'list' object has no attribute 'send_keys'
然后,我尝试使用以下代码获取列表的第一个元素并发送密钥:
Then I tried to fetch first element of the list and send keys by using the following code:
self.driver.find_elements_by_class_name('search-box-inner')[0].send_keys("HSBC Bank plc")
我收到以下错误:
"selenium.common.exceptions.WebDriverException: Message: unknown error: cannot focus element"
我也尝试过上述两种方法,用于预先输入左侧搜索框输入"类.它会引发相同的错误.以下是我用于此的代码:
I have tried both the above methods for "typeahead search-box-input left" class as well. it throws the same error. following is the code I used for this:
self.driver.find_elements_by_xpath("//*[@class='typeahead search-box-input left']").send_keys("HSBC Bank plc")
我再次遇到以下错误:
AttributeError: 'list' object has no attribute 'send_keys'
然后,我尝试使用以下代码获取列表的第一个元素:
Then I tried to fetch the first element of the list with the following code:
self.driver.find_elements_by_xpath("//*[@class='typeahead search-box-input left']").[0].send_keys("HSBC Bank plc")
我再次遇到以下错误:
selenium.common.exceptions.ElementNotVisibleException: Message:
element not interactable
我还尝试了以下方法:
element = wait.until(Ec.visibility_of_element_located((By.XPATH, "//*[@placeholder='Search ratings, research, analysts, and more...']")))
但是无法将参数/键发送到搜索栏.
任何建议都会非常有帮助.
But could not sent the argument/keys to the search bar.
Any suggestions will be really helpful.
推荐答案
find_elements_by_xpath
返回Web元素的列表,因此这就是您不能直接在其上使用send_keys
方法的原因.如果需要在元素上使用send_keys
,则需要使用find_element_by_xpath
或find_elements_by_xpath("xpathExpression")[0]
.
find_elements_by_xpath
returns a list of webelement so that is the reason you cant use the send_keys
method directly on that. You need to use find_element_by_xpath
or find_elements_by_xpath("xpathExpression")[0]
if you need to use send_keys
on the element.
请尝试以下建议解决您的问题:
Please try the below suggestions to solve your problem:
-
尝试使用xpath
self.driver.find_element_by_xpath("//div[@class='input-container']")
而不是您正在使用的xpath.
Try the xpath
self.driver.find_element_by_xpath("//div[@class='input-container']")
instead of the xpath you are using.
即使在使用上述xpath后如果得到ElementNotVisibleException
,也请检查该元素是否在iframe中,如果是,则切换到iframe,然后在该元素上使用send_keys
.
Even after using the above xpath if you get ElementNotVisibleException
then please check if the element is in iframe, if yes, then switch to the iframe and then use send_keys
on the element.
要切换到iframe,您可以使用:driver.switch_to.frame(driver.find_element_by_tag_name('iframe'))
,然后使用提到的xpath在元素上使用send_keys
,如果要切换回默认内容,可以使用driver.switch_to.default_content()
To switch to iframe you can use: driver.switch_to.frame(driver.find_element_by_tag_name('iframe'))
and then send_keys
on the element using the mentioned xpath and if you want to switch back to the default content, you can use driver.switch_to.default_content()
这篇关于如何通过Selenium和Python将文本发送到搜索字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!