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

1.6 selenium3+firefox环境搭建

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

1.6 selenium3+firefox环境搭建

(uH0BQu"F0d$b0有不少小伙伴在安装selenium环境后启动firefox报错,因为现在selenium升级到3.0了,跟2.0的版本还有有一点区别的。
}3M[hd}6Y|~0(备注:这里不建议大家用selenium3,坑太多,如果selenium2实在用不了,那就看这篇吧)51Testing软件测试网6gsr9N:FB/N
安装环境过程中主要会遇到三个坑:51Testing软件测试网!cUZuY$u^7?Q
1.'geckodriver' executable needs to be in PATH
D:M:[ T%zDJKk02.Expected browser binary location, but unable to find binary in default location
.d;e0^REsTO03.Unsupported Marionette protocol version 2, required 351Testing软件测试网"[`$k!p0\:@Q|o Y ]
环境准备:51Testing软件测试网#_` V+l3a*h3RH
--python3.6

vc*Pzy5b @@WO$b#F0

}kA4J Wa0--selenium3.0
(@IV%z{$]~v0--firefox5051Testing软件测试网h*Ma@L }0{xZ
 51Testing软件测试网5d#y8{:Cr6j
一、安装python
]&j#e6V:S#u01.安装python这个简单,下载版本后傻瓜式安装就行了。51Testing软件测试网 gY)D@ A9A d:f
2.安装好之后,看下这个目录D:\python\Scripts,有没pip.exe和easy_install.exe(一般都有,没有的话得重新安装一次了)
Zq {M C,r&Ja03.将D:\python和D:\python\Scripts,添加到环境变量path下51Testing软件测试网w{0k3x)d!L

51Testing软件测试网5[1v{9v U&D,^HR

j)_$bX mH%F051Testing软件测试网s(\j{O+O%VD%h

 51Testing软件测试网3i6f x.W$U

51Testing软件测试网@{7jN"nt(JuW

二、检查pip工具51Testing软件测试网}5r3fU},h
1.打开cmd,输入:pip,出现如下图界面,说明pip环境OK.51Testing软件测试网;X#K[2ODulb7@j p
>>pip

2FC9j H4PX6|0

p ? XD@ v0

E,W;VSQL|0

c)lH h Z&K5S0 51Testing软件测试网 j.NB5RM`%UK!an

%x Kq,O5b[ st2O{&Y0三、安装selenium3.0
p#I!kCP2l{4H01.cmd输入:pip install selenium
hYoyY;` m3^0>>pip install selenium51Testing软件测试网K)JSjX
2.首次安装要看到100%完成,中途失败就重新多输入几次安装。

ZCj%ig{?}n051Testing软件测试网!O7~lt7R|9u

51Testing软件测试网$w/tu7z&R`)Q-y

&Q5a XA`.C2A0 

5?;Z-|"H7_%Tr3hL0

5viN#G&MGHG0四、检查selenium环境

4C2tp*h ?Xk/eT|0

#S4QgN|d4@"?01.在cmd输入如下指令检查环境
pFn,`*S0f5S(GT&\F0>>python51Testing软件测试网%Co8pt0P_ TX
>>from selenium import webdriver
)C9twS6I0[)J"`:E0>>driver=webdriver.Firefox()
.s*DK%ak0>>driver.get("https://www.baidu.com")51Testing软件测试网s'nTZ0WKv\:F
2.能看到浏览器正常启动,说明环境OK,如果遇到异常就继续看下面解决方案。

MjxG5^N7o6u"P A051Testing软件测试网 Yi8?s$FT qc&Ls r

51Testing软件测试网{zg'T0E+|A

`7l)wv*jDi!K lV0 51Testing软件测试网H9iD)\B ro

\ s/t2}0t}f[0五、遇到第一个坑:'geckodriver' executable needs to be in PATH51Testing软件测试网-[I[X#L2U;g;I
1.如果启动浏览器过程中报如下错误
IX"f:PPD#y0Traceback (most recent call last):51Testing软件测试网 s"xLlB c\
  File "<stdin>", line 1, in <module>51Testing软件测试网+]?N%V{(Iv
  File "D:\test\python3\lib\site-packages\selenium\webdriver\firefox\webdriver.py", line 145, in __init__51Testing软件测试网 F'J u$Lz9O7YCV
    self.service.start()
W#AO*Cl?0  File "D:\test\python3\lib\site-packages\selenium\webdriver\common\service.py", line 81, in start
[p7E/@6R kH&\0    os.path.basename(self.path), self.start_error_message)51Testing软件测试网 `x5D8aF4h7o
selenium.common.exceptions.WebDriverException: Message: 'geckodriver' executable needs to be in PATH.51Testing软件测试网F1q}\9D(y7j1\^6g
2.这个是因为最新的selenium3.0启动firefox需要geckodriver.exe这个驱动文件。

4c2\7gF+t%nES3[0

BvTgKq?#p'L@03.下载之后,配置到环境变量path下(可以直接放python根目录)51Testing软件测试网N?OJD ^+m
 51Testing软件测试网b q/R]RXp%J
六、遇到第二坑:Expected browser binary location, but unable to find binary in default location51Testing软件测试网9F'z ig+q1X'| ap/F)m
1.如果启动浏览器过程中报如下错误:51Testing软件测试网])?$gM`cI
Traceback (most recent call last):
w(]X/C%fT&\y|!G(T0  File "<stdin>", line 1, in <module>
TRpS-O D,v!f*io0  File "D:\test\python3\lib\site-packages\selenium\webdriver\firefox\webdriver.py", line 155, in __init__51Testing软件测试网Uwt e(nHHf
    keep_alive=True)
r!h t8M3n t0  File "D:\test\python3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 92, in __init__51Testing软件测试网-Qu3^7U#M
    self.start_session(desired_capabilities, browser_profile)

F:LVD-f-N0

H%i8{:U oSZVb0  File "D:\test\python3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 179, in start_session51Testing软件测试网)PG?G%WP!Np|
    response = self.execute(Command.NEW_SESSION, capabilities)51Testing软件测试网h+{h,b6MGKt
  File "D:\test\python3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 238, in execute51Testing软件测试网$X6e&W^"n5P,J^
    self.error_handler.check_response(response)
R f-_&z}B0  File "D:\test\python3\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 193, in check_response51Testing软件测试网#pb8gK xA
    raise exception_class(message, screen, stacktrace)51Testing软件测试网 X4mB.z@#wb
selenium.common.exceptions.WebDriverException: Message: Expected browser binary location, but unable to find binary in default location,51Testing软件测试网kyWy6P
no 'moz:firefoxOptions.binary' capability provided, and no binary flag set on the command line.

*n\ N7rp9]@ ^}2E0

`3i,rHZX%n02.这个是因为firefox.exe这个文件也需要配置到环境变量path下。51Testing软件测试网H,_8}fdHO;e/D
3.这个路径就是安装完firefox后,找到firefox.exe这个文件的地址,加到path下。

