简体   繁体   English

Python - 如何使用 selenium 从消失的下拉列表中查找元素

[英]Python - How to find an element from a disappearing drop down using selenium

I'm trying to find an element from a disappearing drop down on the steam homepage with selenium .我正在尝试使用seleniumSteam 主页上消失的下拉菜单中找到一个元素。 When you type something in the search bar, results will drop down, and if you click outside of it the drop-down results will disappear.当您在搜索栏中输入内容时,结果将下拉,如果您在其外部单击,则下拉结果将消失。 You would need to click it again if you want the results to appear again.如果要再次显示结果,则需要再次单击它。

Anyway, my code enters an input into the search bar, and the drop-downs do show when input_elem.send_keys(game) runs (I used "terraria" as the input), and every first result has the same css selector.无论如何,我的代码在搜索栏中输入了一个输入,并且当input_elem.send_keys(game)运行时下拉菜单会显示(我使用“terraria”作为输入),并且每个第一个结果都有相同的 css 选择器。 I also tried to find the element with the xpath, it doesn't work either:我还尝试使用 xpath 查找元素,它也不起作用:

from selenium import webdriver

game = input('Type the game you want to find here: ')

# configure browser
browser = webdriver.Firefox()
browser.get('https://store.steampowered.com/')

# input game
input_elem = browser.find_element_by_css_selector('#store_nav_search_term')
input_elem.send_keys(game)

# click the first result
first_match = browser.find_element_by_css_selector('a.match:nth-child(1)')
first_match.click()

Here's the full error:这是完整的错误:

Traceback (most recent call last):
  File "/home/fanjin/Documents/Python Projects/web_projects/steam/game_finder.py", line 14, in <module>
    first_match = browser.find_element_by_css_selector('a.match:nth-child(1)')
  File "/home/fanjin/.local/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 598, in find_element_by_css_selector
    return self.find_element(by=By.CSS_SELECTOR, value=css_selector)
  File "/home/fanjin/.local/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 976, in find_element
    return self.execute(Command.FIND_ELEMENT, {
  File "/home/fanjin/.local/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "/home/fanjin/.local/lib/python3.8/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: a.match:nth-child(1)

To click on the first auto suggestions you have to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies :要单击第一个自动建议,您必须为element_to_be_clickable()诱导WebDriverWait ,并且您可以使用以下任一Locator Strategies

  • Using CSS_SELECTOR :使用CSS_SELECTOR

     driver.get("https://store.steampowered.com/") WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input#store_nav_search_term"))).send_keys("terraria") WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div#search_suggestion_contents>a"))).click()
  • Using XPATH :使用XPATH

     driver.get("https://store.steampowered.com/") WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@id='store_nav_search_term']"))).send_keys("terraria") WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@id='search_suggestion_contents']/a"))).click()
  • Note : You have to add the following imports:注意:您必须添加以下导入:

     from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC
  • Browser Snapshot:浏览器快照:

泰拉瑞亚


References参考

You can find a couple of relevant discussions on NoSuchElementException in:您可以在以下位置找到一些关于NoSuchElementException的相关讨论:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Python Selenium访问下拉元素 - Accessing drop down element using Python Selenium 如何使用 selenium 通过 python 下拉元素? - how to drop down an element by python with selenium? 如何 select 使用 selenium python 来自谷歌表单下拉元素的值 - How to select a value from a google form drop down element using selenium python 如何使用 select 这个非 select 下拉菜单中的元素使用 Selenium Webdriver 和 ZA7F57F35426B9387411 - How to select this element from this non select drop-down menu using Selenium Webdriver and Python 如何使用 Selenium 和 Python 从下拉菜单中的 select 项目? - How to select item from drop down menu using Selenium with Python? Selenium Python:从下拉列表中选择元素 - Selenium Python : pick element from drop down in a span 使用Selenium从另一个框架中查找与下拉列表相关的元素-Python - Using Selenium to find elements related to drop down list from another frame — Python 使用硒python从下拉菜单中动态查找最高价值 - find top value dynamically from drop down menu using selenium python 如何使用 python selenium 拖放元素 - How to drag and drop the element using python selenium Python:如何从Selenium的下拉菜单中选择选项,元素已隐藏 - Python: How to select option from drop down menu in Selenium, element hidden
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM