使用 selenium 网络驱动程序在网站搜索框中输入搜索字符串后无法单击回车

Unable to click enter after entering the search string in the website search box using selenium web driver

我正在尝试打开浏览器,在搜索输入框中输入搜索项并执行回车。

我可以打开浏览器并在搜索框中输入搜索关键字,但我无法执行输入。此外,当我输入关键字时,我得到的建议列表很少,我无法 select 第一项。我不确定代码出了什么问题。

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

我尝试了以下两个选项 click/enter 列表,但我无法 select/enter 它。

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

这是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>

这是网站截图

提前致谢

尝试使用 actions 发送文本,而不是 driver。这将执行缓慢并可能解决建议结果的问题。
也可以尝试将 Enter 键发送到搜索字段而不是单击那里。
您还应该添加等待以让搜索字段元素完全呈现。
如下:

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)

万一

actions.send_keys(Keys.RETURN)

没成功可以试试

actions.send_keys(Keys.ENTER)

试试这个,希望它有效

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)