测试必须在时间、质量和成本之间获取一个平衡点,这是测试策略和测试设计的价值体现。

1.6 selenium3+firefox环境搭建

上一篇 / 下一篇  2018-06-12 11:03:58 / 个人分类:Python+Selenium1 环境搭建

1.6 selenium3+firefox环境搭建

51Testing软件测试网q7}&g H;`Vk

有不少小伙伴在安装selenium环境后启动firefox报错,因为现在selenium升级到3.0了,跟2.0的版本还有有一点区别的。51Testing软件测试网g"m8f){5F+d
(备注:这里不建议大家用selenium3,坑太多,如果selenium2实在用不了,那就看这篇吧)51Testing软件测试网&l!? F2DZ8q
安装环境过程中主要会遇到三个坑:51Testing软件测试网 U(I1GQ%FY2k
1.'geckodriver' executable needs to be in PATH51Testing软件测试网@U6zWM0k$O_ @R(Y
2.Expected browser binary location, but unable to find binary in default location51Testing软件测试网6B(Q%NWgq
3.Unsupported Marionette protocol version 2, required 3
y?ihX7Q([0环境准备:
a%P`g/R!O8k [K0--python3.651Testing软件测试网YcfVp n9}M

(rRp [+P0--selenium3.0
6Kt}5D4s W0--firefox5051Testing软件测试网6d+Q;Vq#m5E
 51Testing软件测试网 N~n3f0gag
一、安装python51Testing软件测试网zh'][8e!_X?
1.安装python这个简单,下载版本后傻瓜式安装就行了。
qJSu!Q*h&EB9f|$v o02.安装好之后,看下这个目录D:\python\Scripts,有没pip.exe和easy_install.exe(一般都有,没有的话得重新安装一次了)
4f9\ y[^03.将D:\python和D:\python\Scripts,添加到环境变量path下51Testing软件测试网 Gq)W` I5I_

51Testing软件测试网&[T%J a FfEHT.{

+e~F\h%G0

g9s9o$g.T6nkP{0 51Testing软件测试网A7eJw~ rJB

b{7h8O j0二、检查pip工具
4Pp;GeXq j4QOC01.打开cmd,输入:pip,出现如下图界面,说明pip环境OK.
D-gZr9vp6wp K0>>pip51Testing软件测试网7i o:e i9i C*W o%G n

51Testing软件测试网nxs8w BUz

o-ENSNx{051Testing软件测试网!fhq:Z%Ul

 

E^/\U*y#b I3v:Z051Testing软件测试网W']@$j;h0I L6N3k

三、安装selenium3.0
j%K_7Y-s${V4a01.cmd输入:pip install selenium51Testing软件测试网9O3QC%V o5c*X0}pVzJK
>>pip install selenium51Testing软件测试网 G,l;E6HX@$]
2.首次安装要看到100%完成,中途失败就重新多输入几次安装。

.T u e.d Y m3\051Testing软件测试网Ee.ky!WQ0sN3prY

51Testing软件测试网 EJ7C2[.KHI,|6Q(H

i\&QL)_\/e0 51Testing软件测试网? sv'Jv9pB!h

51Testing软件测试网/h4W*x&N}t2G'v j2J9f

四、检查selenium环境51Testing软件测试网l ~ bu@*N+oT{

v/P7Jc$j01.在cmd输入如下指令检查环境
&{C,q5m"z5vp0>>python
%X'|^-[p3Q3fB0>>from selenium import webdriver51Testing软件测试网 l D,gct*q%W
>>driver=webdriver.Firefox()
O:\2dWWs%O0>>driver.get("https://www.baidu.com")
EaWh#ika02.能看到浏览器正常启动,说明环境OK,如果遇到异常就继续看下面解决方案。

sI$Zl8S1m m4xQM8j051Testing软件测试网:a1Ly2r h

51Testing软件测试网zJI-^:H:j1\/i\t

51Testing软件测试网/?#` _ENm@!Jp

 51Testing软件测试网 F*sdy6J1TP

51Testing软件测试网Yj3vq GJ:a

五、遇到第一个坑:'geckodriver' executable needs to be in PATH
luh;mc)e2p"M01.如果启动浏览器过程中报如下错误51Testing软件测试网c(SWt6b6h9Q
Traceback (most recent call last):
.z"C4[.h1S qG'v&Q-_~9x0  File "<stdin>", line 1, in <module>51Testing软件测试网 U+J,UR"N
  File "D:\test\python3\lib\site-packages\selenium\webdriver\firefox\webdriver.py", line 145, in __init__51Testing软件测试网4A4`n0{ ib
    self.service.start()
"h1|u(R{5H1e6oi0  File "D:\test\python3\lib\site-packages\selenium\webdriver\common\service.py", line 81, in start51Testing软件测试网_"}Yo4lY"es+T:Z
    os.path.basename(self.path), self.start_error_message)51Testing软件测试网\C$w8Q~y*z
selenium.common.exceptions.WebDriverException: Message: 'geckodriver' executable needs to be in PATH.51Testing软件测试网s#?x|c4a!xI'wP]
2.这个是因为最新的selenium3.0启动firefox需要geckodriver.exe这个驱动文件。51Testing软件测试网U;aWpru ZQ|

51Testing软件测试网9w+s:S"sOZl

3.下载之后,配置到环境变量path下(可以直接放python根目录)
bb+ew LJ D#\'l4f.}0 
1BU3c#hI%DN S0六、遇到第二坑:Expected browser binary location, but unable to find binary in default location51Testing软件测试网FS eS,D
1.如果启动浏览器过程中报如下错误:
%^"@ Q Hxj%ri)W t2p3Ko0Traceback (most recent call last):
jRxL#n A c0  File "<stdin>", line 1, in <module>
FP2_M%c2l g0  File "D:\test\python3\lib\site-packages\selenium\webdriver\firefox\webdriver.py", line 155, in __init__
r7x8S,h z9IEPgD0    keep_alive=True)
f.`,]qo'O0  File "D:\test\python3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 92, in __init__
k"S"R&u;Dl0}&m0    self.start_session(desired_capabilities, browser_profile)51Testing软件测试网1C A1t&K#^|K

