selenium + firefox + webdriver 遇到的坑

2019-03-11 11:03:22 浏览数 (1)

lunix中启动webdriver时报错一:

测试代码为:

代码语言:javascript复制
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3. from selenium import webdriver
  4. driver = webdriver.Firefox()
  5. driver.get("https://www.baidu.com")

运行报错信息如下:

代码语言:javascript复制
  1. Traceback (most recent call last):
  2. File "maimai_web.py", line 14, in <module>
  3. driver = webdriver.Firefox()
  4. File "/usr/local/python3.6/lib/python3.6/site-packages/selenium/webdriver/firefox/webdriver.py", line 152, in __init__
  5. keep_alive=True)
  6. File "/usr/local/python3.6/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 98, in __init__
  7. self.start_session(desired_capabilities, browser_profile)
  8. File "/usr/local/python3.6/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 188, in start_session
  9. response = self.execute(Command.NEW_SESSION, parameters)
  10. File "/usr/local/python3.6/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 256, in execute
  11. self.error_handler.check_response(response)
  12. File "/usr/local/python3.6/lib/python3.6/site-packages/selenium/webdriver/remote/errorhandler.py", line 194, in check_response
  13. raise exception_class(message, screen, stacktrace)
  14. selenium.common.exceptions.WebDriverException: Message: Process unexpectedly closed with status 1

处理方法:

代码语言:javascript复制
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3. from pyvirtualdisplay import Display
  4. from selenium import webdriver
  5. display = Display(visible=0, size=(1920, 1080))
  6. display.start()
  7. driver = webdriver.Firefox()
  8. driver.get("https://www.baidu.com")

结果:

运行ok,搞定!

坑二、webdriver实例化报错

采用多线程调用webdriver时候,偶尔会出现这样的错:selenium.common.exceptions.WebDriverException: Message: connection refused

代码语言:javascript复制
  1. Exception in thread Thread-2:
  2. Traceback (most recent call last):
  3. File "/usr/local/python3.6/lib/python3.6/threading.py", line 916, in _bootstrap_inner
  4. self.run()
  5. File "/usr/local/python3.6/lib/python3.6/threading.py", line 864, in run
  6. self._target(*self._args, **self._kwargs)
  7. File "maimai_tran_account_driver.py", line 591, in debug
  8. t = TrainAccount(count,lock)
  9. File "maimai_tran_account_driver.py", line 32, in __init__
  10. self.chrome = webdriver.Firefox()
  11. File "/usr/local/python3.6/lib/python3.6/site-packages/selenium/webdriver/firefox/webdriver.py", line 152, in __init__
  12. keep_alive=True)
  13. File "/usr/local/python3.6/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 98, in __init__
  14. self.start_session(desired_capabilities, browser_profile)
  15. File "/usr/local/python3.6/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 188, in start_session
  16. response = self.execute(Command.NEW_SESSION, parameters)
  17. File "/usr/local/python3.6/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 256, in execute
  18. self.error_handler.check_response(response)
  19. File "/usr/local/python3.6/lib/python3.6/site-packages/selenium/webdriver/remote/errorhandler.py", line 194, in check_response
  20. raise exception_class(message, screen, stacktrace)
  21. selenium.common.exceptions.WebDriverException: Message: connection refused

 查看geckodriver.log具体报错信息。

后续的坑继续填。。。

(adsbygoogle = window.adsbygoogle || []).push({});

0 人点赞