Selenium无法通过value值选择下拉框元素的问题求助
Selenium无法通过value值选择下拉框元素的问题求助
我尝试用Selenium编写代码来选择页面上的下拉框元素,代码如下:
from selenium import webdriver from selenium.webdriver.chrome.options import Options from selenium.webdriver.chrome.service import Service from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import Select print(f"Checking Browser driver...") options = Options() # options.add_argument('--headless=new') options.add_argument("start-maximized") options.add_argument('--log-level=3') options.add_experimental_option("prefs", {"profile.default_content_setting_values.notifications": 1}) options.add_experimental_option("excludeSwitches", ["enable-automation"]) options.add_experimental_option('excludeSwitches', ['enable-logging']) options.add_experimental_option('useAutomationExtension', False) options.add_argument('--disable-blink-features=AutomationControlled') srv=Service() driver = webdriver.Chrome (service=srv, options=options) waitWD = WebDriverWait (driver, 10) startLlink = "https://www.firmenabc.at/bauen-wohnen-einrichten" driver.get (startLlink) select = Select(driver.find_element(By.XPATH,'(//select[@class="py-4"])[1]')) select.select_by_value('1')
但每次运行都会报错,错误信息如下:
(selenium) C:\DEVNEU\Fiverr2025\PROGRAMS\FirmenABC>python test.py Checking Browser driver... Traceback (most recent call last): File "C:\DEVNEU\Fiverr2025\PROGRAMS\FirmenABC\test.py", line 25, in <module> select.select_by_value('1') ~~~~~~~~~~~~~~~~~~~~~~^^^^^ File "C:\DEVNEU\.venv\selenium\Lib\site-packages\selenium\webdriver\support\select.py", line 84, in select_by_value raise NoSuchElementException(f"Cannot locate option with value: {value}") selenium.common.exceptions.NoSuchElementException: Message: Cannot locate option with value: 1; For documentation on this error, please visit: https://www.selenium.dev/documentation/webdriver/troubleshooting/errors#no-such-element-exception
从截图里能看到,下拉框的第一个选项value值确实是'1':

请问我该怎么用Selenium选中这个选项呢?
备注:内容来源于stack exchange,提问作者Rapid1898
相关产品推荐
相关产品推荐