3`#i:`Jf)C)XHu0  File "D:\test\python3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 179, in start_session51Testing软件测试网9Xj\_.d N
    response = self.execute(Command.NEW_SESSION, capabilities)
*|Kj e0v4Ax0  File "D:\test\python3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 238, in execute
X-t.P+YCpRMZ0    self.error_handler.check_response(response)
5p$G2z dl0  File "D:\test\python3\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 193, in check_response51Testing软件测试网R,_b$}gF0Md [3c"a
    raise exception_class(message, screen, stacktrace)
%\q%YlC X*t @A0selenium.common.exceptions.WebDriverException: Message: Expected browser binary location, but unable to find binary in default location,51Testing软件测试网$bsG c6[5MT0y`u
no 'moz:firefoxOptions.binary' capability provided, and no binary flag set on the command line.

-W;pS J)?]X'Yp:I8C0

*L)\3_MW_02.这个是因为firefox.exe这个文件也需要配置到环境变量path下。51Testing软件测试网(M.Rg;R6S'mf*T\L
3.这个路径就是安装完firefox后,找到firefox.exe这个文件的地址,加到path下。51Testing软件测试网+K:`:X,e:L7L:g:~

51Testing软件测试网6A)ptI`$w

$S1zXP/B:CD5p:sY,P0

6]*KXmh&A0 51Testing软件测试网 U8G6[u#J)Y`m`

51Testing软件测试网:T5l0B(@JJ4r&k2f

七、遇到第三坑:Unsupported Marionette protocol version 2, required 3
6af!H Sjb_/_9j01.如果启动浏览器过程中出现如下错误
6JzQ(G`Z)}?0 Traceback (most recent call last):
\o,@Wj f;c1eO/r5Z0  File "<stdin>", line 1, in <module>51Testing软件测试网 HxR v$kn]*\hc

51Testing软件测试网-ivucs7Z HK

  File "D:\test\python3\lib\site-packages\selenium\webdriver\firefox\webdriver.py", line 155, in __init__51Testing软件测试网`A8qD7P j"a1B z~
    keep_alive=True)51Testing软件测试网"AtwTD*gi
  File "D:\test\python3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 92, in __init__51Testing软件测试网&NU2mCH8?H%x]7D7Jq
    self.start_session(desired_capabilities, browser_profile)
.|oV%q1E;UKS:RH0  File "D:\test\python3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 179, in start_session
#r)`"|:w7Y| `0    response = self.execute(Command.NEW_SESSION, capabilities)51Testing软件测试网\A6[o5b3u
  File "D:\test\python3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 238, in execute51Testing软件测试网 T'e A Qg8qZ
    self.error_handler.check_response(response)51Testing软件测试网#t:]7`B)N f(n6Mw
  File "D:\test\python3\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 193, in check_response

:w*y H4\P|OB051Testing软件测试网yQ)t7[I

    raise exception_class(message, screen, stacktrace)51Testing软件测试网zyG.][&a [
selenium.common.exceptions.WebDriverException: Message: Unsupported Marionette protocol version 2, required 351Testing软件测试网TJ0t*A4r{
2.这个错误原因是firefox版本过低了,最新的selenium3.0版本支持firefox47以上的版本,升级版本就可以了51Testing软件测试网 x(SXN.Q:a

!u[(yA&h5H,~ S)N0

X,V'Z `Sg O051Testing软件测试网P? jwiA

 51Testing软件测试网;{4t a_,G;YF

tWa bg zg7l0 总结:整个环境的配置是python3.6+selenium3.0+firefox47以上版本,当然python用2.7版本也是可以的。

M;A+VF5TR(^N0

TAG:

 

评分:0

我来说两句

Open Toolbar