How To View Text Options Within A Dropdown Menu
The HTML of the select list looks like: However when I run: month = driver.find_element_by_id('date_range_month_start_chosen') month.click() ## make dropdown list visible in bro
Solution 1:
A couple of points here:
- Use the
Select
Class to work with theselect
andoptions
tags. - We should try to select an
option
only throughSelect
type of object. Here is the code block for your reference :
selectmonth = Select(driver.find_element_by_id('date_range_month_start')) for option in selectmonth.options: print(option.text)
Update
If you face a ElementNotVisibleException
due to style="width: 109px; display: none;
use this block of code:
element = driver.find_element_by_id('date_range_month_start')
driver.execute_script("return arguments[0].removeAttribute('style');", element)
selectmonth = Select(driver.find_element_by_id('date_range_month_start'))
for option in selectmonth.options:
print(option.text)
Solution 2:
Selenium does that when an element is not visible on the screen.
You have display: none
in your style
tag. I think you'll find that if you remove it, then you will see what you expect.
I think this is because selenium is built to emulate user behaviour - and a user can not read text that is not shown.
Post a Comment for "How To View Text Options Within A Dropdown Menu"