首页 > 解决方案 > 使用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()

标签: pythonseleniumpdfdownload

解决方案


试试这个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()

浏览器截图:

在此处输入图像描述


推荐阅读