【问题标题】:Can I get options of multi-select with selenium+python我可以使用 selenium+python 获得多选选项吗
【发布时间】:2019-12-12 08:32:51
【问题描述】:

经过测试的 HTML:

<select>
    <option value="html">html</option>
    <option value="css">css</option>
    <option value="JavaScript">JavaScript</option>
    <option value="php">php</option>
</select>
  1. selenium.webdriver.support.select.Select(webelement) 类下没有类似isMultiple() 的方法,也没有select_all()method

  2. 当我一一选择这4个选项时

Select(lang).select_by_visible_text("html")
Select(lang).select_by_visible_text("css")
Select(lang).select_by_visible_text("JavaScript")
Select(lang).select_by_visible_text("php")

然后尝试获取所有选定的选项

Select(lang).all_selected_options

我只能得到最后一个选项'php',这意味着当我选择一个选项时,另一个选项会自动取消选择。 all_selected_options是什么意思,options够用了。而且我无法取消选择任何选项,因为只选择了一个,报告错误:

NotImplementedError: You may only deselect options of a multi-select

【问题讨论】:

  • 这个下拉菜单似乎不支持多选。您可以手动选择多个选项吗?
  • 当然不是。那么all_selected_optionsdeselect_ 方法都没有用?
  • 这个下拉菜单不支持多选,并不代表所有的下拉菜单都不支持。

标签: python selenium selenium-webdriver


【解决方案1】:

如果你想在 python 中使用 selenium 选择多个选项,你可以随时使用ActionChains 链接一系列动作,我们需要以下动作:

  1. CTRL
  2. Click 选项
  3. 释放CTRL

这是在 python 中使用 ActionChains 的一个很好的例子

列出您要在 python 中选择的选项,遍历列表并使用xpath 选择包含text 的选项,然后使用ActionChains 使用上面定义的一系列操作选择选项。

# Text of options needed to select
options = ['html','css','php']

# Add path to your chrome drive
browser = webdriver.Chrome(executable_path="EXECUTABLE_PATH_HERE")

# Add url of website
browser.get("WEBSITE_URL_HERE")

for option in options:
  # Find option that contains text equal to option
  to_select = browser.find_element_by_xpath("//select/option[text()='"+option+"']")

  # Use ActionChains
  ActionChains(browser).key_down(Keys.CONTROL).click(to_select).key_up(Keys.CONTROL).perform()

  • ActionChains() 获取驱动程序的引用,在这种情况下为 browser
  • key_down()CONTROL 传递给它的键。
  • click() 单击使用xpath 选择的传递选项。
  • key_up()释放CONTROL

希望对你有很大帮助。

【讨论】:

    【解决方案2】:

    此下拉菜单不支持多选,此类下拉菜单将具有multiple 属性

    <select multiple="">
      <option value="volvo">Volvo</option>
      <option value="saab">Saab</option>
      <option value="opel">Opel</option>
      <option value="audi">Audi</option>
    </select>
    

    没有is_multiple()函数,但是有一个变量is_multiple。它是通过检查multiple 属性在Select __init__ 中创建的

    def __init__(self, webelement):
        self._el = webelement
        multi = self._el.get_attribute("multiple")
        self.is_multiple = multi and multi != "false"
    

    您可以使用Select 实例访问它

    Select(element).is_multiple
    

    要获取所有下拉选项,无论它们是否被选中,请使用option 属性,这会将所有选项返回为WebElement 列表

    options = Select(element).options
    for option in options:
        print option.text # html, css, ...
    

    【讨论】:

    【解决方案3】:

    根据您共享的 HTML:

    <select>
        <option value="html">html</option>
        <option value="css">css</option>
        <option value="JavaScript">JavaScript</option>
        <option value="php">php</option>
    </select>
    

    &lt;select&gt; 标签没有 multiple 属性。所以可能不是


    要从您可以使用的&lt;option&gt; 标签中提取文本,您可以使用以下任一Locator Strategies

    • 使用tag_name

      select_technology = Select(driver.find_element_by_tag_name('select'))
      for option in select_technology.options:
          print(option.text)  
      
    • 使用xpath

      select_technology = Select(driver.find_element_by_xpath(//select))
      for option in select_technology.options:
          print(option.text)  
      
    • 注意:您必须添加以下导入:

      from selenium import webdriver
      from selenium.webdriver.support.select import Select
      

    【讨论】:

      猜你喜欢
      • 2015-09-01
      • 2020-09-10
      • 1970-01-01
      • 1970-01-01
      • 2012-08-09
      • 1970-01-01
      • 2021-12-27
      • 1970-01-01
      相关资源
      最近更新 更多