How can I click on multiple drop down list on a page with Python and Selenium?

时间:2015-12-14 18:14:06

标签: javascript python selenium

I'm trying to click on multiple dropdown lists on a page but I keep getting an error saying that my list object has no attribute tag_name'.

My code

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()

My traceback

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'

The Html dropdown

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

2 个答案:

答案 0 :(得分:1)

首先,您正在使用cordova plugin add com.blackberry.app 方法,该方法会返回与类名称匹配的 Web元素列表,而不是单个元素。

但是,即使您使用find_elements_by_class_name()代替,您也会收到不同的错误,因为这是一个匹配类名而不是find_element_by_class_name()元素的div元素。

答案 1 :(得分:0)

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

  

构造。检查给定元素确实是a   SELECT标签。如果不是,那么就是UnexpectedTagNameException   抛出。