1、下载Selenium库,可以使用pip install selenium
https://pypi.python.org/pypi/selenium/
2、下载驱动
Chrome: https://sites.google.com/a/chromium.org/chromedriver/downloads
Firefox: https://github.com/mozilla/geckodriver/releases
3、配置环境变量
需要将chrome和firefox的.exe路径和驱动路径均加入path中
比如我电脑中chrome.exe的路径为:
C:\Program Files (x86)\Google\Chrome\Application
chrome的驱动路径为:
E:\Program Files (x86)\chromedriver_win32
4、需要重启电脑,就okay了。
测试成功:
from selenium import webdriver
browser = webdriver.Chrome()
browser.get('http://baidu.com')
from selenium import webdriver
from bs4 import BeautifulSoup
from selenium.webdriver.common.keys import Keys
browser= webdriver.PhantomJS()
browser.get("https://baike.baidu.com/")
browser.find_element_by_tag_name("input").send_keys("Python")
browser.find_element_by_tag_name("button").click()
soup = BeautifulSoup(browser.page_source, "html.parser")
print (soup.prettify())
browser.quit()