【问题标题】:How can I click on multiple drop down list on a page with Python and Selenium?如何使用 Python 和 Selenium 在页面上单击多个下拉列表?
【发布时间】:2015-12-14 18:14:05
【问题描述】:

我正在尝试单击页面上的多个下拉列表,但我不断收到错误消息,提示我的列表对象没有属性 tag_name'。

我的代码

def click_follow_buttons(driver):
    selects = Select(driver.find_elements_by_class_name("jBa"))#jBa
    print selects
    for select in selects:
        select.select_by_index(0)
        driver.find_element_by_class_name("bA").click()

我的回溯

Traceback (most recent call last):
  File "google_follow.py", line 50, in <module>
    if click_follow_buttons(driver) == False:
  File "google_follow.py", line 18, in click_follow_buttons
    selects = Select(driver.find_elements_by_class_name("jBa"))#jBa
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/support/select.py", line 35, in __init__
    if webelement.tag_name.lower() != "select":
AttributeError: 'list' object has no attribute 'tag_name'

HTML 下拉菜单

<div class="jBa XG">
<div class="ny dl d-k-l" jslog="7128; track:impression">

【问题讨论】:

    标签: javascript python selenium


    【解决方案1】:

    首先,您正在使用find_elements_by_class_name() 方法,该方法将返回一个网络元素列表,该列表与类名匹配,而不是单个元素。

    但是,即使您改用 find_element_by_class_name(),您也会得到不同的错误,因为这是匹配类名的 div 元素而不是 select 元素。

    【讨论】:

    • @alexce 我是第一个=P
    • @drets 很好,但答案有所不同:)
    【解决方案2】:

    您需要传递给Select 具有select 标签名称的类Web 元素的构造函数: https://selenium.googlecode.com/git/docs/api/py/webdriver_support/selenium.webdriver.support.select.html

    构造函数。检查给定元素是否确实是 选择标签。如果不是,则 UnexpectedTagNameException 是 扔了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-02-11
      • 2015-06-13
      • 2018-08-02
      • 2013-08-05
      • 2019-03-10
      • 1970-01-01
      • 2020-01-24
      相关资源
      最近更新 更多