【问题标题】:Select an element inside a p-dropdown tag using Selenium and Python使用 Selenium 和 Python 在 p-dropdown 标签内选择一个元素
【发布时间】:2021-07-31 00:40:44
【问题描述】:

我正在尝试使用 Selenium 和 Python 在 p-dropdown 上选择 200 选项,此函数在没有消息的情况下出现超时异常...

Selection = Select(WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "//div[contains(@class,'ui-dropdown-') and @aria-label='200']"))))
Selection.select_by_value("200")

下拉菜单如下所示:

<div class="ui-dropdown-items-wrapper" style="max-height: 200px;">
    <ul class="ui-dropdown-items ui-dropdown-list ui-widget-content ui-widget ui-corner-all ui-helper-reset" role="listbox">
        <p-dropdownitem class="ng-tns-c10-0 ng-star-inserted" style="">
            <li role="option" class="ui-dropdown-item ui-corner-all ui-state-highlight" aria-label="25">
                <span class="ng-star-inserted">25</span>
            </li>
        </p-dropdownitem>
        <p-dropdownitem class="ng-tns-c10-0 ng-star-inserted" style="">
            <li role="option" class="ui-dropdown-item ui-corner-all" aria-label="50">
                <span class="ng-star-inserted">50</span>
            </li>
        </p-dropdownitem>
        <p-dropdownitem class="ng-tns-c10-0 ng-star-inserted" style="">
            <li role="option" class="ui-dropdown-item ui-corner-all" aria-label="100">
                <span class="ng-star-inserted">100</span>
            </li>
        </p-dropdownitem>
        <p-dropdownitem class="ng-tns-c10-0 ng-star-inserted" style="">
            <li role="option" class="ui-dropdown-item ui-corner-all" aria-label="200">
                <span class="ng-star-inserted">200</span>
            </li>
        </p-dropdownitem>
    </ul>
</div>

任何人都知道或可以告诉我如何选择 200 选项以在一页中显示所有寄存器并避免初始分页?

提前致谢。

【问题讨论】:

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


    【解决方案1】:

    如果元素在 select 标记中,则 Select() 有效。

    收集p 元素并迭代以找到正确的选项。

        options = driver.find_elements_by_xpath(".../p")
        for o in options:
        ...
    

    或者点击下拉菜单并点击所需的选项。

    【讨论】:

      【解决方案2】:

      如果你将此元素传递给Select,这不是Select 类型,它将在内部抛出异常UnexpectedTagNameException,它的构造函数执行检查给定元素是否确实是一个SELECT 标记。如果不是,则抛出UnexpectedTagNameException

      如果您可以检查以下 XPath,请根据提供的 HTML 源代码

      ((//div[@class='ui-dropdown-items-wrapper']//li)[4])
      
       element = driver.find_element_by_xpath("((//div[@class='ui-dropdown-items-wrapper']//li)[4])")
      
      print(element.get_attribute("aria-label")
      
      you can enhance the code as per your requirement
      

      【讨论】:

      • 感谢您的回复,我已经尝试了您的代码,但抛出错误消息:无法找到元素:{“method”:“xpath”,“selector”:“((//div [ @class='ui-dropdown-items-wrapper']//li)[4])"} 也许可以用不同的方式找到元素?
      • @efe 好的,你能分享一下这个网址吗,可能会有帮助
      • 感谢您的回复,不幸的是,这是来自 Vuforia 门户 (developer.vuforia.com) 的私人区域,您需要一个帐户才能访问它。也许我可以复制并粘贴 Chrome 控制台上显示的代码?否则我不知道如何正确分享。
      • 我想我知道问题出在哪里...当您进入页面时,最初的 HTML 代码并未显示分页组合内的所有值。它只是显示这个:`
        `我想我会尝试一种不同的方式,例如尝试使用 EC.element_to_be_clickable 对单击“按日期排序”跨度的表的值进行排序
      猜你喜欢
      • 1970-01-01
      • 2018-09-15
      • 1970-01-01
      • 2019-03-10
      • 1970-01-01
      • 2021-12-08
      • 2020-10-02
      • 2020-07-29
      • 2023-03-09
      相关资源
      最近更新 更多