赞
踩
目录:
1、安装Python环境及IDE
2、下载浏览器驱动及环境配置
3、创建项目写入代码并执行(包含pytest测试框架创建应用的示例以及基本的python file格式示例)
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 驱动应与浏览器版本相符。
可以用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()
操作如下:登录网站,输入账号密码,选择反馈管理按钮,选择回复,输入信息并发送给反馈的人。循环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盘
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。