Reputation: 47
I am trying to open the browser and enter the search item in the search input box and perform enter.
I am able to open the browser and able to enter the search keyword in the search box but i am unable to perform the enter. Also when i enter the keyword , i get few list of suggestion that i could not select the first item. I am not sure what is going wrong with the code.
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.ui import WebDriverWait as wait
from selenium.webdriver.support import expected_conditions as EC
from selenium import webdriver
from selenium.webdriver.common.by import By
browser = webdriver.Firefox()
browser.get("https://www.tickertape.in/stocks/")
browser.maximize_window()
inputElement=browser.find_element_by_id('search-stock-input')
inputElement.click()
inputElement.send_keys('Reliance industries')
inputElement.click()
I tried below two options to click/enter the list but i am not able to select/enter it.
inputElement = wait(browser, 5).until(EC.visibility_of_element_located((By.CSS_SELECTOR, '.Select-option#"react-autowhatever-1-section-0-item-1')))
inputElement.click()
actions = ActionChains(browser)
actions.move_to_element(inputElement).click().send_keys('Reliance industries')
actions.perform()
This is the HTML.
//*[@id="search-stock-input"]
<input type="search" value="Reliance Industries" autocomplete="off" aria-autocomplete="list" aria-controls="react-autowhatever-1" class="jsx-4060663325 stock-input-box full-width" placeholder="Search stocks, indices, ETFs, Mutual Funds or brands" maxlength="80" id="search-stock-input" aria-label="search text">
<div id="react-autowhatever-1" role="listbox" class="jsx-513088474 jsx-507199909 jsx-145021258 react-autosuggest__suggestions-container"><ul class="jsx-513088474 jsx-507199909 jsx-145021258 d-flex tags-list"></ul><div class="jsx-513088474 jsx-507199909 jsx-145021258 assets-suggestion-container "></div></div>
Thanks in advance
Upvotes: 0
Views: 875
Reputation: 266
Try this, Hope this works
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.ui import WebDriverWait as wait
from selenium.webdriver.support import expected_conditions as EC
from selenium import webdriver
from selenium.webdriver.common.by import By
browser = webdriver.Firefox()
browser.get("https://www.tickertape.in/stocks/")
browser.maximize_window()
inputElement=browser.find_element_by_id('search-stock-input')
inputElement.click()
inputElement.send_keys('Reliance industries')
inputElement.click()
inputElement = wait(browser, 5).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "#search-stock-input")))
inputElement.click()
inputElement.send_keys(Keys.RETURN)
Upvotes: 1
Reputation: 33351
Try sending the text with actions
, not with driver
. This will perform it slowly and possibly resolve the problem of suggested results.
Also try sending Enter
key to the search field instead of clicking there.
Also you should add wait to let the search field element be fully rendered.
As following:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains
browser = webdriver.Firefox()
wait = WebDriverWait(browser, 20)
actions = ActionChains(browser)
browser.get("https://www.tickertape.in/stocks/")
browser.maximize_window()
input_element = wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "#search-stock-input")))
actions.move_to_element(input_element)
actions.click()
actions.send_keys('Reliance industries')
actions.send_keys(Keys.RETURN)
In case the
actions.send_keys(Keys.RETURN)
not worked you can try
actions.send_keys(Keys.ENTER)
Upvotes: 1