【问题标题】:How to select dropdown menu in python using Selenium如何使用 Selenium 在 python 中选择下拉菜单
【发布时间】:2019-02-01 15:52:03
【问题描述】:

我想知道如何从网站的下拉列表中选择选项。

html 在这里

<thead>
    <td style="width: 40%;">
        <select name="product_size" id="sct-size" data-md-selectize>
            <option value="-">Choose Size</option>
                                                    <option value="323">XS</option>
                                                    <option value="324">S</option>
                                                    <option value="325">M</option>
                                                    <option value="326">L</option>
                                                    <option value="327">XL</option>
                                                    <option value="328">XXL</option>
                                                    <option value="342">1 years old</option>
                                                    <option value="343">5 years old</option>
                                                    <option value="344">8 years old</option>
                                                    <option value="345">12 years old</option>
                                                </select>
    </td>
    <td style="width: 40%;">
        <select name="product_color" id="sct-color" data-md-selectize>
            <option value="-">Choose Color</option>
                                                    <option value="594">N/A</option>
                                                </select>
    </td>
    <td style="width: 19%;"><input type="text" class="md-input" name="product_stock_" id="inp-stock" placeholder="Stock" style="margin-top: -11px;text-align: center;"/></td>
    <td style="width: 1%;"><a href="#" id="btn-addstock" style="margin-top: 5px; display: block;" title="Add Stock"><i class="material-icons">&#xE148;</i></a></td>
</thead>

我想选择“选择尺寸”,然后选择“S”选项。我试过这段代码。

from selenium import webdriver
from selenium.webdriver.support.select import Select

mySelect = Select(driver.find_element_by_id("sct-size"))
mySelect.select_by_visible_text("S")

但是出错了

NoSuchElementException:消息:找不到具有可见文本的元素:S

我已经在 stackoverflow 上查看了很多关于这个问题的解决方案。它假设从下拉菜单中选择“S”,但没有。我不知道我还能尝试什么。

谢谢。

已经试过了

  1. https://sqa.stackexchange.com/questions/12029/how-do-i-work-with-dropdowns-in-selenium-webdriver
  2. Select a dropdown using Python + Selenium

已编辑 1

我也试过

mySelect = Select(driver.find_element_by_id("sct-size"))
mySelect.select_by_value("323")

但出现此错误

NoSuchElementException:消息:找不到具有值的选项:323

我还尝试用名称更改 find_element 方法

obj = Select(driver.find_element_by_name('product_size'))
obj.select_by_index(1)

出现错误

NoSuchElementException:消息:找不到索引为 1 的选项

也许 find_element 是问题所在?我不知道。

编辑 2

我厌倦了在谷歌浏览器中使用“检查”工具,发现当页面加载时,html 仅加载 1 个选项(在本例中为“选择大小”)。这解释了错误的原因。

页面加载时的html代码。

<thead>
<td style="width: 40%;">
    <select name="product_size" id="sct-size" data-md-selectize>
        <option value="-">Choose Size</option>                                                    
</td>
<td style="width: 40%;">
    <select name="product_color" id="sct-color" data-md-selectize>
        <option value="-">Choose Color</option>
                                                
                                            </select>
</td>
<td style="width: 19%;"><input type="text" class="md-input" name="product_stock_" id="inp-stock" placeholder="Stock" style="margin-top: -11px;text-align: center;"/></td>
<td style="width: 1%;"><a href="#" id="btn-addstock" style="margin-top: 5px; display: block;" title="Add Stock"><i class="material-icons">&#xE148;</i></a></td>
</thead>

尝试了 Seema Nair 解决方案,但出现了其他错误

AttributeError: 'Select' 对象没有属性 'click'

我的新代码是

mySelect = Select(driver.find_element_by_id("sct-size")) 
mySelect.click() 
mySelect.select_by_visible_text("S")

那么,新问题,如何点击下拉菜单让选项代码显示?

【问题讨论】:

    标签: python selenium-chromedriver


    【解决方案1】:

    您通过可见文本或值选择的原始代码将起作用。

    我遇到了类似的问题,这是因为在从下拉列表中选择值之前,我必须单击下拉链接然后执行选择。

    所以在 Select 之前添加一个 click 方法,你最初的代码就可以工作了。

    【讨论】:

    • 所以,我尝试了但又遇到了另一个错误。 AttributeError:“选择”对象没有属性“点击”..
    • mySelect = Select(driver.find_element_by_id("sct-size")) mySelect.click() mySelect.select_by_visible_text("S")
    • 我根据您对“已编辑 2”的建议更新我的问题
    • 试着调整我的代码,你就对了。非常感谢。
    • 我很高兴,我能帮上忙。祝你好运!
    猜你喜欢
    • 2022-07-08
    • 2023-01-19
    • 2020-11-22
    • 2023-03-25
    • 2022-11-11
    • 1970-01-01
    • 2016-07-28
    • 1970-01-01
    • 2011-12-13
    相关资源
    最近更新 更多