我正在尝试自动化用 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 来编写 Angular 自动化测试)。请帮忙。
最佳答案
使用以下代码:
如果选择选项之一后自动完成下拉列表关闭:
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
希望对你有帮助!!!
关于javascript - 无法使用 selenium web-driver python 从自动完成下拉列表中选择多个选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55008438/