使用Selenium操作weather.com搜索框并选择选项遇问题求助
问题描述
我正在尝试实现让用户输入城市名并在weather.com网站上进行搜索的功能。目前已通过Selenium完成向搜索框输入城市名的操作,但无法实现选择搜索建议选项的功能(如图所示)。

当前代码
driver = webdriver.Chrome() driver.get("https://weather.com/") search = driver.find_element(By.CLASS_NAME, "SearchInput--InputField--1UoCv Search--inputClass--1FEhl") city = input("Enter a city") search.send_keys(city)
异常信息
运行代码后出现如下异常:
Traceback (most recent call last): File "c:\Users\Richard\Documents\GitHub\sunset-predictor\main.py", line 22, in <module> getInfo() File "c:\Users\Richard\Documents\GitHub\sunset-predictor\main.py", line 19, in getInfo search = driver.find_element(By.CLASS_NAME, "SearchInput--InputField--1UoCv Search--inputClass--1FEhl") ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\Richard\AppData\Local\Programs\Python\Python311\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 741, in find_element return self.execute(Command.FIND_ELEMENT, {"using": by, "value": value})["value"] ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\Richard\AppData\Local\Programs\Python\Python311\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 347, in execute self.error_handler.check_response(response) File "C:\Users\Richard\AppData\Local\Programs\Python\Python311\Lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 229, in check_response raise exception_class(message, screen, stacktrace) selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":".SearchInput--InputField--1UoCv Search--inputClass--1FEhl"} (Session info: chrome=119.0.6045.160); For documentation on this error, please visit: https://www.selenium.dev/documentation/webdriver/troubleshooting/errors#no-such-element-exception Stacktrace: GetHandleVerifier [0x00007FF70BFD82B2+55298] (No symbol) [0x00007FF70BF45E02] (No symbol) [0x00007FF70BE005AB] (No symbol) [0x00007FF70BE4175C] (No symbol) [0x00007FF70BE418DC] (No symbol) [0x00007FF70BE7CBC7] (No symbol) [0x00007FF70BE620EF] (No symbol) [0x00007FF70BE7AAA4] (No symbol) [0x00007FF70BE61E83] (No symbol) [0x00007FF70BE3670A] (No symbol) [0x00007FF70BE37964] GetHandleVerifier [0x00007FF70C350AAB+3694587] GetHandleVerifier [0x00007FF70C3A728E+4048862] GetHandleVerifier [0x00007FF70C39F173+4015811] GetHandleVerifier [0x00007FF70C0747D6+695590] (No symbol) [0x00007FF70BF50CE8] (No symbol) [0x00007FF70BF4CF34] (No symbol) [0x00007FF70BF4D062] (No symbol) [0x00007FF70BF3D3A3] BaseThreadInitThunk [0x00007FFCA2CF7344+20] RtlUserThreadStart [0x00007FFCA3F226B1+33]
我认为当前查找的元素存在错误。
内容的提问来源于stack exchange,提问作者Richard
相关产品推荐
相关产品推荐

