selenium中的Select模块以及firefox下载设置

一直以来在python脚本处理网页下拉框时,都是使用find_element,然后选择option,模拟鼠标click()操作的方式来进行处理的,最近发现selenium本身提供Select模块,可以有效进行处理。没找到哪儿有具体说明,查看了python帮助文档,学习了下具体使用方式。

简单示例

from selenium.webdriver.support.ui import Select
Select(driver.find_element_by_tag_name("select")).select_by_index(2)

Select类的__init__函数需要带有一个webelement的输入参数,亦即find_element_by_...方式的返回值。

Select中的方法及属性说明

清除元素选择的方法

deselect_all() 清除所有选择项;

deselect_by_index(index) 清除相应index的选择;

deselect_by_value(value) 注意value是web元素中的value属性对应的值,说明中举例<option value = "foo">Bar</option>,使用deselect_by_value时,输入参数为"foo";

deselect_by_visible_text(text)亦处理上例中的<option value = "foo">Bar</option>时,使用方式为deselect_by_visible_text("Bar");

选择元素对应方法

select_by_index(index),根据index进行选择;

select_by_value(value) 根据元素的value属性进行选择;

select_by_visible_text(text) 根据显示文本进行选择;

一些数据属性

all_selected_options 返回所有选择过的元素列表;

first_selected_option 第一个被选择的元素;

options 可选择列表;

firefox下载设置

为什么选择firefox?在使用过程中,发现safari无法设置浏览器属性,本想将不同的内容存储在不同的文件夹,而Safari无法设置。可以通过类中构造函数信息确认,Safari中不带有profile的设置。

from selenium import webdriver

help(webdriver.Safari)查看__init__函数信息:
__init__(self, port=0, executable_path='/usr/bin/safaridriver', reuse_service=False, desired_capabilities={'browserName': 'safari', 'version': '', 'platform': 'MAC'}, quiet=False, keep_alive=True, service_args=None)

help(webdriver.Firefox)查看__init__函数信息:
__init__(self, firefox_profile=None, firefox_binary=None, timeout=30, capabilities=None, proxy=None, executable_path='geckodriver', options=None, service_log_path='geckodriver.log', firefox_options=None, service_args=None, desired_capabilities=None, log_path=None, keep_alive=True)

Firefox使用时弹出错误

selenium.common.exceptions.WebDriverException: Message: 'geckodriver' executable needs to be in PATH.

解决方式,安装geckodriver,利用homebrew安装geckodriver的方式:brew install geckodriver

Firefox的下载设置

profile = FirefoxProfile()

#setPreference(“browser.helperApps.neverAsk.openFile”,value);
#设置哪些文件直接打开

profile.set_preference("browser.helperApps.neverAsk.saveToDisk" , "application/x-msdownload,application/zip,text/plain")
#用来设置哪些文件自动下载,具体文件类型可参考http://www.w3school.com.cn/media/media_mimeref.asp

profile.set_preference("browser.helperApps.alwaysAsk.force", False)
profile.set_preference('general.warnOnAboutConfig', False)

profile.set_preference("browser.download.folderList",2)
#2为下载到设置的文件夹中,0下载到用户桌面1为下载Downlaod文件夹;

profile.set_preference("browser.download.manager.showWhenStarting", False)
#缺省值为true,是否弹出下载设置管理窗口;

profile.set_preference("browser.download.dir",saveDataPath)
#设置下载文件夹位置

profile.update_preferences()
self.webDriver = webdriver.Firefox(firefox_profile=profile)

python与matlab中datenum数值差异

总是觉得matlab操作图形比较方便,再处理信息画图时,出现日期不匹配的问题,原因在于同样的datenum在python和matlab中处理稍有差别。

help(dates.date2num)得到的帮助文档信息

Number of days (fraction part represents hours, minutes, seconds, ms) since 0001-01-01 00:00:00 UTC, plus one.

也就是时间转换为数值的计算是从0001-01-01作为1开始计算的

import matplotlib.dates as dates
import datetime
a=datetime.datetime(1, 1, 1, 0, 0)
dates.date2num(a)
得到的结果为1.0

matlab中

doc datenum得到的帮助文档信息

The datenum function creates a numeric array that represents each point in time as the number of days from January 0, 0000.

>> datenum('01-01-0001')
ans =
367
datenum('01-01-0001')得到的结果为367,在matlab和python中日期转换为浮点的值相差为366。

octave中的datenum和matlab中的结果一致

>>datenum('0001-01-01')
ans = 367

所以从结果上看,python的datenum与matlab中的datenum存在366的恒定差值。

小知识

python语句换行: 在python语句太长时,需要换行处理,可以行尾用\作为连接符;

字典根据值排序: 可以使用lambda函数,lambda为匿名函数,形如a = sorted(x.items(), key=lambda x: x[1], reverse=True/False)方式,x为字典,a为元组列表。

解压中文文件问题:在调用MAC unzip命令解压带中文的文件,弹出错误,error: cannot create -?-+???+??????.xls Illegal byte sequence,查到的信息unzip可以带有-o选项指定编码格式GBK进行解压,但MAC上的unzip版本没有此选项。后来使用unar进行指定编码格式unar -e GBK file.zip进行解压,可以使用homebrew命令直接安装brew install unar。

如何将错误输出到文件:打印到屏幕上的信息有两种:stdout stderr 如果使用command > filename或者command 1> filename的形式,只将stdout的内容写到filename文件中;如果用command 2> filename的形式,只有将2-stderr的内容重新定向到filename中;如果要将stdout和stderr的信息全部重定向到文件中,可以用:command > filename 2>&1。关于2>&1的理解0,1,2为文件描述符,0表示stdin,1表示stdout,2表示stderr,2>的意思是将stderr的信息输出到某文件,&1表示指向stdout的文件位置。

参考地址

https://www.sitepoint.com/mime-types-complete-list/

http://www.w3school.com.cn/media/media_mimeref.asp

https://www.toolsqa.com/selenium-webdriver/how-to-download-files-using-selenium/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值