1

I am using Python Selenium for surfing on a webpage. On this dropdown list, I must select the option "Appartment", option value = "2_4_18_31"

Would you know how to do it? I try to use "send Keys but it does not work :

my code :

    APPARTEMENT = driver.find_element(By.NAME, "data[Search][idtype][]")
    APPARTEMENT.click()
    time.sleep(1)
    APPARTEMENT.send_keys("Appartement")
    time.sleep(1)
    APPARTEMENT.send_keys(Keys.ENTER)

The HTLML :

<select name="data[Search][idtype][]" id="inputTypeBien_transac" class="form-control selectpicker" multiple="" title="Type de bien" data-style="btn-selectpicker" style="display: none;">
        <option value="void" disabled="">Type de bien</option>
            <option value="1_25_22_32_7_26_11_38_39_27_41_34">Maison</option>
        <option value="2_4_18_31" selected="selected">Appartement</option>
        <option value="5">Terrain</option>
        <option value="15_16">Parking/Garages</option>
        <option value="pneuf">Programme Neuf</option>
        <option value="prestige">Prestige</option>
        <option value="21">Immeuble</option>
        <option value="28_30_10_29_10_20_17_24">Autres</option>
        <option value="44">Terrain agricole</option>
        <option value="43">Terrain à batir</option>
        <option value="45">Terrain de loisir</option>
</select>

2 Answers 2

0

You need to use Selenium's Select Class to handle dropdowns in selenium.

Refer the code below:

time.sleep(5)
dropdown = Select(driver.find_element(By.ID, "inputTypeBien_transac"))

dropdown.select_by_visible_text("Appartement")

# or this
dropdown.select_by_value("2_4_18_31")

Imports:

from selenium.webdriver.support.ui import Select
3
  • Unfortunately this code does not work.. Commented Mar 4, 2024 at 20:46
  • What error/exception do you see?
    – Shawn
    Commented Mar 4, 2024 at 22:33
  • using this code: time.sleep(5) dropdown = Select(driver.find_element(By.ID, "inputTypeBien_transac")) dropdown.select_by_visible_text("Appartement") -->I have thsi exception : NoSuchElementException Commented Mar 6, 2024 at 21:17
0

SeleniumBase has some shortcuts for that:

sb.select_option_by_text(dropdown_selector, option, dropdown_by="css selector")
sb.select_option_by_index(dropdown_selector, option, dropdown_by="css selector")
sb.select_option_by_value(dropdown_selector, option, dropdown_by="css selector")

In your case, you could use:

sb.select_option_by_text("#inputTypeBien_transac", "Appartement")

A full example:

from seleniumbase import SB

with SB(demo=True) as sb:
    sb.open("https://seleniumbase.io/demo_page")
    sb.select_option_by_text("#mySelect", "Set to 75%")
    sb.sleep(1)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.