安装 Selenium+pycharm及配置
安装 Selenium
1、安装命令:pip install Selenium
2、查看安装版本:pip show Selenium
C:\Users\leo>pip show Selenium
Name: selenium
Version: 4.3.0
Summary: None
Home-page: https://www.selenium.dev
Author: None
Author-email: None
License: Apache 2.0
Location: d:\programdata\anaconda3\lib\site-packages
Requires: trio-websocket, urllib3, trio
Required-by:
3、查看已安装的库:pip list
4、pycharm添加该Selenium模块
报错:导入失败
原因:本地安装了几个python版本,selenium安装在的是d:\programdata\anaconda3\lib\site-packages,原来pycharm使用的不是这个版本,重新选择后就有库包
5、配置谷歌驱动包,放置到python文件夹里面d:\programdata\anaconda3
当前已使用python38版本(如果是本地装有几个版本的python,建议直接放在python路径下)
驱动包的路径:根据谷歌版本来取:http://chromedriver.storage.googleapis.com/index.html
谷歌版本经常更新,关闭自动更新,以防频繁更换驱动包
找不到谷歌更新的服务
6、方法使用不对?怎么解决,
看的书本太老,现在已经更新了源码方法,老方法:driver.find_element_by_id('kw').send_keys('selenium')
新版的驱动元素定位方法是find_element(),并且找不到By模块,所以我们首先要导入By模块
# 导入By模块from selenium.webdriver.common.by import By
使用方法:
# driver.find_element(By.定位方法,‘元素信息’)
driver.find_element(By.ID, ‘kw‘)
使用中的定位方法和普通的定位方法是一致的。
# coding = utf-8
from selenium import webdriver
from selenium.webdriver.common.by import By
driver=webdriver.Chrome()
driver.get('http://wwww.baidu.com')
driver.find_element(By.ID,'kw').send_keys('selenium')
帖子:https://blog.csdn.net/weixin_29813667/article/details/112311562
总结:安装python,pip安装selenium,安装pycharm
pip安装selenium还遇到pip版本太低,执行提示的命令即可