我正在使用 Selenium Web 驱动程序来自动化 Angular 网站。问题是该网站有 3 个下拉菜单,1 个代表国家/地区,1 个代表省份,1 个代表城市。
\n\n我知道我无法使用 Select 类,因此我要做的第一件事是单击 mat-select 元素,然后单击我想要的 mat-option。
\n\n问题是,有时当我运行测试时,mat-option 的 id、xpath 或 cssSelector 是不同的。
\n\n如何保证始终访问相同的 mat-option?
\n\n用于垫选择的 HTML
\n\n<mat-select _ngcontent-c16="" class="mat-select ng-tns-c19-42 ng-pristine ng-valid ng-star-inserted ng-touched" formcontrolname="country" placeholder="Seleccione un pa\xc3\xads" role="listbox" id="mat-select-5" tabindex="0" aria-label="Seleccione un pa\xc3\xads" aria-required="false" aria-disabled="false" aria-invalid="false" aria-multiselectable="false">\n <div class="mat-select-trigger" aria-hidden="true" cdk-overlay-origin="">\n <div class="mat-select-value">\n <!----><span class="mat-select-placeholder ng-tns-c19-42 ng-star-inserted">Seleccione un pa\xc3\xads</span>\n <!---->\n </div>\n <div class="mat-select-arrow-wrapper">\n <div class="mat-select-arrow"></div>\n </div>\n </div>\n <!---->\n</mat-select>\nRun Code Online (Sandbox Code Playgroud)\n\n单击垫子选择后,它会变成这样
\n\n<mat-select _ngcontent-c16="" …Run Code Online (Sandbox Code Playgroud)