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

1.6 selenium3+firefox环境搭建

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

1.6 selenium3+firefox环境搭建

/h kZ9\7Q%_15244338有不少小伙伴在安装selenium环境后启动firefox报错,因为现在selenium升级到3.0了,跟2.0的版本还有有一点区别的。
k6[q L0x6K3g15244338(备注:这里不建议大家用selenium3,坑太多,如果selenium2实在用不了,那就看这篇吧)
&x-~XD ]1?*h15244338安装环境过程中主要会遇到三个坑:51Testing软件测试网?6|*Z._[,`g
1.'geckodriver' executable needs to be in PATH51Testing软件测试网#rvJ&u8Ms X
2.Expected browser binary location, but unable to find binary in default location
#vOC.Mal152443383.Unsupported Marionette protocol version 2, required 351Testing软件测试网k"u_Y+k
环境准备:51Testing软件测试网e7gT5p p,G~j%X.h
--python3.6

{ W@Q%v"V [v4H7J15244338

A,v@e2zb15244338--selenium3.051Testing软件测试网5_%f!D+hT
--firefox50
&xqzM3DRp [ w15244338 51Testing软件测试网eYpk4{O8_#u[t
一、安装python51Testing软件测试网9F,n(KR:e~%K/E6A-|
1.安装python这个简单,下载版本后傻瓜式安装就行了。51Testing软件测试网$M-Y2~?3}$b9D.G_(S
2.安装好之后,看下这个目录D:\python\Scripts,有没pip.exe和easy_install.exe(一般都有,没有的话得重新安装一次了)51Testing软件测试网 l]r(P)u&NL
3.将D:\python和D:\python\Scripts,添加到环境变量path下

R4xV'j%{"?1524433851Testing软件测试网^V^e"q,a't

C(E1w!?Z~ xM W15244338

} i#V S&Y B15244338 

Ix\vKsW1524433851Testing软件测试网U p@V2z WNX(\

二、检查pip工具51Testing软件测试网4dNm*~w G2s/YW
1.打开cmd,输入:pip,出现如下图界面,说明pip环境OK.
x0k|e,}u6m9P9y15244338>>pip51Testing软件测试网f bV.D0x6A!aS

51Testing软件测试网?@q2NYIX4u}/j

n?c3cC|t1524433851Testing软件测试网9Xm%Sv1C\(sK'jM4R

 

6tB4V?M{15244338

\iV!k LeJ?q0W15244338三、安装selenium3.0
/s wAo"c sr4G152443381.cmd输入:pip install selenium51Testing软件测试网P+Hh)}3Y*A]
>>pip install selenium
1HWX lqhV152443382.首次安装要看到100%完成,中途失败就重新多输入几次安装。

#C)J*p Fw?c S15244338

w9E;M hc9y0g(|1524433851Testing软件测试网NWp']rGy/Z|

6u1|\2Yg P[-S15244338 

1Pl2w`/[ALY&W15244338

J9KrV B15244338四、检查selenium环境

P S^~%@0}8Hu;x15244338

RtQ}O152443381.在cmd输入如下指令检查环境51Testing软件测试网#j-im!Gr M
>>python51Testing软件测试网2pm6NF5~F
>>from selenium import webdriver51Testing软件测试网$s0Fh^r;y2Y8i;S
>>driver=webdriver.Firefox()
:X[ I4TA zz%~Jj]15244338>>driver.get("https://www.baidu.com")51Testing软件测试网5}j'f]#hs
2.能看到浏览器正常启动,说明环境OK,如果遇到异常就继续看下面解决方案。

8B)qE-J nIN&A15244338

4U#UdP_L15244338

"Ow7LH,A15244338

B0a+?2rR(M,Iq p6R15244338 

*O-E uD5F}15244338

:iA'~6T#E!e Nu S1o OT15244338五、遇到第一个坑:'geckodriver' executable needs to be in PATH
ClR ZJye152443381.如果启动浏览器过程中报如下错误51Testing软件测试网"}_!vA~kFR/W
Traceback (most recent call last):51Testing软件测试网 {%l&T_&uh
  File "<stdin>", line 1, in <module>51Testing软件测试网Ux g1B$P|-Yz2g6l
  File "D:\test\python3\lib\site-packages\selenium\webdriver\firefox\webdriver.py", line 145, in __init__
L0ec'Zc0eAPz5?15244338    self.service.start()
)`+wkP j2p5y15244338  File "D:\test\python3\lib\site-packages\selenium\webdriver\common\service.py", line 81, in start51Testing软件测试网F9x"t4{s6h)yS
    os.path.basename(self.path), self.start_error_message)
@ e"{ `*J'S(\k15244338selenium.common.exceptions.WebDriverException: Message: 'geckodriver' executable needs to be in PATH.51Testing软件测试网/?vV |N7d$C p
2.这个是因为最新的selenium3.0启动firefox需要geckodriver.exe这个驱动文件。51Testing软件测试网;y"q8P/n4yx:le

