1壹若、Selenium安裝
pip3 install selenium chromedriver_installer
2、編輯我的第一個(gè)測(cè)試腳本
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""A very basic selenium example."""
from selenium import webdriver
browser = webdriver.Chrome()
browser.get("http://www.baidu.com")
browser.find_element_by_id("kw").send_keys("selenium")
browser.find_element_by_id("su").click()
browser.quit()
3、首次運(yùn)行報(bào)錯(cuò)
調(diào)用chrome報(bào)錯(cuò)信息:
Traceback (most recent call last):
File "/usr/local/lib/python3.7/site-packages/selenium/webdriver/common/service.py", line 76, in start
stdin=PIPE)
File "/usr/local/Cellar/python/3.7.0/Frameworks/Python.framework/Versions/3.7/lib/python3.7/subprocess.py", line 756, in __init__
restore_signals, start_new_session)
File "/usr/local/Cellar/python/3.7.0/Frameworks/Python.framework/Versions/3.7/lib/python3.7/subprocess.py", line 1499, in _execute_child
raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: 'chromedriver': 'chromedriver'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "test1.py", line 8, in <module>
browser = webdriver.Chrome()
File "/usr/local/lib/python3.7/site-packages/selenium/webdriver/chrome/webdriver.py", line 73, in __init__
self.service.start()
File "/usr/local/lib/python3.7/site-packages/selenium/webdriver/common/service.py", line 83, in start
os.path.basename(self.path), self.start_error_message)
selenium.common.exceptions.WebDriverException: Message: 'chromedriver' executable needs to be in PATH. Please see https://sites.google.com/a/chromium.org/chromedriver/home
意思是說(shuō)沒(méi)有找到chromedriver拣技,或者不在PATH中函匕。
換成調(diào)用Firefox也會(huì)有類(lèi)似的報(bào)錯(cuò)信息:
FileNotFoundError: [Errno 2] No such file or directory: 'geckodriver': 'geckodriver'
selenium.common.exceptions.WebDriverException: Message: 'geckodriver' executable needs to be in PATH.
在網(wǎng)上查了一下娱据,有很多人都遇到了這個(gè)問(wèn)題,這確實(shí)是學(xué)習(xí)selenium的第一個(gè)攔路虎盅惜,第一關(guān)就過(guò)不去中剩。
4、解決辦法
直接用brew安裝chromedriver
brew install chromedriver
等了半天沒(méi)有反應(yīng)酷窥,因?yàn)閏hrome是那誰(shuí)家的咽安,在這里克服第二關(guān),你會(huì)看到:
提示:
Error: No available formula with the name "chromedriver"
It was migrated from homebrew/core to homebrew/cask.
You can access it again by running:
brew tap homebrew/cask
And then you can install it by running:
brew cask install chromedriver
按照提示再次輸入:
brew tap homebrew/cask
brew cask install chromedriver
chromedriver was successfully installed!
搞定了Chrome蓬推,再裝Firefox所需的geckodriver
brew install geckodriver
5妆棒、運(yùn)行通過(guò)
再次運(yùn)行腳本,可以正常的看到chrome打開(kāi)了百度,執(zhí)行了搜索糕珊,最后關(guān)閉了瀏覽器动分。