python做web自动化项目用到的知识点

  1. 显示django版本信息
import django
print(django.get_version())
  1. 字符串换行符也在字符的匹配中,以下输出:texxx
user_confirm = "confirm\n"
if user_confirm == "confirm":
    print("test")
else:
    print("texxx")
  1. 存入数据库的数据中存有字符\的正确输入及数据库操作,格式使用了mb4,通用性更强一些,可以存放一些图标。
import mysql.connector
conn = mysql.connector.connect(user='root', password='123456', host='localhost', port='3306',
                                   database='bank', charset='utf8mb4')
c = conn.cursor()
c.execute("select answer,rules from bank.pro_bank where problem = 'Age(inyears)';")
res = c.fetchall()
print(res)
print(res[0][0])
str_ = res[0][0]
print(str_)
str_.replace("\\","\\\\")
print(str_)
res[0][0].replace("\\","\\\\")
#res[0][0].replace("\\\\","\\")
print(res[0][0])
print(res)
conn.commit()
c.close()
conn.close()
  1. 随机取列表中的数值
import random
list1 = ['#QID4-4-label span', '#QID4-7-label span', '#QID4-5-label span', '#QID4-3-label span', '#QID4-2-label span', '#QID4-8-label span']
# 随机返回只有一个值的list
a = random.sample(list1, 1)

# 随机返回只有三个值的list,不按顺序取值,结果有:['#QID4-4-label span', '#QID4-8-label span', '#QID4-2-label span']
b = random.sample(list1, 3)
print(a)
print(b)
random.shuffle(list1)
print(list1)
# 返回list
  1. 自动化测试中,是否关闭了浏览器的测试
from selenium import webdriver

object_existed = False
driver = webdriver.Chrome()
#or driver = webdriver.Chrome()
if driver is not None:
    try:
        time.sleep(6)
        print("test")
        driver.execute_script('javascript:void(0);')
        print("执行11")
        object_existed = True
    except:
        # webdriver要求浏览器执行Javascript出现异常
        try:
            print("关闭驱动")
            driver.quit()
        finally:
            driver = None
    finally:
        pass
if not object_existed:
    print("浏览器已关闭或标签页已关闭")
if object_existed:
    print("浏览器还没有关闭")
#浏览器已关闭或标签页已关闭或其他异常
try:
    driver.get("https://www.baidu.com")
except:
    print("不能请求网页了")
  1. while循环每次列表都是重新赋值,资源都会释放,要特别注意指针的内存泄漏
i = 3
while(i):
    str = list()#str:[]
    print(len(str))
    print(id(str))
    if "lint" in str:
        print("true")
    else:
        print("false")
    str.append("lint")
    str.append("caredte")
    print(len(str))
    print(str)
    i -= 1
    if "lint" in str:
        print("true")
    #str.clear()
  1. 字符串的存储在常量区,重复使用还是使用的之前的内存地址
name = 'admin' #后面的admin值赋给变量name
name1 = name
name2 =name
print(id(name),name)
print(id(name1),name1)
print(id(name2),name2)
print("\n")
name = 'test'
print(id(name),name)
print(id(name1),name1)
print(id(name2),name2)
print("\n")
name = 'admin'
print(id(name),name)
print(id(name1),name1)
print(id(name2),name2)
  1. 更换一段字符串中特定位置的连续数字,一下模板为更新最后一个数字,输出:[‘19’, ‘12’] fasd19-18-dggfas
import re
def rreplace(self, old, new, *max):
   count = len(self)
   if max and str(max[0]).isdigit():
       count = max[0]
   return new.join(self.rsplit(old, count))
#print(re.sub('.*?([0-9]*)$', r'1', s))
#print(re.findall(".*?(\d+)*?$",s))
str_ = 'fasd19-12-dggfas'
plt = 6
buf = re.findall('\d+',str_)
print(buf)
buff = int(buf[len(buf)-1])+plt
print(rreplace(str_,buf[len(buf)-1],str(buff),1))
  1. python语音提示信息’
import pyttsx3
pt = pyttsx3.init()
#改变语速,范围为0-200,默认值为200
rate = pt.getProperty('rate')
pt.setProperty('rate', 120)
#设置语速 范围为0.0-1.0 默认值为200
pt.setProperty('volume', 0.7)
pt.say("j3hc53t"+"页面有填空题,页面需要人工操作,操作完成后点击确认按钮")
pt.runAndWait()
# 另外一个语音包
import win32com.client as win
speak = win.Dispatch("SAPI.SpVoice")
speak.Speak("Come on")
speak.Speak("7号浏览器需要人工操作。")
  1. python使用isalnum()去除特殊字符,以下输出:我select前面中文符号lawtopenglishsymbollast
strsql = "我select!(前面中文符号)🌱🌏law🌏🌱top(english symbol last)@#$%^';"
strsql = ''.join(char for char in strsql if char.isalnum())
print(strsql)
  1. 判断第一个字符是不是数字
str_title = "66test55  tttt"#两个空格
if str_title[0].isdigit() == True:
     str_title = str_title[str_title.find(" ") + 1:]
     print(str_title)#左边有一个空格,find()是从匹配到的第一个开始截断的。
     # tttt
  1. 去除字符中所有的非数字:50
import numpy as np
strsql =" £50 "
print(re.sub(u"([^\u0030-\u0039])", "", strsql))
  1. 产生随机数:第一个不包含6,第二个不包含0。
i = 20
while i:
    num = np.random.randint(2, 6)
    print(num)
    i = i-1
print(np.random.randint(-10, 0))
  1. 循环次数:前两句会直接是0。后两句向前循环,循环到0位置。
for i in range(-5):
	print(i)
for i in range(5, 0, -1):
    print(i)
  1. 不保留小数的除法:print(5//2)
  2. 遍历一串字符的方法:
strs = 'tangcorleone'
for ch in iter(strs):
    print(ch)
    time.sleep(0.2)
  1. python的replace()是按照顺序替换的,输出:!Thisquestionrequiresananswer.QuestionTitletangcorleone
str_title = "!Thisques tionrequire sananswer.Ques tionTitletangcorleone"
str_title = str_title.replace("!Thisquestionrequiresananswer.", "").replace("QuestionTitle", "").replace("\n", "").replace(" ", "")
print(str_title)
```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值