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
  • 1 Are you sure the pictured element is the same one found by your code? Are there other elements on the page that would match <select class="py-4"> ? – John Gordon Commented Mar 16 at 20:44
  • Yes - when you check the website there are 4 select-boxes and i chossed the first one – Rapid1898 Commented Mar 16 at 20:46
  • 1 Perhaps you need a delay after loading the page, to be sure all the elements have time to render properly? – John Gordon Commented Mar 16 at 20:50
  • Or perhaps the desired element is inside a <frame> or <iframe> element? – John Gordon Commented Mar 16 at 20:58
  • Indeed you need to wait a bit after loading the page. When I copy your code and put time.sleep(1) before the XPath line then it works without any problems. – Marijn Commented Mar 16 at 21:23
 |  Show 1 more comment

2 Answers 2

Reset to default 1

The 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