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" >
<option selected="" disabled="" value="">Select Model</option>
<optgroup label="Popular Models" >
<option value="ILX">ILX</option>
<option value="MDX">MDX</option>
<option value="NSX">NSX</option>
<option value="RDX">RDX</option>
<option value="RLX">RLX</option>
<option value="TLX">TLX</option>
</optgroup><optgroup label="Other Models" >
<option value="CL">CL</option>
<option value="ILX Hybrid">ILX Hybrid</option>
<option value="Integra">Integra</option>
<option value="Legend">Legend</option>
<option value="RL">RL</option>
<option value="RSX">RSX</option>
<option value="SLX">SLX</option>
<option value="TL">TL</option>
<option value="TSX">TSX</option>
<option value="TSX Sport Wagon">TSX Sport Wagon</option>
<option value="Vigor">Vigor</option>
<option 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?
CodePudding user response:
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']