Selenium3.14.1+Python安装和第一个Demo

言简意赅的说下Selenium是什么

Selenium是前台测试框架,支持IE(7, 8, 9, 10, 11),Mozilla Firefox,Safari,Google Chrome等浏览器,我只试了这四个,别的大家用的时候可以试下。

用脚本可以模拟网页的点击,输入,提交,验证等操作。

可参照下列网站进行学习:

1.https://selenium-python-zh.readthedocs.io/en/latest/installation.html(中文)

2.https://selenium-python.readthedocs.io/index.html(英文)

安装/运行Demo:

1.Python官方网站下载最新版Python

   https://www.python.org/downloads/

2.通过pip下载安装Selenium

  也可以直接进入Python的Scripts目录,按住Shift+鼠标右键,选择PowerShell窗口。

  注意有提示升级Pip的时候根据命令升级你的PIP

D:>cd D:pythonScripts

D:pythonScripts>pip install selenium

 3.查看Selenium版本

 

D:pythonScripts>pip show selenium
Name: selenium
Version: 3.14.1
Summary: Python bindings for Selenium
Home-page: https://github.com/SeleniumHQ/selenium/
Author: UNKNOWN
Author-email: UNKNOWN
License: Apache 2.0
Location: d:pythonlibsite-packages
Requires: urllib3
Required-by:

D:pythonScripts>

4.下载火狐Firefox的驱动geckodriver 下载地址:https://github.com/mozilla/geckodriver/releases/

   注意版本,下载完成后将geckodriver.exe放到Python的安装目录下。我的是D:Python,然后将D:Python添加到系统的环境变量中

  

5.编写脚本

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

driver = webdriver.Firefox()
driver.get("http://www.python.org")
assert "Python" in driver.title
print (driver.title)
elem = driver.find_element_by_name("q")
elem.clear()
elem.send_keys("pycon")
elem.send_keys(Keys.RETURN)
assert "No results found." not in driver.page_source
driver.close()

6.运行Demo

D:python>python 目录	estPython.py
Welcome to Python.org

D:python>

已上只是跑一个简单的Demo,大家也可以下载Selenium的IDE进行工作更方便。

原文地址:https://www.cnblogs.com/forbetter223/p/9873176.html