使用python中的Selenium Webdriver下载在embed标签中具有stream-url是chrome扩展名的文件

局部放电

根据我的代码,我试图单击包含隐藏文档的“查看”按钮,我需要在python中使用Selenium Webdriver下载该文档。当我检查时,我得到了stream-url = chrome-extension:// mhjfbmdgcfjbbpaeojofohoefgiehjai / 85967fa5-7853-412e-bbe5-c96406308ec6这个在嵌入标记中找到的流URL。我没有得到如何下载该文档。

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
import pandas as pd
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)
driver.get(View)
html = urllib.request.urlopen(request).read()
soup = BeautifulSoup(html, 'html.parser')
divPInfo = soup.find("div", {"id": "DivDocument"})
title = divPInfo.find("div", {'class': 'x_panel'}, 
       recursive=False).find("div", {'class': 'x_title'}).find(
      "h2").text.strip()
print(title)
with open("uploads.csv" , "a") as csv_file:
    csv_file.write(title + "\n")
    csv_file.close()    
table = pd.read_html(driver.page_source)[11]                 
print(table)
table.to_csv("uploads.csv" , sep=',',index = False)
btn = WebDriverWait(driver, 
    20).until(EC.element_to_be_clickable((By.XPATH, "//button[@class='btn 
    btn-info btn-xs' and @id='btnShow_10']")))
driver.execute_script("arguments[0].click();",btn)
简单

在Firefox页面中用于<object data="...">显示带有扫描的PDF。“上传的文档”部分中有按钮,以显示其他扫描。

这段代码使用这些按钮来显示扫描,从中获取数据<object>并保存在文件document-0.pdfdocument-1.pdf,等等。

我使用的是您在上一个问题的答案中看到的相同代码:
在python中使用Selenium Webdriver保存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 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 = webdriver.Firefox()

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)

time.sleep(5)

View = [item.get_attribute('href')
         for item in driver.find_elements_by_tag_name("a")
          if item.get_attribute('href') is not None]

# if there is list then get first element
if View:
    View = View[0]

#-----------------------------------------------------------------------------

# load page    
driver.get(View)

# find buttons in section `Uploaded Documents`
buttons = driver.find_elements_by_xpath('//div[@id="DivDocument"]//button')

# work with all buttons 
for i, button in enumerate(buttons):

    # click button
    button.click()

    # wait till page display scan
    print('wait for object:', i)
    search = WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.TAG_NAME, "object")))

    # get data from object    
    print('get data:', i)    
    import base64

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

    # save scan in next PDF     
    print('save: document-{}.pdf'.format(i))    
    with open('document-{}.pdf'.format(i), 'wb') as fp:
        fp.write(bytes)

    # close scan        
    print('close document:', i)    
    driver.find_element_by_xpath('//button[text()="Close"]').click()    

# --- end ---

driver.close()

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章