YyjS9w \/By051Testing软件测试网YH^|UQ

5y3k/f ^m*OfS0

M+t|;~ uc#F0 

KF[(goZ051Testing软件测试网C2D wT,v T:|

七、遇到第三坑:Unsupported Marionette protocol version 2, required 351Testing软件测试网.M-OPCzO`s[x
1.如果启动浏览器过程中出现如下错误51Testing软件测试网^]N_*Qd_ m6XG1r
 Traceback (most recent call last):
Qj!o4I \iE0  File "<stdin>", line 1, in <module>51Testing软件测试网?9f-_X\s

51Testing软件测试网_;~2o3s+`

  File "D:\test\python3\lib\site-packages\selenium\webdriver\firefox\webdriver.py", line 155, in __init__51Testing软件测试网(o E_"`@B
    keep_alive=True)
Mm&m7mPX3R6|0  File "D:\test\python3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 92, in __init__51Testing软件测试网$gS]-u EV)O:ZM
    self.start_session(desired_capabilities, browser_profile)
!Jz-`P?K0  File "D:\test\python3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 179, in start_session
$O F cYJ0Z1jj]px0    response = self.execute(Command.NEW_SESSION, capabilities)51Testing软件测试网1Cf d"dw"k Xz#B2j
  File "D:\test\python3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 238, in execute
\%[E$h4d3s0    self.error_handler.check_response(response)
:E"s0j+a1g+r1?p0  File "D:\test\python3\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 193, in check_response

xgr&Y-t!|Z }051Testing软件测试网!H5mH8c|k#l)r

    raise exception_class(message, screen, stacktrace)51Testing软件测试网 Br [/Z b2G&bH
selenium.common.exceptions.WebDriverException: Message: Unsupported Marionette protocol version 2, required 3
x1mUT/B+w02.这个错误原因是firefox版本过低了,最新的selenium3.0版本支持firefox47以上的版本,升级版本就可以了

| U0L'} `%p.V#|051Testing软件测试网.jA+b'Z~*_h6g{

,|C@ l6q051Testing软件测试网ks/|C Yn]"{

 

)Ml M+Y3h`7x051Testing软件测试网*jvqY{2uU3c?\

 总结:整个环境的配置是python3.6+selenium3.0+firefox47以上版本,当然python用2.7版本也是可以的。51Testing软件测试网E+s-a}$Jk0\:K1_:g


TAG:

 

评分:0

我来说两句

显示全部

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

Open Toolbar