selenium tips

Selenium Python 自动化测试
本文介绍使用Selenium和Python进行自动化测试的方法,包括设置浏览器窗口大小、获取和操作cookie、保存页面截图等WebDriver操作;同时介绍了如何使用WebElement进行元素定位、文本框输入等操作;还提供了针对Firefox和Chrome浏览器的特定配置方法。

【Selenium Python

1、selenium3 python github

2、文档:readthedocs版github版API。貌似后者只是前者的一个章节。


【Selenium Python WebDriver

1、设置浏览器窗口大小:WebDriver.set_window_size

2、获取cookie:WebDriver.get_cookies

3、保存当前页面:WebDriver.page_source

4、保存当前页面截图:WebDriver.save_screenshot

5、元素查找:WebDriver.find_element_by_*

6、去掉链接的新标签页打开属性(target="_blank"):

1
2
aEle  =  Driver.find_elements_by_tag_name( 'a' ):
Driver.execute_script( "arguments[0].removeAttribute('target')" , aEle)


【Selenium Python WebElement

1、文本框清空:WebElement.clear

2、文本框填写:WebElement.send_keys

3、按钮(元素)点击:WebElement.click

4、定位元素坐标:WebElement.location

5、获取元素的宽度和高度:WebElement.size

6、保存元素截图:element.screenshot


【Selenium Python Firefox

1、Firefox驱动:https://github.com/mozilla/geckodriver/releases

2、禁止firefox加载图片:Disable images in Selenium Python

3、Firefox设置代理:

1
2
3
4
5
profile  =  webdriver.FirefoxProfile()
profile.set_preference( "network.proxy.type" 1 )
profile.set_preference( "network.proxy.http" , ProxyAddr)
profile.set_preference( "network.proxy.http_port" , ProxyPort)
Driver  =  webdriver.Firefox(profile)

4、如果不在环境变量中设置驱动路径,可以用executable_path指定驱动文件路径:

1
webdriver.Firefox(executable_path = r 'E:\webdriver\geckodriver.exe' )


【Selenium Python Chrome

1、Chrome驱动:https://sites.google.com/a/chromium.org/chromedriver/home

2、禁止Chrome加载图片:Python: Disable images in Selenium Google ChromeDriverselenium设置:浏览器不显示图片

3、如果不在环境变量(Path)中设置驱动路径,可以用executable_path指定驱动文件路径:

1
webdriver.Chrome(executable_path = r 'E:\webdriver\chromedriver.exe' )

4、selenium3、chorme55下的一些配置。

1
2
3
4
5
6
7
8
chromeOptions  =  webdriver.ChromeOptions()
prefs  =  {}
#禁用图片
prefs[ 'profile.managed_default_content_settings.images' =  2
#设置下载目录
prefs[ 'download.default_directory' =  'D:/tmp'
chromeOptions.add_experimental_option( "prefs" ,prefs)
browser  =  webdriver.Chrome(chrome_options = chromeOptions)


【相关阅读】

1selenium之WebDriver

2、Python将浏览器cookies共享给requests库

3、数据抓取的艺术(一):Selenium+Phantomjs数据抓取环境配置

4、Skip image loading by selenium

5、Selenium WebDriver go to page without waiting for page load

6、Get HTML Source of WebElement in Selenium WebDriver using Python

7、How to print the contents in the “ui”,“li” tags using selenium web driver


*** walker * Updated 2016-12-2 ***

本文转自walker snapshot博客51CTO博客,原文链接http://blog.51cto.com/walkerqt/1596120如需转载请自行联系原作者

RQSLT
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值