【问题标题】:Save the pdf using the selenium webdriver in python在 python 中使用 selenium webdriver 保存 pdf
【发布时间】:2019-07-18 06:23:45
【问题描述】:

我无法保存单击时打开的 pdf。根据我的说法,使用 selenium webdriver 代码会自动运行。我希望打开的 pdf 文件应该通过在 python 中使用 selenium 的代码自动保存。请协助以下代码将pdf保存在文件夹中。

enter code here
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
import urllib.request
from bs4 import BeautifulSoup
import os
from selenium.webdriver.support.select import Select
import time
url = 'https://maharerait.mahaonline.gov.in'
chrome_path = r'C:/Users/User/AppData/Local/Programs/Python/Python36/Scripts/chromedriver.exe'

driver = webdriver.Chrome(executable_path=chrome_path)
driver.get(url)
WebDriverWait(driver, 
    20).until(EC.element_to_be_clickable((By.XPATH,"//div[@class='search- 
    pro-details']//a[contains(.,'Search Project Details')]"))).click()
Registered_Project_radio= WebDriverWait(driver, 
    10).until(EC.element_to_be_clickable((By.ID,"Promoter")))
driver.execute_script("arguments[0].click();",Registered_Project_radio)
Application = driver.find_element_by_id("CertiNo")
Application.send_keys("P50500000005")
Search = WebDriverWait(driver, 
    10).until(EC.element_to_be_clickable((By.ID,"btnSearch")))
driver.execute_script("arguments[0].click();",Search)
View = [item.get_attribute('href') for item in 
      driver.find_elements_by_tag_name("a") if
      item.get_attribute('href') is not None]

View = View[0]
request = urllib.request.Request(View)
btn = WebDriverWait(driver, 
     20).until(EC.element_to_be_clickable((By.XPATH, 
     "//a[@class='btn btn-md btn-success' and @id='btnShow_2017']")))

driver.execute_script("arguments[0].click();",btn)

【问题讨论】:

  • 通常在网络浏览器中,您可以设置如何处理不同类型的文件 - 下载或使用某些外部程序打开。它应该在选项中的某个地方。在一些问题中,我看到了如何使用 ChromeOptions 或 ChromeProfile 使用 webdriver 设置它。
  • 如果你可以得到href这个文件,那么你可以尝试使用urllib或requests来下载它。
  • 它似乎在data= 中添加了<object data=...> 的PDF 编码base64
  • 点击按钮后弹出窗口。
  • 这个弹出窗口里有什么?此窗口是否显示 PDF?似乎页面使用<object data=...> 显示PDF,所有PDF 都在data= 编码base64。

标签: python selenium-webdriver


【解决方案1】:

点击后会添加<object data="application/pdf;base64,...">,它在data=中将所有PDF作为文本编码为bas64

driver.execute_script("arguments[0].click();",btn)

time.sleep(5)

# get tag <object>
obj = driver.find_element_by_tag_name('object')

# get `data=`
data = obj.get_attribute('data')

# get text after `base64,`
text = data.split(',')[1]

# encode text to PDF's content (as bytes)
import base64
bytes = base64.b64decode(text)

# save bytes in file
with open('output.pdf', 'wb') as fp:
    fp.write(bytes)

现在你已经拥有了output.pdf

火狐测试

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
import urllib.request
from bs4 import BeautifulSoup
import os
from selenium.webdriver.support.select import Select
import time

url = 'https://maharerait.mahaonline.gov.in'
#chrome_path = r'C:/Users/User/AppData/Local/Programs/Python/Python36/Scripts/chromedriver.exe'

driver = webdriver.Firefox()#executable_path=chrome_path)
driver.get(url)
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH,"//div[@class='search-pro-details']//a[contains(.,'Search Project Details')]"))).click()
Registered_Project_radio = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID,"Promoter")))

driver.execute_script("arguments[0].click();",Registered_Project_radio)

Application = driver.find_element_by_id("CertiNo")
Application.send_keys("P50500000005")

Search = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID,"btnSearch")))
driver.execute_script("arguments[0].click();", Search)
View = [item.get_attribute('href') for item in driver.find_elements_by_tag_name("a") if item.get_attribute('href') is not None]

btn = WebDriverWait(driver, 
     20).until(EC.element_to_be_clickable((By.XPATH, 
     "//a[@class='btn btn-md btn-success' and @id='btnShow_2017']")))

driver.execute_script("arguments[0].click();",btn)

time.sleep(5)

obj = driver.find_element_by_tag_name('object')
data = obj.get_attribute('data')
text = data.split(',')[1]

import base64
bytes = base64.b64decode(text)

with open('output.pdf', 'wb') as fp:
    fp.write(bytes)

【讨论】:

猜你喜欢
  • 2018-10-26
  • 1970-01-01
  • 2013-02-10
  • 2017-08-26
  • 1970-01-01
  • 1970-01-01
  • 2021-07-12
相关资源
最近更新 更多