【问题标题】:Unable to click enter after entering the search string in the website search box using selenium web driver使用 selenium web driver 在网站搜索框中输入搜索字符串后无法点击回车
【发布时间】:2021-10-22 23:28:34
【问题描述】:

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

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

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, '.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>

这是网站截图

提前致谢

【问题讨论】:

  • 我会做的,因为我不知道我需要接受答案。我会检查指南。
  • 如果我的回答对你有用,请告诉我
  • 网络浏览器正在打开,但没有搜索到关键字,并且没有打开特定关键字搜索的站点。
  • 当然,我的代码缺少一些您已经拥有的代码。将更新它以使其清晰
  • 网页仍在打开,但没有搜索到关键字,并且特定关键字站点未打开。执行代码后,我得到以下日志。

标签: python-3.x selenium-webdriver css-selectors selenium-firefoxdriver


【解决方案1】:

试试这个,希望这有效

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)

【讨论】:

    【解决方案2】:

    尝试使用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)
    

    【讨论】:

      猜你喜欢
      • 2018-04-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-13
      • 2018-07-24
      • 1970-01-01
      • 2023-01-08
      相关资源
      最近更新 更多