python - 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 包含下载链接。我想点击具体的下载链接。
提前致谢。
解决方案
您可以使用以下 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)
推荐阅读
- c++ - 自动解包一对迭代器
- postgresql - 将变量从一个 sql 脚本传递到另一个作为 postgres sql 中的参数
- firebase - 如何在 Flutter 中查询其字段值包含在本地列表变量中的 Firestore 数据
- c# - 使用 linq 排序、区分并选择前 5 个结果
- powershell - Test-WSMan 上的 if/else 语句
- javascript - 从有效负载中删除密钥
- google-sheets - 与多个工作表相比,识别列表中的新项目
- ansible - import_role 和 include_role 在变量优先级方面的区别
- reactjs - 在循环中将项目添加到状态数组
- reactjs - yup.js 验证数字字段大于兄弟,或可为空