如何通过Selenium和Python将文本发送到搜索字段

时间:2019-02-21 13:08:41

标签: python selenium selenium-webdriver xpath css-selectors

我正在尝试使用Python中的Selenium将参数传递给HTML代码中的搜索文本:

我正在处理以下HTML代码:

</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或名称。它仅按类具有元素。

我尝试了以下

self.driver.find_elements_by_class_name('search-box-inner').send_keys("HSBC Bank plc")  

但是出现以下错误:

AttributeError: 'list' object has no attribute 'send_keys'

然后,我尝试使用以下代码获取列表的第一个元素并发送密钥:

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"

我也尝试过上述两种方法,用于“预先输入左侧搜索框输入”类。它会引发相同的错误。以下是我用于此的代码:

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'

然后,我尝试使用以下代码获取列表的第一个元素:

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...']")))

但是无法将参数/键发送到搜索栏。
任何建议都会非常有帮助。

4 个答案:

答案 0 :(得分:2)

find_elements_by_xpath返回一个Web元素列表,这就是您不能直接在其上使用send_keys方法的原因。如果需要在元素上使用find_element_by_xpath,则需要使用find_elements_by_xpath("xpathExpression").get(0)send_keys

请尝试以下建议解决您的问题:

  1. 尝试使用xpath self.driver.find_element_by_xpath("//div[@class='input-container']")而不是您正在使用的xpath。

  2. 即使在使用上述xpath之后,如果得到ElementNotVisibleException,也请检查元素是否在iframe中,如果是,则切换到iframe,然后在元素上使用send_keys

    要切换到iframe,您可以使用:driver.switch_to.frame(driver.find_element_by_tag_name('iframe')),然后使用提到的xpath在元素上使用send_keys,如果要切换回默认内容,可以使用driver.switch_to.default_content()

答案 1 :(得分:0)

为什么不使用CSS选择器,例如

.find_element_by_css_selector(".search-box-input").send_keys 

例如,您可以扩展选择器:

.search-box-input[placeholder^='Search ratings']

答案 2 :(得分:0)

根据您提供的HTML,很明显HTML DOM中存在多个<form>节点。


对错误进行分析

  • 第一次尝试:self.driver.find_elements_by_class_name('search-box-inner')将返回一个列表,因此您无法调用send_keys(),并且看到的错误为AttributeError: 'list' object has no attribute 'send_keys'
  • 第二次尝试:self.driver.find_elements_by_class_name('search-box-inner')[0]将标识不能成为焦点<form>节点,因此您无法调用send_keys()并且看到错误为WebDriverException: Message: unknown error: cannot focus element"
  • 第三次尝试:self.driver.find_elements_by_xpath("//*[@class='typeahead search-box-input left']")将再次返回一个列表,因此您无法调用send_keys(),并且看到的错误为AttributeError: 'list' object has no attribute 'send_keys'
  • 第四次尝试:self.driver.find_elements_by_xpath("//*[@class='typeahead search-box-input left']").[0]不是有效的Locator Strategy

解决方案

要将字符序列传递给所需的元素,可以使用以下任一解决方案:

  • 使用CSS_SELECTOR

    self.driver.find_element_by_css_selector("form.search-box-inner input.typeahead.search-box-input.left[placeholder^='Search ratings']").send_keys("HSBC Bank plc")
    
  • 使用XPATH

    self.driver.find_element_by_xpath("//form[@class='search-box-inner']//input[@class='typeahead search-box-input left' and starts-with(@placeholder, 'Search ratings')]").send_keys("HSBC Bank plc")
    

答案 3 :(得分:0)

我会用这个拉伸:

    #Importing libs
    from selenium import webdriver
    from selenium.webdriver.common.keys import Keys
    
    #Look for the class
    search_box = driver.find_element_by_class_name("typeahead search-box-input left")
    
    #Write what we be searched#
    search_box.send_keys('What will be searched')

    #Submit the text
    search_box.send_keys(Keys.RETURN)