如何使用 Selenium 在 Chrome 上另存为 PDF
我们可以使用Seleniumwebdriver在Chrome上保存pdf文件。要在特定位置下载pdf文件,我们必须借助Options类。
我们将创建此类的一个对象并对其应用add_experimental_option。然后将值-prefs和要下载pdf的路径作为参数传递给此方法。
语法
o = Options()
o.add_experimental_option("prefs" ,
{"download.default_directory": "../downloads"} )示例
代码实现
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
#object of Options
o = Options()
#path of downloaded pdf
o.add_experimental_option("prefs",{"download.default_directory": "../downloads"})
#pass Option to driver
driver = webdriver.Chrome(executable_path='../drivers/chromedriver', options=o)
#implicit wait
driver.implicitly_wait(0.5)
#url launch
driver.get("http://demo.automationtesting.in/FileDownload.html")
#maximize browser
driver.maximize_window()
#identify elements
l = driver.find_element_by_id('pdfbox')
l.send_keys("test")
m = driver.find_element_by_id('createPdf')
m.click()
n = driver.find_element_by_id('pdf-link-to-download')
n.click()
#driver quit
driver.quit()热门推荐
1 训练狗狗憋尿
2 狗狗追踪训练
3 狗狗训练要多少钱
5 深圳狗狗训练
6 训练狗狗唤回
7 狗狗叫怎么训练
8 狗狗训练班
10 狗狗社会化训练
11 狗狗叫回训练
12 广州狗狗训练学校
13 狗狗训练躺下
14 狗狗随行训练
15 训练狗狗巡回
16 狗狗要怎么训练
17 狗狗喂食训练
18 训练狗狗躺下