I would like to choose a value from this optgroup which should then give me a dropdown of links.
<div class="searchbar">
<select id="q" multiple="" tabindex="-1" class="select2-hidden-accessible" aria-hidden="true">
<option></option>
<option class="q-all-text" value="al:all">Search all text</option>
<optgroup label="Business Type">
<option value="bt:Buyer">Buyer</option>
<option value="bt:Farmer/Rancher">Farmer/Rancher</option>
<option value="bt:Farmers Market">Farmers Market</option>
<option value="bt:Fishery">Fishery</option>
<option value="bt:Food Bank">Food Bank</option>
</optgroup>
Here is my code so far:
driver = webdriver.Chrome('/path/to/chromedriver')
driver.get("https://foodmarketmaker.com/main/mmsearch")
iframe = driver.find_element_by_tag_name("iframe")
driver.switch_to.frame(iframe)
select = Select(driver.find_element_by_class_name("select2-hidden- accessible"))
select.select_by_value("bt:Farmer/Rancher")
links = driver.find_elements_by_tag_name('a')
print(links)
for link in links:
print(link.get_attribute('href'))
I either get an exception that the element does not exist, or when I access by index, i get an ElementNotVisibleException due to the aria-hidden attribute being true. Is there any way around this?
As per the HTML you have shared to print all the options you have to induce WebDriverWait to switch to the desired frame, then click on the Search Box for the options to be visible as follows :
Code Block :
Console Output :
select
is not the node that you need to handle as it's not visible.Try below code to get required output:
P.S. You need to replace text value of
li
node in'//li[.="Bass"]'
XPath with required option