我創建了一個程式,該程式將轉到某個網站,輸入作品集編號,然后向下瀏覽頁面并單擊以生成 PDF。我現在需要將該 pdf 下載到指定路徑。
我已經能夠生成PDF。但是我正在發送密鑰但它不起作用。
PDF 檔案網址:https : //gisweb.miamidade.gov/SPTXLienLetters/ReportPage.aspx? folio = 0131230371470 & pSecurityGuardDistrict =& pStreetLightDistrict =& pMultiPurposeDistrict =& pMunicipalityForDistrict = MIAMI & pAddressForDistrict204N% 2000ST
用于轉到頁面并生成 pdf 的代碼:
options = webdriver.ChromeOptions()
options.headless = True
driver = webdriver.Chrome(ChromeDriverManager().install(), options=options)
driver.implicitly_wait(10)
#set chromedriver.exe path
driver = webdriver.Chrome('driver/chromedriver.exe')
driver.maximize_window()
#driver.execute_script("document.body.style.zoom='75%'")
#Launch Url
driver.get('https://gisweb.miamidade.gov/SPTXLienLetters/')
driver.find_element_by_xpath('//*[@id="divSplashScreenContent"]/table/tbody/tr[2]/td/div/div').click()
#Take data from config file
file = open('configsa.txt')
lines = file.readlines()
folio_number = lines[0]
driver.implicitly_wait(30)
#Find elements and take snapshots
elementID = driver.find_element_by_xpath('//*[@id="txtAddress"]')
elementID.send_keys(folio_number)
elementID = driver.find_element_by_xpath('//*[@id="tdDivAddress"]/table/tbody/tr/td[2]').click()
elementID = driver.find_element_by_xpath('//*[@id="trOtherAppLinks"]/td/div/span[1]/a').click()
我曾嘗試使用以下代碼進行保存但無濟于事:
action_chains = ActionChains(driver)
action_chains.send_keys(Keys.CONTROL).send_keys('s').perform()
或者
saveas = ActionChains(driver).key_down(Keys.CONTROL).send_keys('S').key_up(Keys.CONTROL).send_keys('MyDocumentName').key_down(Keys.ALT).send_keys('S').key_up(Keys.ALT)
上述代碼均無效。任何人都可以幫助解決這個問題嗎?
uj5u.com熱心網友回復:
單擊“生成并列印財產留置權信函”標簽后,它會打開一個新選項卡,其中顯示了生成的檔案。
您必須切換到新選項卡,然后在那里執行 CONTROL S 以保存檔案,關閉新選項卡并切換回原始視窗:
window1 = driver.window_handles[0]
elementID = driver.find_element_by_xpath('//*[@id="txtAddress"]')
elementID.send_keys(folio_number)
elementID = driver.find_element_by_xpath('//*[@id="tdDivAddress"]/table/tbody/tr/td[2]').click()
elementID = driver.find_element_by_xpath('//*[@id="trOtherAppLinks"]/td/div/span[1]/a').click()
window2 = driver.window_handles[1]
driver.switch_to_window(window2)
action_chains = ActionChains(driver)
action_chains.send_keys(Keys.CONTROL).send_keys('s').perform()
driver.close()
driver.switch_to_window(window1)
uj5u.com熱心網友回復:
當您單擊 時Generate and Print Property Lien Letter,將啟動一個新選項卡。Selenium 對此一無所知,因此它希望您讓 Selenium 知道您要切換到新選項卡:
current_windows = driver.current_window_handle
elementID = driver.find_element_by_xpath('//*[@id="trOtherAppLinks"]/td/div/span[1]/a').click()
all_handles = driver.window_handles
driver.switch_to.window(all_handles[1])
print('Switch to new tab has been successful.')
此外, CTRL s會提示一個下載框,您也必須撰寫非 selenium 代碼來解決這個問題。
您不應driver.implicitly_wait(30)在代碼中多次使用。因為這是一個隱式等待,應該為整個 Web 驅動程式生命周期設定。
uj5u.com熱心網友回復:
您無法下載 pdf 的原因是因為當您打開 PDF chrome 或您的瀏覽器在 pdf 查看器中打開它時。此選項卡不再屬于 selenium Web 驅動程式的范圍。您知道這一點是因為您無法在瀏覽器上進行檢查并選擇輸入框來指定路徑。要下載檔案,您可以使用更簡單的方法。獲得 PDF 網址后,您可以使用如下所示的 urllib 庫
import urllib.request
pdf_url = "https://gisweb.miamidade.gov/SPTXLienLetters/ReportPage.aspx?folio=0131230371470&pSecurityGuardDistrict=&pStreetLightDistrict=&pMultiPurposeDistrict=&pMunicipalityForDistrict=MIAMI&pAddressForDistrict=1400 NW 44 ST"
download_path = "local_filepath/filename.pdf"
urllib.request.urlretrieve(pdf_url, download_path)
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/402418.html
標籤:
