python+selenium--环境搭建

环境准备(windows 7)

  1. python:3.6
  2. Selenium2.53.6
  3. Firfox46以下版本
  4. Chrome
  5. pycharm

 

python3.6安装

待更新...

Selenium安装

Python安装完成后使用pip安装,推荐版本2.53.6

pip install selenium==2.53.6

 

查看selenium

 

 Firefox版本推荐46以下

也可以尝试

Selenium3+Firefox47以上+geckdriver驱动+python3

测试Firefox环境,在pycharm中可以正常执行以下代码并打开浏览器访问网页

环境ok

from selenium import webdriver

driver = webdriver.Firefox()
driver.get('http://www.baidu.com')
driver.quit()

 

 

安装 Chrome

从官网下载最新的就可以

安装驱动Chromewebdriver.exe到python根目录

ChromeDriver与Chrome版本对应参照表及ChromeDriver下载链接

 

 测试Chrome环境

在pycharm中可以正常执行以下代码并打开浏览器访问网页

环境ok

from selenium import webdriver

driver = webdriver.Chrome()
driver.get("http://www.baidu.com")
driver.quit()

 

操作浏览器

from selenium import webdriver
import time

driver = webdriver.Firefox()
#打开百度页面
driver.get('http://www.baidu.com')
#强制等待3s
time.sleep(3)
#刷新浏览器
driver.refresh()
#关闭当前窗口
driver.close()
#退出浏览器,清空临时文件
driver.quit()

 

注意:测试完成后要用quit()退出浏览器,要不然C盘临时文件越来越多

Chrome遇到问题:Python is likely shutting down

Exception ignored in: <bound method Service.__del__ of <selenium.webdriver.chrome.service.Service object at 0x000000000269FF28>>
Traceback (most recent call last):
File "D:\python36\lib\site-packages\selenium\webdriver\common\service.py", line 163, in __del__
File "D:\python36\lib\site-packages\selenium\webdriver\common\service.py", line 139, in stop
File "D:\python36\lib\site-packages\selenium\webdriver\common\service.py", line 110, in send_remote_shutdown_command
ImportError: sys.meta_path is None, Python is likely shutting down

 

 解决:在代码后边添加driver.quit()即可

浏览器加载本地配置

 Firefox

from selenium import webdriver

#配置文件地址
profile_directory = r'xxx'
#加载配置文件
profile = webdriver.FirefoxProfile(profile_directory)
#启动浏览器配置文件
driver = webdriver.Firefox(profile)

 

配置文件在

点击 '帮助' ---->'故障排除信息'

 

点击显示文件就可以跳转到配置路径

 

 

Chrome加载配置

 

posted @ 2020-09-20 21:03  莫使娇躯空对月  阅读(196)  评论(0编辑  收藏  举报