Python 安装 selenium 与 chromedriver.exe

  1. Python安装selenium
    进入cmd,输入:pip3 install selenium (有时受到用户权限限制,输入:pip3 install --user selenium)

  2. 安装chromedriver.exe
    在链接 http://chromedriver.storage.googleapis.com/index.html 选择合适版本的chromedriver.exe,
    下载chromedriver_win32.zip,即使电脑中安装的是x64的chrome浏览器也没有任何关系。
    解压后将chromedriver.exe放到chrome浏览器的安装路径中(例如:C:Program Files (x86)GoogleChromeApplication)
    并将该安装路径添加到系统环境变量Path中

  3. 在编写test_selenium.py代码,内容如下,

# coding=utf-8
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.keys import Keys
import time
browser = webdriver.Chrome() # Get local session of Chrome
browser.get("http://www.yahoo.com") # Load page
assert "Yahoo!" in browser.title
elem = browser.find_element_by_name("p") # Find the query box
elem.send_keys("seleniumhq" + Keys.RETURN)
time.sleep(0.2) # Let the page load, will be added to the API
try:
    browser.find_element_by_xpath("//a[contains(@href,'http://seleniumhq.org')]")
except NoSuchElementException:
    assert 0, "can't find seleniumhq"
    browser.close()

运行test_selenium.py,如果安装正确,则会开启chrome浏览器,并打开了http://www.yahoo.com页面

原文地址:https://www.cnblogs.com/lqqgis/p/12641540.html