【问题标题】:Selenium Python : pick element from drop down in a spanSelenium Python:从下拉列表中选择元素
【发布时间】:2019-11-21 14:51:29
【问题描述】:

我对 Selenium 比较陌生,我正在开发一个网络浏览器自动化项目,其中一个操作是从下拉菜单中选择一个元素,请在 html 代码下方找到。

<span id="export_menu" class="ui-button drop-down export-menu" tabindex="0" role="application">
<span class="menu_text">Export</span>
<span class="drop-down-menu ui-icon ui-icon-triangle-1-s"></span>
<ul class="export-actions"><li><header>Export Report</header>
    <ul><li class="menu-action"><input type="button" value="CSV" class="button ui-button ui-widget ui-state-default ui-corner-all" id="export_csv" data-format="csv" role="button" aria-disabled="false"></li></ul>
    <ul><li class="menu-action"><input type="button" value="PDF" class="button ui-button ui-widget ui-state-default ui-corner-all" id="export_pdf" data-format="pdf" role="button" aria-disabled="false"></li></ul>
    <ul><li class="menu-action"><input type="button" value="Schedule Export" class="button ui-button ui-widget ui-state-default ui-corner-all" id="schedule" role="button" aria-disabled="false"></li></ul></li></ul>
</ul>
</span>

我在 Python 上尝试了以下操作,错误如下

driver.find_element_by_id("export_menu").click()
driver.find_element_by_id("export_csv").click()

selenium.common.exceptions.ElementNotInteractableException:消息:元素无法滚动到视图中

经过一些研究,我也尝试了以下操作,这只是 超时

WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="export_csv"]'))).click()

请求帮助!

【问题讨论】:

    标签: python selenium-webdriver xpath css-selectors webdriverwait


    【解决方案1】:

    要从下拉菜单中选择 value 作为 CSV 的元素,您必须为 element_to_be_clickable() 引入 WebDriverWait 和您可以使用以下任一解决方案:

    • 使用CSS_SELECTOR

      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "span.ui-button.drop-down.export-menu#export_menu"))).click()
      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "ul.export-actions li.menu-action > input.button.ui-button.ui-widget.ui-state-default.ui-corner-all#export_csv"))).click()
      
    • 使用XPATH

      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[@class='ui-button drop-down export-menu' and @id='export_menu']"))).click()
      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//ul[@class='export-actions']//li[@class='menu-action']/input[@class='button ui-button ui-widget ui-state-default ui-corner-all' and @id='export_csv']"))).click()
      
    • 注意:您必须添加以下导入:

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

    您可以在How to select an option from a dropdown of non select tag?找到相关讨论

    【讨论】:

      【解决方案2】:

      尝试先点击&lt;ul&gt;

      WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH, '//*[@class="export-actions"]'))).click()
      
      WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="export_csv"]'))).click()
      

      【讨论】:

      • @Moshe,谢谢,由于某种原因,这似乎对我不起作用,一段时间后它会超时
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-05-25
      • 2021-04-28
      • 1970-01-01
      • 1970-01-01
      • 2020-12-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多