javascript - 无法使用 selenium web-driver python 从自动完成下拉列表中选择多个选项

标签 javascript python angular selenium selenium-webdriver

我正在尝试自动化用 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/

相关文章:

python - 编写一个小而灵活的 HTTP 客户端

angular - 为什么 Angular 2 使用装饰器?

javascript - azure AppInsights和第三方脚本CORS问题

python - 如何向一个输入变量添加不同的金额?

python - 无法在 Google App Engine HTML 模板中输出字典键和值

html - 是否可以为 <router-outlet> 中的内容定义默认包装器?

node.js - 在 Angular 中调用外部应用程序

javascript - 如果我的文档中有许多带有 src 的脚本到同一个 js 文件,会发生什么?

javascript - 如何从 jquery datepicker 计算年龄?

javascript - 用 Vue.js 2 观察数组