0

A bit stumped here. I am writing a Selenium test for a class project in Python/Django and cannot understand the behavior I am seeing. As part of the test I am clicking on a dropdown menu, then clicking on an option from that menu.

The initial click on the menu works. The menu opens, and then the second click does not. However it's not completely failing -- the click still generates a hover effect over the option in question.

I've done a fair bit of Selenium in the past, so I've tried alot of the standard debugging issues and nothing has worked yet. Among them I've tried double clicking, using Select, using a different selector etc with no luck. I'm going to goof around with ActionChains and SendKeys next, but would like to understand why simpler methods are not working. My code:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.select import Select
from django.contrib.auth.models import User
from django.test.testcases import LiveServerTestCase
from time import sleep
import os

class CreateIssueTestCase(LiveServerTestCase):
    def setUp(self):
        self.driver = webdriver.Firefox()
        user = User.objects.create_superuser(username="username", password="password", email="[email protected]")
        user.save()

    def tearDown(self):
        self.driver.quit()
        User.objects.all().delete()

    def test_create_issue(self):

        self.driver.get("localhost:8081/issue/create")
        self.driver.find_element_by_id("id_username").send_keys("username")
        self.driver.find_element_by_id("id_password").send_keys("password")
        self.driver.find_element_by_id("id_password").send_keys(Keys.ENTER)
        sleep(2)
        self.driver.find_element_by_id("id_project").click()
        sleep(2)
        self.driver.find_element_by_css_selector("#id_project > option:nth-child(3)").click()
        sleep(2)
        self.driver.find_element_by_id("id_issue_type").click()
        sleep(2)

The failed click is the second one in the test. There are additional dropdowns on this page that are also behaving the same way.

I'll note I am not seeing any error output from this test. Also, if I don't take further actions, the dropdown menu will remain open with the targeted option highlighted until the test ends.

Thanks,

UPDATE:

I've tried using

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

This also has not had the desired effect. Instead of selecting the element in question, it simply closes the drop down menu.

The relevant html as read from firefinder:

<select id="id_project" name="project">
  <option value selected="selected">----------</option>
  <option value="1">Dummy project1</option>
  <option value="2">Dummy project2</option>
</select>

1 Answer 1

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()
Sign up to request clarification or add additional context in comments.

5 Comments

apologies, I posted code mid-edit, the current version does use find by css_selector, and is still exhibiting the same error. Thank you for pointing out the issue with the posted code, however.
What does the HTML markup for that section of your document look like?
adding relevant html above
That did the trick. Thanks! Any insight as to why this does not work when I open the drop-down menu first? Just curious.
I'm not sure. I was able to reproduce the issue you explained. The dropdown opens and the third option is highlighted...but not actually selected. It may be a limitation of Selenium.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.