博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Selenium Python FirefoxWebDriver处理打开保存对话框
阅读量:4593 次
发布时间:2019-06-09

本文共 3049 字,大约阅读时间需要 10 分钟。

 代码如下(网上示例):

#profile =  webdriver.FirefoxProfile(r"C:\Users\Skyyj\AppData\Roaming\Mozilla\Firefox\Profiles\1rzh6139.default")

profile = webdriver.FirefoxProfile()
##设置成0代表下载到浏览器默认下载路径;设置成2则可以保存到指定目录
profile.set_preference("browser.download.folderList", 2)
#这里设置与否不影响,没有发现有什么影响。
#profile.set_preference("browser.download.manager.showWhenStarting", False)
profile.set_preference("browser.download.dir", r"c:\Down")
profile.set_preference("browser.helperApps.neverAsk.saveToDisk", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
#这里设置与否没有发现有什么影响
#profile.set_preference("browser.helperApps.alwaysAsk.force", False);
self.driver = webdriver.Firefox(profile)

 

 

自己写的实例(Environment:Python3.6.5+Selenium+PyCharm+Firefox61+FirefoxDriver)

from selenium import webdriver

from selenium.webdriver.common.keys import Keys
import time,os
profile = webdriver.FirefoxProfile()

#save the file to directory=E:\sli\CIFS2

profile.set_preference("browser.download.dir", r"E:\sli\CIFS2")
profile.set_preference("browser.download.folderList", 2)
profile.set_preference("browser.download.manager.showWhenStarting", False)

#download .zip file

profile.set_preference("browser.helperApps.neverAsk.saveToDisk", "application/zip")
#driver = webdriver.Firefox(firefox_profile=profile)
driver = webdriver.Firefox(profile)
driver.get("http://10.49.5.8:8080/RLink/login.jsp")
driver.maximize_window()
# -----------------------------------Login Steps----------------------------------------------
# input user name
driver.find_element_by_id("j_username").send_keys("1")
time.sleep(1)
# input password
driver.find_element_by_id("j_password").send_keys("1")
time.sleep(1)
# click "Log in" button
driver.find_element_by_id("buttonSubmit").click()
time.sleep(1)
# -----------------------------------Download Steps----------------------------------------------
# Highlight a zip file by xpath
driver.find_element_by_xpath("/html/body/div[6]/div[2]/div/div[7]/div/div/div[3]/div/div[4]/div/div[4]/div/div/div/div/div/ul/li[10]/div/table/tbody/tr[1]/td/div/div/div/table/tbody/tr/td/div/div/img").click()
time.sleep(2)
# click "Download" button
driver.find_element_by_xpath("/html/body/div[6]/div[2]/div/div[5]/div/table/tbody/tr/td[1]/table/tbody/tr/td[2]/div/table/tbody/tr/td[2]/table/tbody/tr[2]/td/div").click()
time.sleep(2)
# -----------------------------------Logout Steps----------------------------------------------
# click "下拉框" of user menu
driver.find_element_by_xpath('/html/body/div[6]/div[2]/div/div[5]/div/table/tbody/tr/td[2]/table/tbody/tr/td/div/table/tbody/tr/td[2]/table/tbody/tr/td[2]/img').click()
time.sleep(1)
# click "Log Out"
driver.find_element_by_xpath("//div[text()='Log Out']").click()
time.sleep(1)
# click "Yes" on confirm dialog
driver.find_element_by_xpath('/html/body/div[9]/div/div/div/div[3]/div/div[2]/button[1]').click()
# click "No" on confirm dialog
#driver.find_element_by_xpath('/html/body/div[9]/div/div/div/div[3]/div/div[2]/button[2]').click()
time.sleep(1)
driver.quit()

 

 

 

 

转载于:https://www.cnblogs.com/Raul2018/p/9288560.html

你可能感兴趣的文章
html的学习
查看>>
[转载]java字符串模糊匹配(
查看>>
rsync
查看>>
阅读偏底层技术书有感
查看>>
【leetcode】Path Sum
查看>>
URL传递多个参数遇到的bug
查看>>
linux基础-附件1 linux系统启动流程
查看>>
C# 中静态调用C++dll 和C# 中动态调用C++dll
查看>>
linux并发控制之完成量
查看>>
二十、oracle pl/sql基础
查看>>
PHP之旅 php数据类型
查看>>
FPGA中IBERT核的应用(转)
查看>>
堆排序(heapsort)
查看>>
ActiveX控件开发 C#
查看>>
UVa 1149 - Bin Packing
查看>>
C++ Opencv HoughLines()用霍夫变换在二元图像中寻线
查看>>
验证方法学的发展历程及比较
查看>>
[BZOJ1478]Sgu282 Isomorphism
查看>>
UIView 的exclusiveTouch属性
查看>>
遇见未知的自己
查看>>