【问题标题】:Selenium can't find element (timeout exception) by xpath or ID with explicit waitSelenium 无法通过显式等待的 xpath 或 ID 找到元素(超时异常)
【发布时间】:2020-08-27 01:58:44
【问题描述】:

我正在尝试从this 网站下载 csv,方法是使用 selenium 使用以下代码从 Python 中单击“下载数据”按钮:

driver = webdriver.Chrome(executable_path=r'/usr/local/bin/chromedriver')
URL = 'https://www.stats.govt.nz/experimental/covid-19-data-portal'
driver.get(URL)
element = WebDriverWait(driver, 15).until(
    EC.presence_of_element_located((By.ID, "download_data-show"))
    )
# Or this:
# element = WebDriverWait(driver, 15).until(
#     EC.presence_of_element_located((By.XPATH, '//*[@id="download_data-show"]'))
#     )

element.click()

这是我检查按钮时得到的 html 代码:

<button id="download_data-show" class="btn btn-modal action-button shiny-bound-input" type="button"> Download data </button>

当我运行 python 代码时,我得到一个 TimeoutException,当我尝试隐式等待 30 秒时,我得到以下错误:

NoSuchElementException: no such element: Unable to locate element: {"method":"link text","selector":"Download data"}
  (Session info: chrome=84.0.4147.135)

关于为什么找不到该元素有什么想法吗?

【问题讨论】:

    标签: python selenium button click


    【解决方案1】:

    你应该从这个 URL 下载它(它是 iframe)https://statisticsnz.shinyapps.io/covid_19_dashboard/ 我用你的代码测试了它,它找到了选择器。

    如果您想使用原始链接,也许这会对您有所帮助: Can't select an Iframe in selenium webdriver

    【讨论】:

      【解决方案2】:

      问题是,您的按钮位于 iframe 中:

      这是一个如何进入 iframe 的答案: Select iframe using Python + Selenium

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-05-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-12-04
        • 2022-07-05
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多