1.Selenium 2.0是最新的版本,如果还有最新可以到官网上下载,使用pip工具安装即可
命令:pip install selenium
2.Selenium官网上拿的测试代码,如下,本来是FireFox的,被我改成Chrome,IE和FireFox也是可以的,毕竟Selenium都兼容
from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait # available since 2.4.0
from selenium.webdriver.support import expected_conditions as EC # available since 2.26.0
# Create a new instance of the Firefox driver
driver = webdriver.Chrome()
# go to the google home page
driver.get("http://www.baidu.com")
# the page is ajaxy so the title is originally this:
print driver.title
# find the element that's name attribute is q (the google search box)
inputElement = driver.find_element_by_name("q")
# type in the search
inputElement.send_keys("cheese!")
# submit the form (although google automatically searches now without submitting)
inputElement.submit()
try:
# we have to wait for the page to refresh, the last thing that seems to be updated is the title
WebDriverWait(driver, 10).until(EC.title_contains("cheese!"))
# You should see "cheese! - Google Search"
print driver.title
finally:
driver.quit()
3.要驱动这段代码,还要安装浏览器对应版本的驱动,驱动网上有很多下载地址,百度一下即可,下载后,将驱动解压到浏览器安装目录,再将目录添加Path中,测试代码既可以(如果一开始没有装驱动,但是又打开了cmd,装好驱动后需要重启cmd才可以运行py文件,否则Path不生效)
4.补充不同chrome下载驱动的链接:http://chromedriver.storage.googleapis.com/index.html(链接来源用的源码路径:Python27\Lib\site-packages\selenium\webdriver\chrome\webdriver.py 其中的注释说明:
"""
Controls the ChromeDriver and allows you to drive the browser.
You will need to download the ChromeDriver executable from
http://chromedriver.storage.googleapis.com/index.html
"""
)
5.Selenium python文档:https://selenium-python.readthedocs.io/installation.html