[Answer]-Selenium in DJango Click failing on dropdown menu

1👍

You should use find_element_by_css_selector(), since you’re looking for a child element:

self.driver.find_element_by_css_selector("#id_project > option:nth-child(3)")

find_element_by_id() doesn’t accept the # selector syntax, or child elements.

EDIT: You’re really better off selecting things this way:

el = self.driver.find_element_by_id('id_project')
for option in el.find_elements_by_tag_name('option'):
    if option.text == 'Dummy project2':
        option.click()

However, the following also works and has been tested on my machine, without first clicking id_project. I simply deleted the line before it, and replaced it with:

self.driver.find_element_by_css_selector('#id_project > option:nth-child(3)').click()

Leave a comment