使用Selenium + Python单击下拉项

时间:2018-02-07 11:16:43

标签: python selenium web-scraping

如何使用Selenium + Python点击此下拉菜单中的项目?

<div class="form-group pull-right">
      <select name="per_page" class="form-control" onchange="this.form.submit()">
         <!-- <option value="5" >5</option> -->
         <option value="10">10</option>
         <option value="20">25</option>
         <option value="50">50</option>
         <option value="100" selected="selected">100</option>
      </select>
</div>

这是我的代码:

select_obj = Select(driver.find_element_by_class_name('form-control'))
select_obj.select_by_visible_text('100')

我收到此错误:

Message: Select only works on <select> elements, not on <input>

1 个答案:

答案 0 :(得分:0)

使用&#34;名称&#34;属性而不是&#34; class&#34;属性:

select_obj = Select(driver.find_element_by_name("per_page"))
select_obj.select_by_visible_text("100")

希望它可以帮到你!