首页 > 解决方案 > python - 当有多个包含使用Selenium webdriver在python中的下载链接的div时如何单击特定的下载链接

问题描述

我想从这里下载 F&O Bhavcopy

https://www.nseindia.com/all-reports-derivatives#cr_deriv_equity_archives

在 Python 中使用硒。

F&O Bhavcopy 在 div 的 1 中。该 div 中有下载链接。

我试过以下代码:

WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"//div[@class='card-body']/span[@class='reportDownloadIcon']/a[@class='pdf-download-link']"))).click()

或者

driver.find_element_by_xpath("//div[@class='card-body']/span[@class='reportDownloadIcon']/a").click()

或者

driver.find_element_by_css_selector("div.card-body span.reportDownloadIcon a.pdf-download-link").click()

没有什么对我有用。因为有很多 div 包含下载链接。我想点击具体的下载链接。

提前致谢。

标签: pythonselenium

解决方案


您可以使用以下 xpath 单击 F&O Bhavcopy(csv) 的下载按钮:

WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"//div[@class='card-body']//label[contains(text(),'F&O - Bhavcopy(csv)')]//following-sibling::span[@class='reportDownloadIcon']"))).click()

如果您单击 F&O - Bhavcopy (fo.zip) 的下载按钮,则可以使用:

WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"//div[@class='card-body']//label[contains(text(),'F&O - Bhavcopy (fo.zip)')]//following-sibling::span[@class='reportDownloadIcon']"))).click()

除了答案

使用 JavaScriptExecutor 单击按钮单击

downloadButton = driver.find_element_by_xpath("//div[@class='card-body']//label[contains(text(),'F&O - Bhavcopy(csv)')]//following-sibling::span[@class='reportDownloadIcon']//a")
driver.execute_script("arguments[0].click();", downloadButton)

推荐阅读