Selenium+Python浏览器调用:Firefox

如何查看python selenium的API

python -m pydoc -p  4567

说明:

python -m pydoc表示打开pydoc模块,pydoc是查看python文档的首选工具;

-p 4567表示在4567端口上启动server

然后在浏览器中访问http://localhost:4567/,此时应该可以看到python中所有的Modules按ctrl+f,输入selenium,定位到selenium文档的链接,然后点击进入到http://localhost:4567/selenium.html这个页面

这就是selenium文档所在的位置了,接下来便可以根据自己的需要进行查看了。举个例子,如果你想查看Webdriver类的基本方法,可以访问这个页面http://localhost:4567/selenium.webdriver.remote.webdriver.html

Firefox浏览器调用

Firefox浏览器驱动添加

Firefox原生支持,无需下载驱动,只要安装浏览器即可

Firefox浏览器的调用

#coding=utf-8
from selenium import webdriver

driver=webdriver.Firefox()
url='http://www.baidu.com'
driver.get(url)
driver.close()

说明:

1、【#coding=utf-8】为了防止乱码问题,以便在程序中添加中文注释,把编码统一为UTF-8,注意=两遍不要留空格,否则不起作用,另外【#_*_coding:utf-8_*_】的写法也可以达到相同的作用

2、【from selenium import webdriver】该步骤是导入selenium的webdriver包,只有导入selenium包,我们才能使用webdriver API进行自动化脚本的开发

3、【driver=webdriver.Firefox()】这里将控制webdriver的Firefox赋值给driver,通过driver获得浏览器操作对象,后就可以启动浏览器、打开网址、操作对应的页面元素了。

Firefox自动运行中需要启动固定插件

首先,根据上述的浏览器调用,webdriver在启动浏览器时,启动的一个干净的没有任务、插件及cookies信息的浏览器(即使你本机的firefox安装了某些插件,webdriver启动firefox也是没有这些插件的),但是有可能被测系统本身需要插件或者需要调试等等,此时脚本会卡主无法运行,那么该如何解决呢?在解答问题前,先了解下,如何自定义带有特定配置的Firefox。

自定义Firefox配置文件

步骤如下:

1.运行CMD,打开Firefox的 Profile manager

2.点击"Create Profile...",完成步骤,包括输入Profile名字

3.点击"Start Firefox"

4.在新启动的Firefox中安装自己所需要的Add-On或者做其他配置

附:java代码:

string sPath = @"C:\Users\xxxx\AppData\Roaming\Mozilla\Firefox\Profiles\5f3xae4a.default";  FirefoxProfile ffprofile = new FirefoxProfile(sPath);

方法一:使用自定义的Firefox profile

用webdriver驱动firefox浏览器时如果不设置参数,默认使用的Firefox的profile和平时打开浏览器使用的firefox不一样,如果要使用平常使用的配置,需要增加如下操作:

 

profile_dir="C:\Users\admin\AppData\Roaming\Mozilla\Firefox\Profiles\wrdjxgdk.default-1434681389856"
profile = webdriver.FirefoxProfile(profile_dir)
driver = webdriver.Firefox(profile)

增加上述配置后,再调用driver进行get操作即可,其中黄色背景部分为Firefox的prifiles文件目录,一般都在:C:\Users\admin\AppData\Roaming\Mozilla\Firefox\Profiles目录下,至于启动什么样的浏览器,可以根据自己的需要定义,如:

a. 浏览网站,必须安装的插件,都安装完毕,且设置为:总是激活

b. 必要的安全设置等

方法二:使用代码进行配置

该方法是直接在代码里面进行插件的安装和profile的配置,如下先举例firebug插件的调用:

profile=webdriver.FirefoxProfile()
#加载插件
profile.add_extension('c:\\firebug-2.0.8-fx.xpi') 
#激活插件        
profile.set_preference("extensions.firebug.allPagesActivation", "on")
driver=webdriver.Firefox(profile)

增加上述配置后,再调用driver进行get操作即可。

我们除了可以使用上面提到的方法定制插件,webdriver还可以对profile进行定制(在firefox地址栏中输入about:config,可以查看firefox的参数),下面举例设置代理和默认下载路径:

myweb="192.168.9.111"
myport="8080"

profile=webdriver.FirefoxProfile()
#设置代理
profile.set_preference("network.proxy.type", 1)
profile.set_preference("network.proxy.http", myweb)
profile.set_preference("network.proxy.http_port", myport)
#设置文件下载目录
profile.set_preference("browser.download.folderList", 2)
profile.set_preference("browser.download.dir", "C:\\test")
driver=webdriver.Firefox(profile)

增加上述配置后,再调用driver进行get操作即可

参考资料

[1]Selenium2(WebDriver)总结(一)---启动浏览器、设置profile&加载插件,

http://www.cnblogs.com/puresoul/p/4251536.html

[2]Webdriver使用自定义Firefox Profile运行测试,

http://lijingshou.iteye.com/blog/2085276

[3]记录我遇到的selenium哪些令人摸不着头脑的问题,

http://blog.csdn.net/old_imp/article/details/11417999

posted @ 2015-12-02 17:47  leo博客  阅读(27484)  评论(0编辑  收藏  举报