【问题标题】:Unable to select multiple options from autocomplete drop-down using selenium web-driver python无法使用 selenium web-driver python 从自动完成下拉列表中选择多个选项
【发布时间】:2019-03-06 03:10:51
【问题描述】:

我正在尝试自动化使用 Angular 编写的网页,并且我有自动完成下拉菜单,其中包含很多元素。我正在尝试单击每个元素并检查它是否填充了下面的所有字段。 这是该下拉菜单的 innerHTML

<div class="mat-autocomplete-panel mat-autocomplete-visible" role="listbox" id="mat-autocomplete-0">
<!---->
<mat-option _ngcontent-c3="" class="mat-option" role="option" tabindex="0" id="mat-option-67" aria-selected="false" aria-disabled="false">
    <!---->
    <span class="mat-option-text">  Miss </span>
    <div class="mat-option-ripple mat-ripple" mat-ripple=""></div>
</mat-option>
<mat-option _ngcontent-c3="" class="mat-option" role="option" tabindex="0" id="mat-option-68" aria-selected="false" aria-disabled="false">
    <!---->
    <span class="mat-option-text">  SLCA </span>
    <div class="mat-option-ripple mat-ripple" mat-ripple=""></div>
    </mat-option>
<mat-option _ngcontent-c3="" class="mat-option mat-selected" role="option" tabindex="0" id="mat-option-21" aria-selected="true" aria-disabled="false">

我尝试使用 select,但它给出的错误提示应该是 select 而不是 div。那么有没有办法解决这个问题,或者我是否必须切换另一种语言(例如 JS 来编写自动化的角度测试)。请帮忙。

【问题讨论】:

    标签: javascript python angular selenium selenium-webdriver


    【解决方案1】:

    使用以下代码:

    如果选择以下选项之一后自动完成下拉菜单关闭:

    driver.find_element_by_xpath("//div[@id='mat-autocomplete-0']").click()
    
    all_options = driver.find_elements_by_xpath("//span[@class='mat-option-text']")
    i = 0
    while i<len(all_options) : 
        driver.find_element_by_xpath("//div[@id='mat-autocomplete-0']").click()
        driver.find_elements_by_xpath("//span[@class='mat-option-text']")[i].click()
        i=i+1
    

    如果选择以下选项之一后自动完成下拉菜单保持不变:

    driver.find_element_by_xpath("//div[@id='mat-autocomplete-0']").click()    
    
    all_options = driver.find_elements_by_xpath("//span[@class='mat-option-text']")
    i = 0
    while i<len(all_options) : 
        driver.find_elements_by_xpath("//span[@class='mat-option-text']")[i].click()
        i=i+1
    

    希望对你有帮助!!!

    【讨论】:

    • 您好,它给了我一个 StaleElementReferenceException 并说该元素未附加到页面文档
    • @Girishvenkata :选择选项时下拉菜单是否关闭,如果即使在选择后仍保持打开状态,则不需要 - driver.find_element_by_xpath("//div[@id='mat-autocomplete -0']").click() ,所以删除它并运行代码
    • 是的,我们选择一个选项后下拉菜单关闭,感谢您的代码确实有效。
    猜你喜欢
    • 2018-01-18
    • 1970-01-01
    • 1970-01-01
    • 2015-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-04
    • 2021-11-05
    相关资源
    最近更新 更多