第一个python自动化实例

本文详细介绍了如何在Windows环境下安装Python、验证pip、安装selenium以及下载对应版本的ChromeDriver。通过设置环境变量确保执行路径正确,并编写了一个简单的自动化测试脚本`test.py`,该脚本访问百度并进行搜索操作。最后,指导读者如何在CMD中运行这个测试脚本。
  1. 下载python安装

  2. 验证是否安装pip

python -m pip --version
输出如下,表示安装
pip 19.2.3 from C:\_Program\Python27\lib\site-packages\pip (python 2.7)
  1. 安装selenium
pip install selenium
  1. 查看chrome版本,然后到https://chromedriver.storage.googleapis.com/index.html下载对应的win32.zip
    解压zip文件,放在chrome浏览器根目录下
    验证是否安装:直接在cmd目录下执行chromedriver即可

  2. 配置环境变量,将chrome根目录和python根目录添加到环境变量中。

  3. 添加test.py文件,代码如下

# coding=utf-8

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

class TestTest01():
  def __init__(self):
    self.driver = webdriver.Chrome()
    self.vars = {}

    def over(self):
        self.driver.quit()

	def test_test01(self):
		self.driver.get("https://www.baidu.com/")
		self.driver.set_window_size(1088, 816)
		self.driver.find_element(By.ID, "kw").send_keys("selenium")
		self.driver.find_element(By.ID, "kw").send_keys(Keys.ENTER)

obj = TestTest01()
obj.test_test01()
# obj.over()
  1. cmd窗口运行命令
python test.py
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值