【问题标题】:Django selenium select form options in <select><select> 中的 Django selenium 选择表单选项
【发布时间】:2012-11-06 23:10:55
【问题描述】:

在 Django 中如何使用 selenium 在表单的&lt;select&gt; 标签中选择选项?

这是我走了多远:

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 !#
    ...

【问题讨论】:

    标签: django-testing


    【解决方案1】:

    所以这篇文章非常有用:

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

    基本上我必须通过xpath 直接定位&lt;select&gt;&lt;option&gt;,然后是点击事件:

    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()
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-08-10
      • 1970-01-01
      • 2016-03-29
      • 2011-05-28
      • 2015-09-02
      • 1970-01-01
      • 2011-12-17
      相关资源
      最近更新 更多