当python中有许多div包含SeleniumWebDriver的下载链接时,如何单击特定的下载链接

2024-10-04 09:17:47 发布

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

我想下载F&;O Bhavcopy来自

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

在Python中使用selenium

F&;O Bhavcopy位于其中一个分区中。该分区中有下载链接

我尝试了以下代码:

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是由下载链接组成的。我想点击特定的下载链接

提前谢谢


Tags: divpdf链接downloaddriverbodyelementcard
2条回答

使用a[onclick*=cr_deriv_equity_archives]css选择器下载,但首先必须在页面上选择日期:

driver.get('https://www.nseindia.com/all-reports-derivatives#cr_deriv_equity_archives')
cards = wait.until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, "#cr_deriv_equity_archives .card")))

# select date
driver.execute_script("arguments[0].value=arguments[1]", 
                      driver.find_element_by_id("cr_deriv_equity_archives_date"), "01-Jan-2020")
for card in cards:
    card.find_element_by_css_selector("a[onclick*=cr_deriv_equity_archives]").click()

你可以点击F&;的下载按钮;O使用以下xpath复制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)

相关问题 更多 >