python - 使用python selenium驱动下载pdf文件
问题描述
我正在尝试从链接steel下载 pdf 文件('DOWNLOAD PRODUCT CATALOGUE') 。使用 python xpath 来实现这一点。但出现语法错误。尝试了所有的排列和组合。我尝试过的代码如下:
import time
from selenium import webdriver
driver = webdriver.Chrome('c:/windows/chromedriver.exe') # Optional argument, if not specified will search path.
driver.get("https://sail.co.in/en/products/sail-structural-sections")
time.sleep(5) # Let the user actually see something!
elem =driver.find_element_by_xpath('//a[text()='Download Product Catalogue']//parent::div[@class='toppdf_brochure pdfbrochure']')
elem.click()
time.sleep(5) # Let the user actually see something!
driver.quit()
解决方案
试试这个xpath
:
elem =driver.find_element_by_xpath("//*[contains(text(), 'Download Product Catalogue')]")
完整代码:
import time
from selenium import webdriver
driver = webdriver.Chrome('c:/windows/chromedriver.exe') # Optional argument, if not specified will search path.
driver.get("https://sail.co.in/en/products/sail-structural-sections")
time.sleep(5) # Let the user actually see something!
elem =driver.find_element_by_xpath("//*[contains(text(), 'Download Product Catalogue')]")
elem.click()
time.sleep(5) # Let the user actually see something!
driver.quit()
浏览器截图:
推荐阅读
- python - 为什么内置函数在 python 中没有被归类为可调用函数,如何让它们在我的程序中显示为可调用函数?
- r - R:循环删除 NA 只删除一些 NA?
- kotlin - 在 Kotlin 中使用委托实现时传递此引用
- azure - Azure Bot 框架 AAD 身份验证问题
- typescript - Typescript - 推断函数是否被柯里化
- mysql - 仅在选择中更改用户电子邮件地址格式
- ios - 摇动手势后从 SKScene 中选择随机元素
- python-3.x - ModuleNotFound 导致 502 在 aws lambda 上使用 zappa 部署 django
- javascript - 分配给 window.location 的代码是否容易受到 XSS 攻击?
- python - Django - 与系统不同 - 如何更改按钮颜色以不喜欢?