美文网首页
学习日记8-1(selenium环境搭建)

学习日记8-1(selenium环境搭建)

作者: 不知名的二狗 | 来源:发表于2019-08-24 20:30 被阅读0次

一、环境搭建(selenium2.53.6+python3.6+chrom70+chromdriver)

1.安装python
2.安装selenium(安装版本不匹配容易报错)

pip install selenium==2.53.6

cmd下测试(或者在pycharm中输入以下代码也可测试)

>>> from selenium import webdriver
>>> driver = webdriver.Chrome()
>>> driver.get("https://www.baidu.com")

如果报错

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "E:\Program Files\python3.6\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 62, in __init__
    self.service.start()
  File "E:\Program Files\python3.6\lib\site-packages\selenium\webdriver\common\service.py", line 71, in start
    os.path.basename(self.path), self.start_error_message)
selenium.common.exceptions.WebDriverException: Message: 'chromedriver' executable needs to be in PATH. Please see https://sites.google.com/a/chromium.org/chromedriver/home

查询自己的chrom浏览器版本,下载相应的chromdriver,并放在与python.exe相同的目录下(确定此时python已经配置了环境变量),运行成功。
chromdrive地址:http://chromedriver.storage.googleapis.com/index.html
3.浏览器操作
本来想使用firefox安装firebug和firepath,但是新版本和旧版本的firefox都搜索不到firebug,于是就用了Chrom安装了firebug lite和chropath。

  • 打开百度
driver = webdriver.Chrome()
driver.get("https://www.baidu.com")
  • 休眠5s,打开谷歌主页
import time
time.sleep(5)
driver.get("https://www.google.com")
  • 后退
time.sleep(5)
driver.back()
  • 前进
time.sleep(5)
driver.forward()
  • 刷新
driver.refresh()
  • 退出
driver.close() # 关闭当前窗口
driver.quit() # 退出浏览器,清空临时文件,不然C盘文件会爆炸

4.Chrom加载配置文件
firefox的配置文件路径需要去查看,Chrom浏览器都是一样的:C:\Users%username%\AppData\Local\Google\Chrome\User Data\Default

profile_directory = r"C:\Users\cc\AppData\Local\Google\Chrome\User Data\Default"
option = webdriver.ChromeOptions()
option.add_argument(profile_directory)
driver = webdriver.Chrome(chrome_options=option)
driver.get("http://www.baidu.com")

相关文章

网友评论

      本文标题:学习日记8-1(selenium环境搭建)

      本文链接:https://www.haomeiwen.com/subject/qbefectx.html