安装selenium:
python -m pip install selenium-3.4.3-py2.py3-none-any.whl
下载对应浏览器版本的驱动,且在环境变量PATH中指定驱动程序的存放路径,否则需要指定驱动文件路径。
代码举例:
# selenium登录操作举例 import os, time
from selenium import webdriver # 指定浏览器安装位置
chrome_path = "C:\\Users\\nriet\AppData\Local\Google\Chrome\Application\chrome.exe"
os.environ["webdriver.chrome.bin"] = os.path.abspath(chrome_path)
# 指定浏览器驱动路径
driver_path = "D:\\Program Files\\Python36\\chromedriver.exe"
driver = webdriver.Chrome(executable_path=driver_path)
time.sleep(1) # 模拟登录操作
driver.get("http://192.168.20.179:8080/YZWXTQ/login.do")
driver.maximize_window()
driver.find_element_by_id('name').send_keys('username')
driver.find_element_by_id('password').send_keys('password')
time.sleep(2)
driver.find_element_by_id('login-btn').click()
time.sleep(3)
driver.quit()