1

I'm writing a python script to grab a preselected option in a dropdown menu and then storing that grabbed option in a variable that is created. Need some help with getting the preselected option.

As you can see below, the option with an already existing "selected" When the page loads, the text box is already filled with the text relevant to that option tag

<select name="ctl00$ContentPlaceHolder1$homeParkDropDownList" id="ContentPlaceHolder1_homeParkDropDownList">
    <option value="-1"></option>
    <option value="2"></option>
    <option value="3"></option>
    <option value="15"></option>
    <option selected="selected" value="8"></option>
    <option value="9"></option>
    <option value="12"></option>
    <option value="100"></option>
    <option value="19"></option>
    <option value="14"</option>
    <option value="13"></option>
    <option value="18"></option>
    <option value="6"></option>
    <option value="4"></option>
    <option value="5"></option>
    <option value="1"></option>
    <option value="7"></option>
    <option value="34"></option>
    <option value="11"></option>

</select>

I would like to grab the text that already has that 'selected' tag

Current code so far

select = Select(driver.find_element(By.ID, "ContentPlaceHolder1_homeParkDropDownList"))
options = select.options
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
FallenEgg
  • 23
  • 5

1 Answers1

0

You need to use first_selected_option as follows:

select = Select(driver.find_element(By.ID, "ContentPlaceHolder1_homeParkDropDownList"))
element = select.first_selected_option

To print the text:

print(Select(driver.find_element(By.ID, "ContentPlaceHolder1_homeParkDropDownList")).first_selected_option.text)
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352