【问题标题】:How to send text to the search field through Selenium and Python如何通过 Selenium 和 Python 将文本发送到搜索字段
【发布时间】:2019-07-15 09:57:37
【问题描述】:

我正在尝试使用 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 或 name。它仅按类包含元素。

我尝试了以下

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"

我也尝试过上述两种“typeahead search-box-input left”类的方法。它会引发相同的错误。以下是我用于此的代码:

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

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

【问题讨论】:

  • 抱歉,您输入了 HTML,输入字段为 class="typeahead search-box-input left",如果您想在必须获取后选择 class="search-box-inner"它的 2 个孩子
  • 嗨,卡洛,我也尝试过使用这个类“预先输入搜索框输入左侧”,但仍然没有用。
  • 很奇怪...你能添加一个ID再试一次吗?否则选择父对象并打印它以查看你得到了什么,然后选择子对象并再次尝试打印你拥有的东西,直到你得到文本字段;)

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


【解决方案1】:

find_elements_by_xpath 返回一个 webelement 列表,这就是你不能直接使用 send_keys 方法的原因。如果需要在元素上使用send_keys,则需要使用find_element_by_xpathfind_elements_by_xpath("xpathExpression")[0]

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

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

  2. 即使使用了上面的xpath,如果你得到ElementNotVisibleException,那么请检查元素是否在iframe中,如果是,则切换到iframe,然后在元素上使用send_keys

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

【讨论】:

  • 谢谢萨米尔。我尝试过这种方式,虽然我能够获得 webelement 但我无法将任何密钥传递给它。它再次给了我“ElementNotVisibleException”。然后我检查了元素是否在框架中,但事实并非如此。如果我最后遗漏了什么,请告诉我。谢谢。
  • 并尝试在获取元素之前稍等片刻,这样应该可以正常工作。
  • @NidhiArora 你检查了新的 xpath,它工作了吗?
  • 完成 :) 谢谢!
  • @NidhiArora 你是怎么解决这个问题的?我面临同样的问题。我能够获得 webelement,但我无法将任何密钥传递给它。即使它不在 iframe 中。在获取元素之前还包括 time.sleep(5)
【解决方案2】:

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

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

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

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

【讨论】:

    【解决方案3】:

    我会使用这个拉伸:

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

    【讨论】:

      【解决方案4】:

      根据您提供的 HTML,很明显 HTML DOM 内存在多个 &lt;form&gt; 节点。


      分析错误

      • 第一次尝试:self.driver.find_elements_by_class_name('search-box-inner') 将返回一个 List,因此您无法调用 send_keys(),您会看到错误为 AttributeError: 'list' object has no attribute 'send_keys'
      • 第二次尝试:self.driver.find_elements_by_class_name('search-box-inner')[0] 将识别无法在 focus 中的 &lt;form&gt; 节点,因此您无法调用 send_keys(),您会看到错误为 WebDriverException: Message: unknown error: cannot focus element"
      • 第三次尝试:self.driver.find_elements_by_xpath("//*[@class='typeahead search-box-input left']") 将再次返回 List,因此您无法调用 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")
        

      【讨论】:

        猜你喜欢
        • 2018-12-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-02-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多