Selenium 3.x 出来也有段时间了,有哪些坑呢?

有好长一段时间没有用selenium了。最近想用来做个web自动化的小工具。根据以往经验,firefox是不需要下载driver的。启动firefox. 立即抛出一个异常。

selenium.common.exceptions.WebDriverException: Message: 'geckodriver' executable needs to be in PATH

搜索得知:

  1. selenium 3.x开始,webdriver/firefox/webdriver.py的init中,executable_path=”geckodriver”;而2.x是executable_path=”wires”
  2. firefox 47以上版本,需要下载第三方driver

有没搞错,firefox也需要装driver了!
windows
下载解压后将getckodriver.exe复制到Firefox的安装目录下,
设置path,或者:

from selenium.webdriver.firefox.firefox_profile import FirefoxProfile
driver = webdriver.Firefox(executable_path="C:\Program Files (x86)\Mozilla Firefox\geckodriver.exe")

mac:
可以用: brew install geckodriver
或者下载
把解压缩后的文件放到/usr/local/bin里面

from selenium import webdriverfrom selenium.webdriver.firefox.firefox_binary import FirefoxBinary
binary = FirefoxBinary('/Applications/Firefox.app/Contents/MacOS/firefox-bin')
browser = webdriver.Firefox(firefox_binary=binary)

Chrome就不用说了,直接下载一个driver, 一般直接放在python的路径下,找起来方便。

有的时候,加载图片,CSS等很慢,其实我们并不需要都加载完全,可以禁止掉
那么代码可以写成这样。

from selenium import webdriver
import site
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.firefox.firefox_profile import FirefoxProfile
def get_python_location(self):
return site.getsitepackages()[0] def get_driver(self):
if os.path.exists(os.path.join(self.get_python_location(), 'chromedriver.exe')):
chrome_options = Options()
chrome_options.add_experimental_option("prefs", {'profile.manage_default_content_settings.images': 2})
driver = webdriver.Chrome(os.path.join(self.get_python_location(), 'chromedriver.exe'), chrome_options=chrome_options)
else:
firefoxProfile = FirefoxProfile()
firefoxProfile.set_preference('permissions.default.stylesheet', 2)
firefoxProfile.set_preference('dom.ipc.plugins.enabled.libflashplayer.so', 'false')
firefoxProfile.set_preference('permissions.default.image', 2)
driver = webdriver.Firefox(firefoxProfile,executable_path="C:\Program Files (x86)\Mozilla Firefox\geckodriver.exe")
return driver

用phantomjs也可以,直接这样:

driver = webdriver.PhantomJS(service_args=['--load-images=false'])

这样,加载速度就可以快很多了。

更多精彩,请关注 微信公众号:python爱好部落

最新文章

  1. 使用js_md5加密密码
  2. C#小小总结(面向对象)
  3. Hibernate 的dialect 大全
  4. poj1113 凸包
  5. 用jquery写循环播放div的相关笔记 珍贵的总结 -1
  6. javascript模拟jQuery封装委托事件,兼容IE
  7. css3动画笔记
  8. spring中文乱码过滤器
  9. 【JavaScript】XMLHttpRequest Level2使用指南
  10. 解决mac上Android开发时出现的ADB server didn't ACK
  11. DataGrid( 数据表格) 组件[2]
  12. js校验身份证
  13. 爬起点小说day03
  14. angular url 传参
  15. Oracle Package的全局变量与Session
  16. DataGridView修改数据并传到数据库
  17. ref和引用类型传参的区别
  18. python的类和实例化对象
  19. 2.6 C++通过引用来传递和返回类对象
  20. 下载企业级证书打包的app 出现“无法下载应用程序”的问题

热门文章

  1. Log POST Data in Nginx
  2. [BZOJ1597]土地购买
  3. angular多页面切换传递参数
  4. Ubuntu下解决解压zip文件中文文件名乱码问题
  5. 我们为什么要使用AOP?
  6. 使用requireJs的方法
  7. 【解决】使用compass watch xxx.scss 失败
  8. [iOS]从零开始开发一个即时通讯APP
  9. ReactiveCocoa源码解析(三) Signal代码的基本实现
  10. html页面多个a标签点击时显示不同的样式