How to send text to the search field through Selenium and Python

给你一囗甜甜゛ 提交于 2020-06-27 12:05:01

问题


I am trying to pass arguments to a search text in a HTML code using Selenium in Python:

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>

The code does not have id or name. It has elements by class only.

I tried the following

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

But I am getting the following error:

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

I get the following error:

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

I again got the following error:

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

I again got the following error:

selenium.common.exceptions.ElementNotVisibleException: Message:
element not interactable

I also tried the following way:

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.


回答1:


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").get(0) if you need to use send_keys on the element.

Please try the below suggestions to solve your problem:

  1. Try the xpath self.driver.find_element_by_xpath("//div[@class='input-container']") instead of the xpath you are using.

  2. 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.

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




回答2:


Why not use a css selector e.g.

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

You could extend the selector, for example, with:

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



回答3:


As per the HTML you have provided it is pretty clear that there are multiple <form> nodes present within the HTML DOM.


Analizing the errors

  • First attempt: self.driver.find_elements_by_class_name('search-box-inner') would return a List so you can't invoke send_keys() and you see the error as AttributeError: 'list' object has no attribute 'send_keys'
  • Second attempt: self.driver.find_elements_by_class_name('search-box-inner')[0] will identify the <form> node which can't be in focus so you can't invoke send_keys() and you see the error as WebDriverException: Message: unknown error: cannot focus element"
  • Third attempt: self.driver.find_elements_by_xpath("//*[@class='typeahead search-box-input left']") would again return a List so you can't invoke send_keys() and you see the error as AttributeError: 'list' object has no attribute 'send_keys'
  • Forth attempt: self.driver.find_elements_by_xpath("//*[@class='typeahead search-box-input left']").[0] wasn't a valid Locator Strategy

Solution

To pass a character sequence to desired element you can use either of the following solution:

  • Using 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")
    
  • Using 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")
    


来源:https://stackoverflow.com/questions/54807892/how-to-send-text-to-the-search-field-through-selenium-and-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!