Reputation: 27
I put the "extract the text" in caps because I have yet to see any answer that works. I need to extract every option available in a drop down list that has two nested optgroups, I DO NOT want to just simply select the values. The html is as follows:
<select data-tracking-id="car_reviews_index_mmy_model_select" data-no-refresh="false" id="sel-3155799655901836" name="select-model" class="js-models-select mmy-select w-100 btn btn-sm btn-secondary text-left">
<option selected="" class="" disabled="" value="">Select Model</option>
<optgroup label="Popular Models" class="">
<option class="" value="ILX">ILX</option>
<option class="" value="MDX">MDX</option>
<option class="" value="NSX">NSX</option>
<option class="" value="RDX">RDX</option>
<option class="" value="RLX">RLX</option>
<option class="" value="TLX">TLX</option>
</optgroup><optgroup label="Other Models" class="">
<option class="" value="CL">CL</option>
<option class="" value="ILX Hybrid">ILX Hybrid</option>
<option class="" value="Integra">Integra</option>
<option class="" value="Legend">Legend</option>
<option class="" value="RL">RL</option>
<option class="" value="RSX">RSX</option>
<option class="" value="SLX">SLX</option>
<option class="" value="TL">TL</option>
<option class="" value="TSX">TSX</option>
<option class="" value="TSX Sport Wagon">TSX Sport Wagon</option>
<option class="" value="Vigor">Vigor</option>
<option class="" value="ZDX">ZDX</option>
</optgroup></select>
I have used the following code to select all the values in the first dropdown, which is not nested, and to select the value for the second drop down, but from which I can't seem to extract the text:
driver = webdriver.Chrome('WebScraping/chromedriver')
url = 'https://www.edmunds.com/car-reviews/'
driver.get(url)
cars = []
options = WebDriverWait(driver,10).until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, '[name="select-make"] option')))
for val in tqdm(range(1,69,1)):
options[val].click
cars.append(options[val].text)
select = Select(driver.find_element_by_name('select-make'))
select.select_by_value(cars[0].lower())
select_element = Select(driver.find_element_by_name('select-model'))
all_available_options = select_element.select_by_index(1)
Is anyone aware of how to get the TEXT VALUES of the second drop down?
Upvotes: 1
Views: 269
Reputation: 33384
First thing first to select the first drop down item you need use cars[1]
instead cars[0]
because it is already selected and disabled.
To get the text from second dropdown you need to select the first dropdown item first.
So your code will be like
select = Select(driver.find_element(By.NAME,'select-make'))
select.select_by_value(cars[1].lower())
select_element = Select(driver.find_element(By.NAME,'select-model'))
all_available_options =[option.text for option in select_element.options]
print(all_available_options)
Output:
['Select Model', 'ILX', 'MDX', 'NSX', 'RDX', 'RLX', 'TLX', 'CL', 'ILX Hybrid', 'Integra', 'Legend', 'RL', 'RSX', 'SLX', 'TL', 'TSX', 'TSX Sport Wagon', 'Vigor', 'ZDX']
Upvotes: 1