【问题标题】:What is the correct import statement to use for a Select object in webdriver 2.4 under python?在 python 下的 webdriver 2.4 中,用于 Select 对象的正确导入语句是什么?
【发布时间】:2011-11-27 21:54:45
【问题描述】:

我正在 python 2.7 上使用 selenium webdriver 2.4 编写测试。

文档 (http://seleniumhq.org/docs/03_webdriver.html) 演示了操作选择表单元素的能力,如下所示:

Select select = new Select(driver.findElement(By.xpath("//select")));
select.deselectAll();
select.selectByVisibleText("Edam");

我需要像这样在 python 中操作选择表单元素。但是我不知道要导入什么才能成功实例化 Select 对象。

我的导入语句应该是什么?

谢谢。

【问题讨论】:

  • 这不是python。您确定您正在阅读正确的文档吗?

标签: python testing selenium webdriver


【解决方案1】:

但是我不知道要导入什么才能成功实例化 Select 对象。

它可以作为: from selenium.webdriver.support.ui import Select

另请参阅:http://selenium.googlecode.com/svn/trunk/docs/api/py/webdriver_support/selenium.webdriver.support.select.html#module-selenium.webdriver.support.select

【讨论】:

    【解决方案2】:

    自从我发布这个问题以来,我花了很多时间搜索与 java Select() 对象等效的 python,但一无所获。

    我想出了一个基于此的解决方法:https://gist.github.com/1205069

    也许下面的代码可以帮助某人节省一些时间。

    from selenium import webdriver
    from selenium.common.exceptions import NoSuchElementException
    def select_by_text(web_element, select_text):
        """given a web element representing a select object, click the option 
        matching select_text
        """
        option_is_found = False
        options = web_element.find_elements_by_tag_name('option')
        for option in options:
            if option.text.strip() == select_text:
                option.click()
                option_is_found = True
                break
    
        if option_is_found == False:
            raise NoSuchElementException('could not find the requested element')
    
    # ...omitted setting up the driver and getting the page 
    web_element = webdriver.find_element_by_name('country_select')
    select_by_text(web_element, 'Canada')
    

    此代码应单击给定文本的选择元素,如果给定元素不是选择表单元素或文本不存在,则引发 NoSuchElementException 异常。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-05-29
      • 1970-01-01
      • 2016-04-09
      • 1970-01-01
      • 2020-08-05
      • 1970-01-01
      • 2013-01-24
      • 2018-12-09
      相关资源
      最近更新 更多