我正在尝试自动化一个用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来编写对角度的自动化测试)。请帮忙。

最佳答案

使用以下代码:

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

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


希望能帮助到你!!!

10-04 23:30
查看更多