Selenium自动化测试 – 启动浏览器
第一个selenium就。。。
先show代码:
from selenium import webdriver
driver = webdriver.Firefox()
driver.get("https://www.oxcoder.com")
assert "oxcoder".decode('utf-8') in driver.title
print driver.title
driver.close()
这只是一段使用selenium操作firefox的代码,最核心一句就是webdriver.Firefox()
然而,轻松写完却无法启动,出现如下问题:
问题No.1 - 找不到可执行文件geckodriver
Traceback (most recent call last):
File "/Users/sure/Python/helloSelenium.py", line 3, in <module>
driver = webdriver.Firefox()
File "/usr/local/lib/python2.7/site-packages/selenium/webdriver/firefox/webdriver.py", line 140, in __init__
self.service.start()
File "/usr/local/lib/python2.7/site-packages/selenium/webdriver/common/service.py", line 81, in start
os.path.basename(self.path), self.start_error_message)
selenium.common.exceptions.WebDriverException: Message: 'geckodriver' executable needs to be in PATH.
Exception AttributeError: "'Service' object has no attribute 'process'" in <bound method Service.__del__ of <selenium.webdriver.firefox.service.Service object at 0x10861a490>> ignored
[Finished in 0.2s with exit code 1]
[shell_cmd: python -u "/Users/sure/Python/helloSelenium.py"]
[dir: /Users/sure/Python]
[path: /usr/bin:/bin:/usr/sbin:/sbin]
解决方案:
- 下载对应的webdriver:
- Firefox - geckodriver
- chrome - chromedriver 或使用~> pip/brew install chromedriver
- python目录下/usr/local/bin
问题No.2 - 无法连接
还以为就这样解决了,谁知道。。。
Traceback (most recent call last):
File "/Users/sure/Python/helloSelenium.py", line 3, in <module>
driver = webdriver.Firefox()
File "/usr/local/lib/python2.7/site-packages/selenium/webdriver/firefox/webdriver.py", line 140, in __init__
self.service.start()
File "/usr/local/lib/python2.7/site-packages/selenium/webdriver/common/service.py", line 102, in start
raise WebDriverException("Can not connect to the Service %s" % self.path)
selenium.common.exceptions.WebDriverException: Message: Can not connect to the Service geckodriver
[Finished in 30.5s with exit code 1]
[shell_cmd: python -u "/Users/sure/Python/helloSelenium.py"]
[dir: /Users/sure/Python]
[path: /usr/bin:/bin:/usr/sbin:/sbin]
解决方案:
- 据说这是Selenium的问题参考github
- 【求教】有人知道怎么破吗???
此文仅适用于Mac OSX