Selenium Python下拉菜单

时间:2018-01-31 22:08:48

标签: python selenium

我对硒有点新意。我正在尝试使用python中的selenium从下拉菜单中选择值,但是对于这个网站,它似乎不起作用。

关于如何做到的任何想法?

下拉列表的HTML代码:

<div id="dropdown-breakdown-select" class="drop-down drop-down--open">
   <div class="drop-down__selected drop-down__selected--open ">Geographical breakdown</div>
   <ul class="dropdown-list" tabindex="1" style="height: 246px; 
       display: block; overflow-y: hidden; outline: none;">
      <li style="font-weight: normal;">Geographical breakdown</li>
      <li style="font-weight: normal;">Rating</li>
      <li style="font-weight: normal;">Maturity</li>
      <li style="font-weight: normal;">Benchmark breakdown</li>
      <li style="font-weight: normal;">Risk currency</li>
      <li style="font-weight: normal;">Active currency risk</li>
      <li style="font-weight: normal;">Active duration risk</li>
   </ul>
</div>

下拉菜单截图:

enter image description here

从此页面下拉:Web link

2 个答案:

答案 0 :(得分:0)

Find element by css selector#breakdown-select-wrapper .drop-down__selected)并点击它。

之后,迭代lis#dropdown-breakdown-select li)和find the one you are looking for by text

最后,click就可以了。

答案 1 :(得分:0)

由于您的下拉列表是CSS下拉列表,而不是原生下拉列表,因此您无法使用Select类。

def choose_breakdown(option):

    // click to make option list visible
    driver
        .find_element_by_css_selector(
            "div#dropdown-breakdown-select > div.drop-down__selected"
        ).click()

    // choose option by given option text
    driver
        .find_element_by_xpath(
            "//div[@id='dropdown-breakdown-select']/ul/li[.='" + option + "']"
        ).click()