用过selenium 和excel 的vba 以后,你们一定会和我有一样的感觉,selenium 和excel的宏录制明显就是一对兄弟, 因为,宏的作用是 对excel进行自动化,而selenium 在网页操作进行自动化, 既然牵涉到自动化,那么我们一定是为了解决的重复操作的效率问题。
在我的工作中,经常会牵涉到,数据下载的功能,那么我们就用selenium,把这一步下载进行自动化,并验证数据下载的功能模块的正确性。
在开始之前,我们首先在 windows 下安装python pycharm,然后pip install selenium2.x,用pip show selenium来查看版本, 如果我们用ie或者chrome 我们需要安装相应的驱动,但是为了方便我们就还是用firefox48一下的版本,这样就用不着安装驱动了,48版本需要安装geckodriver,驱动下载地址:https://docs.seleniumhq.org/download/ 。
import unittest
from selenium import webdriver
class PythonOrgSearch(unittest.TestCase): def setUp(self):
#加载浏览器驱动
self.driver = webdriver.Firefox()
#打开网页
self.driver.get("http://////////")
def openfox(self):
driver.find_element_by_id("username").send_keys("账户")
driver.find_element_by_name("password").send_keys("密码")
driver.find_element_by_id("login-btn").click()
def downloadss(self):
#前一天时间选取
drive.find_element_by_css_selector("[value]='1']").click()
#查询商户数据
driver.find_element_by_id("agent_profit_search").click()
#下载数据
driver.find_element_by_id("agent_profit_static_down").click()
def tearDown(self): self.driver.close() if __name__ == "__main__": unittest.main()