51Testing软件测试网V&x+J I7C] R

3.下载之后,配置到环境变量path下(可以直接放python根目录)
l){[4x5i AT T15244338 
,y'x"X&];WSB15244338六、遇到第二坑:Expected browser binary location, but unable to find binary in default location51Testing软件测试网3d*nwbgK+L'bG
1.如果启动浏览器过程中报如下错误:
5g*YR|,N'F,p15244338Traceback (most recent call last):
4M mR O:ct9e(_*T!Lj15244338  File "<stdin>", line 1, in <module>
LD:KjP*N)w6N*O.i15244338  File "D:\test\python3\lib\site-packages\selenium\webdriver\firefox\webdriver.py", line 155, in __init__
(K[(kpU y ~'a#i6A15244338    keep_alive=True)
J5g?m*lA8^15244338  File "D:\test\python3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 92, in __init__51Testing软件测试网 TXT`;]u/G
    self.start_session(desired_capabilities, browser_profile)51Testing软件测试网-^!v,|!o0H N7q[!U

`$D x5OO;qlp\+J c5G15244338  File "D:\test\python3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 179, in start_session
| T pl.^l5n15244338    response = self.execute(Command.NEW_SESSION, capabilities)
&c SL~,ry$u~/F2F15244338  File "D:\test\python3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 238, in execute
q;nHGW{15244338    self.error_handler.check_response(response)51Testing软件测试网:I{R7G!\ o9k^
  File "D:\test\python3\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 193, in check_response51Testing软件测试网)}.H%~ht
    raise exception_class(message, screen, stacktrace)51Testing软件测试网 t K&G K8w;i6{N
selenium.common.exceptions.WebDriverException: Message: Expected browser binary location, but unable to find binary in default location,
)R;CuP:SS-J15244338no 'moz:firefoxOptions.binary' capability provided, and no binary flag set on the command line.

V;OVe#[x15244338

_5LS5z$aC,o152443382.这个是因为firefox.exe这个文件也需要配置到环境变量path下。
tMR2\@(i152443383.这个路径就是安装完firefox后,找到firefox.exe这个文件的地址,加到path下。

)[6W1};[k0cU){1524433851Testing软件测试网-E|yf4`[$Ty*g9o

51Testing软件测试网6h*R\7D9Qx

&~A.m$^4KF15244338 51Testing软件测试网E*{)z PCg5O

q'ZG0@#b V15244338七、遇到第三坑:Unsupported Marionette protocol version 2, required 3
)H(W6Y._ B152443381.如果启动浏览器过程中出现如下错误
7x'y-yy+Cr}8e15244338 Traceback (most recent call last):51Testing软件测试网e6d.Y#T o-A
  File "<stdin>", line 1, in <module>

+\,oMO-ce15244338

U#\Q8Ea3`kis15244338  File "D:\test\python3\lib\site-packages\selenium\webdriver\firefox\webdriver.py", line 155, in __init__51Testing软件测试网f8r ?_T U
    keep_alive=True)51Testing软件测试网#`s6m#wYm~k1W_;Wy
  File "D:\test\python3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 92, in __init__
(C|*Y9ve j/UZ+b15244338    self.start_session(desired_capabilities, browser_profile)51Testing软件测试网;B(Z{\:s7[-q+h A!M
  File "D:\test\python3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 179, in start_session51Testing软件测试网8Ew|2S!f6\(o5dl!r
    response = self.execute(Command.NEW_SESSION, capabilities)
._ U BUq#o15244338  File "D:\test\python3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 238, in execute51Testing软件测试网 jK2u u2|m\y
    self.error_handler.check_response(response)
KyD.W{3\CE15244338  File "D:\test\python3\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 193, in check_response51Testing软件测试网U+jM"c"d(w

51Testing软件测试网,J7BeJ J.d R7D*T ?

    raise exception_class(message, screen, stacktrace)51Testing软件测试网)AcF;Gb`
selenium.common.exceptions.WebDriverException: Message: Unsupported Marionette protocol version 2, required 3
kn^*{B K!J4e152443382.这个错误原因是firefox版本过低了,最新的selenium3.0版本支持firefox47以上的版本,升级版本就可以了51Testing软件测试网Bi-g7k cb

51Testing软件测试网(~_,k,e5Cy

Qr9qD^5Z|15244338

8k'OQ CT15244338 

}$k-GpH1524433851Testing软件测试网$TK7Z7I q:KtkQu

 总结:整个环境的配置是python3.6+selenium3.0+firefox47以上版本,当然python用2.7版本也是可以的。51Testing软件测试网 v0n!Qu;o@9{~


TAG:

 

评分:0

我来说两句

显示全部

:loveliness: :handshake :victory: :funk: :time: :kiss: :call: :hug: :lol :'( :Q :L ;P :$ :P :o :@ :D :( :)

Open Toolbar