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
相关产品推荐
相关产品推荐

