Python+selenium环境搭建及简单实例(包含Pytest测试框架)

目录:
1、安装Python环境及IDE
2、下载浏览器驱动及环境配置
3、创建项目写入代码并执行(包含pytest测试框架创建应用的示例以及基本的python file格式示例)

一、安装Python环境及IDE

1、详情 查看如下文章:https://blog.csdn.net/Eayonz/article/details/106469500
(1)更新Python pip,以及展示selenium版本指令如下:
python -m pip install --upgrade pip
pip show selenium(我这边的Firefox已经装了ide插件,若使用指令pip show selenium不能展示版本号,请查看网上Python pip安装selenium的方法步骤或者寻找其他方法)
在这里插入图片描述

二、下载浏览器驱动及环境配置

1、这里以火狐浏览器驱动为例,地址如下:
下载地址:https://github.com/mozilla/geckodriver/releases
在这里插入图片描述
(1)下载完成后解压即可使用
在这里插入图片描述
补充: 谷歌浏览器驱动链接:http://npm.taobao.org/mirrors/chromedriver/
在这里插入图片描述

2、将浏览器驱动放置Python目录下的Scripts文件下内,具体路径如下:
C:\Users\Administrator\AppData\Local\Programs\Python\Python36\Scripts(此处的Python是默认路径安装的)
在这里插入图片描述
(1)如有需要,请将webDriver驱动存放路径,加入到path环境变量,webDriver 驱动应与浏览器版本相符。
在这里插入图片描述

三、创建项目写入代码并执行

1、pytest示例

可以用ide录制脚本,将脚本导入到pycharm中(此处录制的脚本适用于python的测试模块pytest,需要在代码中导入pytest包,在pycharm中鼠标移至import pytest点击导入包即可自动下载安装)。
补充:
pycharm中如何选择pytest框架
(1)新建一个工程后,左上角file->Setting->Tools->Python Integrated Tools->项目名称->Default test runner->选择py.test
在这里插入图片描述
(2)在项目中设置好了pytest运行模式,重新创建一个.py文件,写入python的pytest代码,运行即可。

(3)示例代码:

import pytest
import time
import json
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 TestTestss():
  def setup_method(self, method):
    self.driver = webdriver.Firefox()
    self.vars = {}

  def teardown_method(self, method):
    self.driver.quit()

  def test_testss(self):
    self.driver.get("http://xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
    self.driver.set_window_size(1889, 980)
    self.driver.find_element(By.CSS_SELECTOR, "#account .ivu-input").click()
    self.driver.find_element(By.CSS_SELECTOR, ".ivu-input:nth-child(3)").send_keys("cc")
    self.driver.find_element(By.CSS_SELECTOR, ".ivu-input:nth-child(2)").click()
    self.driver.find_element(By.CSS_SELECTOR, "#psw .ivu-input").send_keys("123456")
    self.driver.find_element(By.CSS_SELECTOR, ".ivu-btn").click()
    element = self.driver.find_element(By.CSS_SELECTOR, ".ivu-btn")
    actions = ActionChains(self.driver)
    actions.move_to_element(element).perform()
    element = self.driver.find_element(By.CSS_SELECTOR, "body")
    actions = ActionChains(self.driver)
    actions.move_to_element(element, 0, 0).perform()
    self.driver.find_element(By.CSS_SELECTOR, ".center:nth-child(9) > .title > div:nth-child(1)").click()
    self.driver.find_element(By.CSS_SELECTOR, ".el-table__fixed-body-wrapper .el-table__row:nth-child(1) .el-button:nth-child(1) > span").click()
    self.driver.find_element(By.CSS_SELECTOR, "textarea").click()
    self.driver.find_element(By.CSS_SELECTOR, "textarea").send_keys("111111")
    self.driver.find_element(By.CSS_SELECTOR, ".button").click()

2、简单的python file

操作如下:登录网站,输入账号密码,选择反馈管理按钮,选择回复,输入信息并发送给反馈的人。循环4次。

import pytest
import time
import json

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


from selenium import webdriver  # 引入包

i = 1
while i < 5:
    driver = webdriver.Firefox()  # 实例化浏览器

    driver.get("http://xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")  # 打开网页
    driver.find_element(By.CSS_SELECTOR, "#account .ivu-input").click()
    driver.find_element(By.CSS_SELECTOR, "#account .ivu-input").send_keys("cc")
    driver.find_element(By.CSS_SELECTOR, ".ivu-input:nth-child(2)").click()
    driver.find_element(By.CSS_SELECTOR, "#psw .ivu-input").send_keys("123456")
    driver.find_element(By.CSS_SELECTOR, ".ivu-btn").click()# 点击登录按钮
	time.sleep(2)#强制停留2s
	driver.find_element(By.CSS_SELECTOR, ".center:nth-child(9) > .title > div:nth-child(1)").click()
    time.sleep(2)#强制停留2s
    driver.find_element(By.CSS_SELECTOR,
                    ".el-table__fixed-body-wrapper .el-table__row:nth-child(1) .el-button:nth-child(1) > span").click()
    driver.find_element(By.CSS_SELECTOR, "textarea").click()
    driver.find_element(By.CSS_SELECTOR, "textarea").send_keys("我在测试循环第%d次"%i)
    driver.find_element(By.CSS_SELECTOR, ".button").click()
    
	driver.save_screenshot('D:/a.png')#截图存放至D盘
	
    driver.quit()  # 关闭浏览器
    
    i += 1

在这里插入图片描述
在这里插入图片描述

补充:

(1)一些常用元素定位的api:

id定位:driver.find_element_by_id("xx")
name定位:driver.find_element_by_name("xx")
class定位:driver.find_element_by_class_name("xx")
tag定位:driver.find_element_by_tag_name("xx")
link定位:driver.find_element_by_link_text("xx").click()
Xpath定位:driver.find_element_by_xpath('//input[@id="kw"]')

(2)截屏操作

driver.save_screenshot('D:/a.png')#截图存放至D盘
  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值