2016-06-08 37 views
-1

我正在編寫python代碼來自動測試以下載文件。 我已通過單擊下載圖標下載了該文件。我現在正在嘗試的是聲明正確的文件是在後臺下載的。爲此,我需要提取get請求和參數。此參數和URL將包含用於標識是否/是否下載正確文檔的值。在Python中獲取獲取請求中的URL

你能幫我嗎。非常感謝。

以下是我正在運行的代碼。

import os 
from selenium import webdriver 
from selenium.webdriver.common.keys import Keys 

mime_types = "application/pdf,application/vnd.adobe.xfdf,application/vnd.fdf,application/vnd.adobe.xdp+xml" 

fp = webdriver.FirefoxProfile() 
fp.set_preference("browser.download.folderList", 2) 
fp.set_preference("browser.download.manager.showWhenStarting", False) 
fp.set_preference("browser.download.dir", os.getcwd()) 
fp.set_preference("browser.helperApps.neverAsk.saveToDisk", mime_types) 
fp.set_preference("plugin.disable_full_page_plugin_for_types", mime_types) 
fp.set_preference("pdfjs.disabled", True) 


driver = webdriver.Firefox(firefox_profile=fp) 
driver.get('site-name') 

# login 
driver.find_element_by_class_name('textboxUserName').send_keys(name) 
driver.find_element_by_id('dummy1').click() 
driver.find_element_by_id('content__login_Password').send_keys(pwd) 
driver.find_element_by_id('content__login_Password').send_keys(Keys.RETURN) 

# search 
driver.find_element_by_id('top-search-input').send_keys('9001') 
driver.find_element_by_id('top-search-input').send_keys(Keys.RETURN) 

# download 
driver.find_element_by_xpath("//ul[@class='search-result-list']/li[1]/div/ul/li[7]").click() 

如前所述,我想確保文件已被下載。所以我想要點擊下載按鈕時從GET獲取特定值。

謝謝

+0

在哪裏,做下載您當前的代碼? –

+0

如果你想要的網址,那麼爲什麼不單擊下載按鈕,你只需獲取關聯的鏈接。如果這沒有幫助,請提供更多信息。 –

回答

0

我看到兩種方法可以做到這一點。

  1. 就像我在評論中提到,你可以簡單地檢查,而不是點擊它的下載鏈接的「HREF」屬性。

  2. 一旦程序點擊這個鏈接使用:

    driver.implicitly_wait(5)#just爲安全起見

    打印driver.current_url

希望這幫助。

UPDATE: 基於您的評論,如果AJAX最終打開另一個窗口,那麼這將這樣的伎倆:

for handle in driver.window_handles: 
    driver.switch_to_window(handle) 
    print driver.current_url 
+0

不幸的是,沒有href屬性。我相信它是由Ajax實現的。此外,代碼繞過本機瀏覽器保存對話框下載文件,而是將其保存在預定義的位置。 –

+0

然後試試。經過大約一個小時的研究,這是我能找到的最好的。 [capture AJAX](http://stackoverflow.com/questions/26481212/capture-ajax-response-with-selenium-and-python) –