【问题标题】:Not able to select drop-down option using select : UnexpectedTagNameException selenium : python无法使用 select 选择下拉选项:UnexpectedTagNameException selenium:python
【发布时间】:2014-12-30 21:44:33
【问题描述】:

如果我犯了非常基本的错误,我深表歉意,我仍然是学习 webdriver 和 python 的新手。

我正在尝试使用 Python 的 Select Class 从下拉列表中选择一个选项,但我得到了 Unexpected TagName 异常。 标签上没有 ID,因此我无法使用除类或属性之外的任何其他方法检测到它。

HTML 代码:

<div class="select-pixels-container ng-scope">
    <div class="action group">
    <table class="org-agency-advertiser">
    <tbody style="background-color: transparent;">
        <tr style="background-color: transparent;">
        <tr>
            <td style="background-color: transparent;">
                <select class="mm-select ng-valid localytics-chosen ng-dirty" ng-disabled="disabled('organizations')" ng-options="org.id as org.name for org in logic.organizations | orderBy:'name'" ng-model="logic.organization" chosen="" style="display: none;" data-placeholder="Select an Option">
                <div class="chosen-container chosen-container-single" style="width: 175px; background-color: transparent;" title="">
                    <a class="chosen-single" tabindex="-1" style="background-color: transparent;">
                        <span style="background-color: transparent;">Select</span>
                        <div>
                    </a>
                    <div class="chosen-drop">
                        <div class="chosen-search">
                            <input type="text" autocomplete="off">
                        </div>
                        <ul class="chosen-results">
                            <li class="active-result" style="" data-option-array-index="0">Select</li>
                            <li class="active-result" style="" data-option-array-index="1">1-800 Hnunagk 100278</li>
                            <li class="active-result result-selected" style="" data-option-array-index="2">10tc Tatgaa 100179</li>

我为此部分编写的代码是:

self.driver.find_element_by_css_selector("tbody>tr>td>div>a.chosen-single").click()
select=Select(self.driver.find_element_by_css_selector("tbody>tr>td>div>a[tabindex='-1']"))
select.select_by_index(0)

我得到的例外是

selenium.common.exceptions.UnexpectedTagNameException: Message: Select only works on <select> elements, not on <a>`

任何帮助将不胜感激。

【问题讨论】:

  • 是否可以分享您正在使用的网站的 URL?
  • 不幸的是:不,因为它在我们的网络之外无法访问
  • 首先,除了select 元素之外,您不能将任何内容传递给Select() 构造函数。另外,正如我所见,select 本身是隐藏的,这可能会使事情变得更加困难。我认为您需要手动找到代表选项的元素并单击它们。
  • @rayonst:它们不是精确的select 标签,因此您得到了例外。需要手动遍历dom,做必要的操作。

标签: python selenium python-3.x selenium-webdriver


【解决方案1】:

所有的 HTML 代码都没有共享,但基本思路应该是这样的:

self.driver.find_element_by_xpath('//*[@class="chosen-single"]').click()
listItems = self.driver.find_element_by_xpath('//div[@class="chosen-drop"]/ul/li')

for item in listItems: # <li> tags list
    if item.get_attribute("data-option-array-index") == "0": # select index 
        item.click() # if index=0 click
        break

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-10-30
    • 1970-01-01
    • 2019-07-29
    • 1970-01-01
    • 2018-12-28
    • 1970-01-01
    • 1970-01-01
    • 2020-01-10
    相关资源
    最近更新 更多