[Python] Selenium selecting element by value not possible

Discussão em 'Python' iniciado por Stack, Março 16, 2025.

  1. Stack

    Stack Membro Participativo

    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.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')


    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}")
    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


    As you can see this should be the first element with value = "1"

    [​IMG]

    How can i select the first opiton using selenium?

    Continue reading...

Compartilhe esta Página