使用Selenium Python单击非选择下拉选项

jjhzyzn0  于 2023-02-28  发布在  Python
关注(0)|答案(1)|浏览(159)

我正在尝试自动执行一些任务,如下载文件和预处理它们。要下载其中一个文件,我需要指定日期(由于隐私问题,我不能共享网站),但我无法在Dec-2022和Jan-2023之间进行选择,如下所示:
下面是HTML代码:

<button type="button" ng-click="clickDrop($event)" ng-show="showButton()" data-wt="toggle">
         <span class="ng-binding">Current Month</span>
         <span ng-hide="dropOpen" class="caret ng-hide"></span>
         <span ng-show="dropOpen" class="dropup"><span class="caret"></span></span>
     </button>
 <div class="rpt-dext-select-dropdown-options" data-wt="dropdown" ng-show="isEditable()">
 <div ng-repeat="option in options|filter:{label:searchFilter}" ng-click="selectOption(option,$event)" class="rpt-dext-select-dropdown-checkable ng-scope" data-wt="202301">   <span ng-class="{'rpt-dext-select-dropdown-checked':selections.includes(option)}" class="ng-binding">Jan 2023</span>
                <span class="fas fa-check check-mark ng-hide" ng-show="selections.includes(option)"></span>
             </div>
 <div ng-repeat="option in options|filter:{label:searchFilter}" ng-click="selectOption(option,$event)" class="rpt-dext-select-dropdown-checkable ng-scope" data-wt="202212">   <span ng-class="{'rpt-dext-select-dropdown-checked':selections.includes(option)}" class="ng-binding">Dec 2022</span>
                <span class="fas fa-check check-mark ng-hide" ng-show="selections.includes(option)"></span>
             </div>

我可以打开菜单:

WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, '.ng-isolate-scope:nth-child(3) .ng-isolate-scope~ .ng-isolate-scope button .ng-binding'))).click()

但是不能使用click()进行选择,我认为select()不起作用。

enyaitl3

enyaitl31#

使用以下代码行打开菜单后:

WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, '.ng-isolate-scope:nth-child(3) .ng-isolate-scope~ .ng-isolate-scope button .ng-binding'))).click()

要单击某个选项,您需要为element_to_be_clickable()引入WebDriverWait,您可以使用以下locator strategies

  • 点击 * 2022年12月 *:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='rpt-dext-select-dropdown-options']//span[@class='ng-binding' and text()='Dec 2022']"))).click()
  • 点击 * 2023年1月 *:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='rpt-dext-select-dropdown-options']//span[@class='ng-binding' and text()='Jan 2023']"))).click()

相关问题