繁体   English   中英

无法使用Selenium Web驱动程序python从自动完成下拉列表中选择多个选项

[英]Unable to select multiple options from autocomplete drop-down using selenium web-driver python

我正在尝试自动化一个用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 not div 那么有没有解决的办法,还是我必须切换另一种语言(例如JS来编写对角度的自动化测试)。 请帮忙。

使用以下代码:

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

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

希望能帮助到你!!!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM