admin管理员组文章数量:1389925
i try to select an element using selenium with the following code:
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.webdrivermon.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 = ";
driver.get (startLlink)
select = Select(driver.find_element(By.XPATH,'(//select[@class="py-4"])[1]'))
select.select_by_value('1')
But i allways get this error:
(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}")
seleniummon.exceptions.NoSuchElementException: Message: Cannot locate option with value: 1; For documentation on this error, please visit: /documentation/webdriver/troubleshooting/errors#no-such-element-exception
As you can see this should be the first element with value = "1"
How can i select the first opiton using selenium?
i try to select an element using selenium with the following code:
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.webdrivermon.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')
But i allways get this error:
(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}")
seleniummon.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
As you can see this should be the first element with value = "1"
How can i select the first opiton using selenium?
Share Improve this question asked Mar 16 at 20:37 Rapid1898Rapid1898 1,2473 gold badges15 silver badges46 bronze badges 6 | Show 1 more comment2 Answers
Reset to default 1The problem is that the SELECT OPTIONs take a moment to fully load. We can wait for this with a custom wait to make sure the number of OPTIONs is greater than 1.
Working code
from selenium import webdriver
from selenium.webdrivermon.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import Select
from selenium.webdriver.support.wait import WebDriverWait
driver = webdriver.Chrome()
url = "https://www.firmenabc.at/bauen-wohnen-einrichten"
driver.get(url)
wait = WebDriverWait(driver, 10)
# close cookie popup
wait.until(EC.element_to_be_clickable((By.ID, "CybotCookiebotDialogBodyButtonDecline"))).click()
select = Select(driver.find_element(By.XPATH, "//label[text()='Bundesland']//select"))
wait.until(lambda _ : len(select.options) > 1)
select.select_by_value("1")
driver.quit()
First try to wait for element to be visible by xpath then try to select value
Or it did not work try to select by visibletext
本文标签: pythonSelenium selecting element by value not possibleStack Overflow
版权声明:本文标题:python - Selenium selecting element by value not possible - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744586622a2614233.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
<select class="py-4">
? – John Gordon Commented Mar 16 at 20:44<frame>
or<iframe>
element? – John Gordon Commented Mar 16 at 20:58time.sleep(1)
before the XPath line then it works without any problems. – Marijn Commented Mar 16 at 21:23