Issue
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.
Solution
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:
Try the xpath
self.driver.find_element_by_xpath("//div[@class='input-container']")
instead of the xpath you are using.Even after using the above
xpath
if you getElementNotVisibleException
then please check if the element is in iframe, if yes, then switch to the iframe and then usesend_keys
on the element.To switch to iframe you can use the property
selenium.webdriver.remote.webdriver.WebDriver.switch_to
:driver.switch_to.frame(driver.find_element_by_tag_name('iframe'))
and thensend_keys
on the element using the mentioned xpath and if you want to switch back to the default content, you can usedriver.switch_to.default_content()
Answered By - Sameer Arora
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.