Python3+Selenium環境配置


 一、所需工具

  1、Python3.6安裝包

  2、Selenium安裝包(selenium-server-standalone-3.8),如果是Python3的話可以不用下載selenium壓縮包,Python3有自帶的,直接輸入命令安裝即可

  3、Chromedriver驅動(Chromedriver2.3)

  4、Firefox驅動(geckodriver0.18)

  5、Chrome瀏覽器(chrome v.59)

  6、Firefox瀏覽器(Firefox v.54)

  7、Pycharm

  二、工具安裝

  1、安裝Python,安裝完設置環境變量(將Python安裝路徑放入path下)

  2、selenium安裝:cmd首先進入E:\Python\Scripts目錄下,然后輸入pip install -U selenium,等待安裝完成即可

  3、pip安裝:將pip工具包解壓到指定盤,cmd命令進入該盤下,輸入命令python setup.py install;再切換到E:\Python\Scripts 目錄下輸入E:\Python\Scripts > easy_install pip

  4、setuptools安裝:將setuptools工具包解壓到指定盤里,進入cmd,執行python ez_install.py即可

  5、將下載好的Chromedriver和geckodriver驅動放到Python安裝路徑下

  注意:Chromedriver和geckodriver驅動一定要和電腦上的瀏覽器版本對應,否則代碼運行會報錯

  Chromedriver和chrome瀏覽器對應版本

 

 

  代碼:

  from selenium import webdriver

  import time

  # browser=webdriver.Chrome()

  browser=webdriver.Firefox()

  url='http://www.baidu.com'

  #通過get方法獲取當前url打印

  print("now access %s" %(url))

  browser.get(url)

  time.sleep(2)

   browser.find_element_by_xpath("//input[@id='kw']").send_keys('Selenium')

  browser.find_element_by_id('su').click()

  # browser.find_element_by_css_selector("input[value$='下']")

  time.sleep(3)

  print("成功搜索")

  browser.quit()

  以上代碼如果能正常運行說明環境配置成功

  PS:以下三個問題是安裝過程中常遇到的坑

  遇到第一個坑:'geckodriver' executable needs to be in PATH

  1.如果啟動瀏覽器過程中報如下錯誤

  Traceback (most recent call last):

  File "<stdin>", line 1, in <module>

  File "D:\test\python3\lib\site-packages\selenium\webdriver\firefox\webdriver.py", line 145, in __init__

  self.service.start()

  File "D:\test\python3\lib\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.

  2.這個是因為最新的selenium3.0啟動firefox需要geckodriver.exe這個驅動文件。

  3.下載之后,配置到環境變量path下(可以直接放python根目錄)

  遇到第二坑:Expected browser binary location, but unable to find binary in default location

  1.如果啟動瀏覽器過程中報如下錯誤

  Traceback (most recent call last):

  File "<stdin>", line 1, in <module>

  File "D:\test\python3\lib\site-packages\selenium\webdriver\firefox\webdriver.py", line 155, in __init__

  keep_alive=True)

  File "D:\test\python3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 92, in __init__

  self.start_session(desired_capabilities, browser_profile)

  File "D:\test\python3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 179, in start_session

  response = self.execute(Command.NEW_SESSION, capabilities)

  File "D:\test\python3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 238, in execute

  self.error_handler.check_response(response)

  File "D:\test\python3\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 193, in check_response

  raise exception_class(message, screen, stacktrace)

  selenium.common.exceptions.WebDriverException: Message: Expected browser binary location, but unable to find binary in default location, no 'moz:firefoxOptions.binary' capability provided, and no binary flag set on the command line

  2.這個是因為firefox.exe這個文件也需要配置到環境變量path下

  3.這個路徑就是安裝完firefox后,找到firefox.exe這個文件的地址,加到path下

 

  遇到第三坑:Unsupported Marionette protocol version 2, required 3

  1.如果啟動瀏覽器過程中出現如下錯誤

  Traceback (most recent call last):

  File "<stdin>", line 1, in <module>

  File "D:\test\python3\lib\site-packages\selenium\webdriver\firefox\webdriver.py", line 155, in __init__

  keep_alive=True)

  File "D:\test\python3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 92, in __init__

  self.start_session(desired_capabilities, browser_profile)

  File "D:\test\python3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 179, in start_session

  response = self.execute(Command.NEW_SESSION, capabilities)

  File "D:\test\python3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 238, in execute

  self.error_handler.check_response(response)

  File "D:\test\python3\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 193, in check_response

  raise exception_class(message, screen, stacktrace)

  selenium.common.exceptions.WebDriverException: Message: Unsupported Marionette protocol version 2, required 3

  2.這個錯誤原因是firefox版本過低了,最新的selenium3.0版本支持firefox47以上的版本,升級版本就可以了


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM