Home > Net >  Python Selenium - Select only works on <select> elements, not on <div>
Python Selenium - Select only works on <select> elements, not on <div>

Time:04-02

I am trying to select an element in a drop down box from a work website. I am a bit new to web scraping selenium and need some pointing in the right direction. Other similar questions I have found have a more obvious id path that could be used in comparison to my example, so I am slightly stuck.

Screenshot of html and website layout

The options I would like to select seem to be altered by the aria-activedescendant value, e.g. aria-activedescendant="react-select-3--option-5"

I have tried the following code but I get the fault in the title when trying this.

select_element = driver.find_element(By.CLASS_NAME,'Select-input')
select_object = Select(select_element)
select_object.select_by_visible_text('Author')

CodePudding user response:

You can use a chain of Keys.TAB commands or Keys.ARROW_DOWN to get your desired input. Use something like this:

from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains

el = driver.find_element(By.CLASS_NAME,'Select-input').click()

action = ActionChains(driver)

action.send_keys(Keys.ARROW_DOWN)
action.send_keys(Keys.ARROW_DOWN)
action.send_keys(Keys.ARROW_DOWN)
action.send_keys(Keys.ENTER)
action.perform()
  • Related