Django selenium在<select> </select>中选择表单选项

时间:2012-11-06 23:10:55

标签: django-testing

如何在Django中使用selenium来选择并选择表单的<select>标记中的选项?

这是我走了多远:

def setUp(self):
    self.browser = webdriver.Firefox()

def tearDown(self):
    self.browser.quit()

def test_project_info_form(self):
    # set url
    self.browser.get(self.live_server_url + '/tool/project_info/')
    # get module select
    my_select = self.browser.find_element_by_name('my_select')
    #! select an option, say the first option !#
    ...

1 个答案:

答案 0 :(得分:0)

所以这篇文章非常有用:

https://sqa.stackexchange.com/questions/1355/what-is-the-correct-way-to-select-an-option-using-seleniums-python-webdriver

基本上我必须直接将<select><option>定位到xpath,然后点击一下事件:

self.browser.find_element_by_xpath(
        "//select[@id='my_select_id']/option[text()='my_option_text']"
    ).click()

或者我可以针对索引:

self.browser.find_element_by_xpath(
        "//select[@id='my_select_id']/option[2]"
    ).click()

我希望这对有类似问题的人有所帮助。