如何在python的硒菜单中单击带有按钮的按钮?

时间:2020-01-03 10:06:33

标签: python selenium xpath css-selectors webdriverwait

我有一个网页,里面有一个看起来像这样的菜单:

<span localization="" data-key="EXPORT_AND_IMPORT" class="ng-binding ng-isolate-scope">Export &amp; Import</span>

当我单击它时,它会打开一个带有三个选项的子菜单:

<div class="qmenu dropdown-menu positioned" style="top: 293px; left: 900px; max-height: 99%; width: 231px; height: 113px;"><ul class="menu-items"><li class="no-check"><a><i class="icon icon-download-lg"></i>Export Data...</a></li><li class="no-check"><a><i class="icon icon-uploadcsv-lg"></i>Import Data...</a></li><li class="divider"></li><li class="no-check"><a><i class="icon icon-twofiles"></i>Manage Previous Downloads...</a></li></ul></div>

这是一张图片:

enter image description here

如何在子菜单中按下导出和导入按钮,然后按下导出数据按钮?

1 个答案:

答案 0 :(得分:0)

首先在文本为导出和导入的元素上click(),然后在文本为导出数据...的子菜单元素上click()。 / strong>,您必须为element_to_be_clickable()引入 WebDriverWait ,并且可以使用以下Locator Strategies中的任何一个:

  • 使用CSS_SELECTOR

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "span.ng-binding.ng-isolate-scope[data-key='EXPORT_AND_IMPORT']"))).click()
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.qmenu.dropdown-menu>ul.menu-items li:nth-of-type(1)>a"))).click()
    
  • 使用XPATH

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[@class='ng-binding ng-isolate-scope' and @data-key='EXPORT_AND_IMPORT']"))).click()
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='qmenu dropdown-menu positioned']/ul[@class='menu-items']//li/a[contains(., 'Export Data')]"))).click()
    
  • 注意:您必须添加以下导入:

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC