如何使用selenium和python处理firefox中的“保存文件”对话框?

时间:2016-05-06 16:49:14

标签: python-2.6

我正在尝试使用python中的selenium工具下载.csv文件,但点击了网页中的保存文件链接后,浏览器显示我对话框。我试过“警告”来处理这个但是它给我的错误就像,警报不存在.... 我使用python作为脚本语言,使用selenium作为工具。  这是我的代码:

fp = webdriver.FirefoxProfile()
fp.set_preference('browser.download.folderList', 2) # custom location
fp.set_preference('browser.download.manager.showWhenStarting', False)
fp.set_preference('browser.download.dir', 'C:\Temp\')
fp.set_preference('browser.helperApps.neverAsk.saveToDisk', 'application/csv,text/csv,text/comma-separated-values, application/octet-stream')
driver = webdriver.Firefox(profile)
driver.window_handles
driver.swich_to_window(driver.window_handles[0])
url=''
driver.get(url)
pass=driver.find_element_by_id('pswd')
pass.send_keys('xyz123')
driver.find_element_by_id('btnLogin').click()
driver.implicitly_wait(30)
driver.find_element_by_link_text('FileName').click()
driver.switch_to_alert().accept()

2 个答案:

答案 0 :(得分:4)

最后我完成了“保存文件”对话框。我用一些firefox配置文件首选项设置处理它。因为我无法找到所有可能的确切MIME类型。 下面是我处理保存文件对话框的代码:

fp = webdriver.FirefoxProfile()
fp.set_preference('browser.download.folderList', 2) 
fp.set_preference('browser.download.manager.showWhenStarting', False)
fp.set_preference('browser.download.dir', r'C:\Temp\')
fp.set_preference('browser.helperApps.neverAsk.openFile', 'text/csv,application/x-msexcel,application/excel,application/x-excel,application/vnd.ms-excel,image/png,image/jpeg,text/html,text/plain,application/msword,application/xml')
fp.set_preference('browser.helperApps.neverAsk.saveToDisk', 'text/csv,application/x-msexcel,application/excel,application/x-excel,application/vnd.ms-excel,image/png,image/jpeg,text/html,text/plain,application/msword,application/xml')
fp.set_preference('browser.helperApps.alwaysAsk.force', False)
fp.set_preference('browser.download.manager.alertOnEXEOpen', False)
fp.set_preference('browser.download.manager.focusWhenStarting', False)
fp.set_preference('browser.download.manager.useWindow', False)
fp.set_preference('browser.download.manager.showAlertOnComplete', False)
fp.set_preference('browser.download.manager.closeWhenDone', False)
driver = webdriver.Firefox(fp)
driver.window_handles
driver.switch_to_window(driver.window_handles[0])
url=''
driver.get(url)
pass=driver.find_element_by_id('pswd')
pass.send_keys('xyz123')
driver.find_element_by_id('btnLogin').click()
driver.implicitly_wait(30)
driver.find_element_by_link_text('FileName').click()
time.sleep(5)

答案 1 :(得分:0)

 from selenium import webdriver
 from selenium.webdriver.support.ui import WebDriverWait
 import time
 import pyautogui

try :



driver = webdriver.Firefox()

driver.implicitly_wait(30)
driver.maximize_window()

driver.get("https://www.citysdk.eu/wp-content/uploads/2013/09/DELIVERABLE_WP4_TA_SRS_0.21.pdf")
# WebDriverWait(driver, 10).until(lambda d: d.execute_script('return document.readyState') == 'complete')
# Click the OK button and close

time.sleep(5)
webelem = driver.find_element_by_id('download')
webelem.click()

time.sleep(5)
print('press enter')
pyautogui.press('enter')


except Exception as err:
 print('ERROR: %sn' % str(err))

 driver.quit()