Selenium无法通过xpath或具有显式等待的ID找到元素(超时异常)

2024-04-20 12:15:16 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图从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)

你知道为什么找不到元素吗


Tags: of代码urldatadownloaddrivershowbutton