[英]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 .我正在尝试使用selenium从Steam 主页上消失的下拉菜单中找到一个元素。 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:浏览器快照:
You can find a couple of relevant discussions on NoSuchElementException in:您可以在以下位置找到一些关于NoSuchElementException的相关讨论:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.