selenium tips

selenium是用于web自动化测试的工具,所以也就能够被用来控制浏览器进行一些自动化任务。这里记录一些使用tips。

安装

selenium针对不同的浏览器提供不同的webdriver。webdriver可以理解成类似硬件的驱动程序,具体介绍看这里:http://www.seleniumhq.org/docs/03_webdriver.jsp

假设我们想控制Google Chrome,那么需要安装chrome对应的webdriver(假设已经安装好Chrome),Google官方提供的:https://sites.google.com/a/chromium.org/chromedriver/home
基本就是下载你的OS对应的driver,设置好路径就行了。也可以参考:https://intoli.com/blog/running-selenium-with-headless-chrome/

安装完后,用命令测试一下:

$ chromedriver
Starting ChromeDriver 2.29.461571 (8a88bbe0775e2a23afda0ceaf2ef7ee74e822cc5) on port 9515
Only local connections are allowed.

说明安装成功。

然后安装python包:

$ pip install selenium

启动

from selenium import webdriver

options = webdriver.ChromeOptions()
options.add_argument('headless')
options.add_argument('window-size=1200x600')
options.add_argument('user-data-dir=xxx')

d = webdriver.Chrome(chrome_options=options)
d.get('http://www.google.com')
d.close()

其中,user-data-dir是chrome的用户数据目录,你可以指向你的系统默认目录,参考:https://chromium.googlesource.com/chromium/src/+/master/docs/user_data_dir.md,或者你也可以指向一个自定义的目录,用来创建和保存一个专用于测试的profile。

关于selenium python API可以参考:
http://selenium-python.readthedocs.io/,这个不是最新的,但比官方的更详细。
官方文档:https://seleniumhq.github.io/selenium/docs/api/py/api.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值