You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Angular网站中Python Selenium无法处理自动建议下拉框问题求助

Angular网站Selenium自动化:自动建议下拉框操作失败

我尝试使用Python-Selenium自动化基于Angular搭建的网站工作流,但无法实现自动建议下拉框的自动化操作。

当我使用send_keys方法输入关键词,代码如下:

competence_input = driver.find_element(By.XPATH, "//input[@formcontrolname='Competencies']")
competence_input.send_keys('java')

执行上述操作后会生成一个包含多个选项的容器,我需要从中选择一项。对应的DOM结构如下:

<div class="cdk-overlay-container">
<div class="cdk-overlay-connected-position-bounding-box" dir="ltr" style="top: 0px; left: 0px; height: 100%; width: 100%;"><div id="cdk-overlay-6" class="cdk-overlay-pane" style="width: 295.555px; top: 279.07px; left: 463.195px;">
<div role="listbox" class="mat-autocomplete-panel ng-star-inserted mat-autocomplete-visible" id="mat-autocomplete-1">

<mat-option _ngcontent-edi-c192="" role="option" class="mat-option mat-focus-indicator ng-star-inserted" style="font-size: small; opacity: 60%;" id="mat-option-136" tabindex="0" aria-disabled="false"><!----><span class="mat-option-text"> Java Web Technologies </span><!----><div mat-ripple="" class="mat-ripple mat-option-ripple"></div></mat-option>

<mat-option _ngcontent-edi-c192="" role="option" class="mat-option mat-focus-indicator ng-star-inserted" style="font-size: small; opacity: 60%;" id="mat-option-137" tabindex="0" aria-disabled="false"><!----><span class="mat-option-text"> Java - JMX </span><!----><div mat-ripple="" class="mat-ripple mat-option-ripple"></div></mat-option>
<!---->
</div>
</div>
</div>
</div>

我尝试的代码如下:

try:
    elements = WebDriverWait(driver, 30)
    elements.until(
        expected_conditions.visibility_of_element_located((By.XPATH, "//div[starts-with(@id,'mat-autocomplete-']")))

    competences = driver.find_elements(By.XPATH,"//mat-option[@role='option']//span")
    print(len(competences))

    for item in competences:
        print('options are', item.text)
        if item.text == 'Java Web Technologies':
            item.click()
            break
except:
    print('Item was not found or Dropdown did not appear')

恳请各位提供解决该问题的思路,非常感谢!

内容的提问来源于stack exchange,提问作者Harish M

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.01 00:22:21