By类一共有8种元素定位方式,它们都是静态方法:
- a = [1, 6, 7, 4, 4, 5, 4, 5, 4, 5, 5, 6, 7, 8, 5, 6, 7, 3, 4, 2, 2, 1, 4, 8, 9, 4, 5, 6]
-
-
- def get_datas(a):
- result = []
- data_dict = {}
- # 键值对:键:数字,值:在列表中的次数
- for item in set(a):
- data_dict[str(item)] = a.count(item)
- print(data_dict)
- # 将键值对按值(数字出现的次数)排序 ---从高到低排序
- res = sorted(data_dict.values(),reverse=True)
- for num in res:
- for key,value in data_dict.items():
- # 如果值在列表中不存在,则添加到结果列表中
- if num == value and key not in result:
- result.append(key)
-
- return result
-
-
- a1 = get_datas(a)
执行结果:
- users = {"admin": "123456", "user1": " 123456"}
- count = 0
-
-
- def login():
- global count
- username = input("请输入用户名:")
- if username == None or username == "":
- login()
- if username not in users.keys():
- print("用户名输入不正确,请重新输入用户名:")
- login()
-
- while (count < 3):
- passwd = input("请输入密码:")
- if passwd == users[username]:
- print("登录成功!!")
- count = 3
- else:
- count += 1
- print("密码输入错误,您还有{0}次机会。".format(3 - count))
-
-
- login()
1、能在当前目录以及当前目录的所有子目录下查找文件名包含指定字符串的文件;2、并打印出相对路径。
-
- import os
-
- paths = []
-
-
- def get_paths(dir, relate_dir=None, search_str=None):
- global paths
- if search_str == None:
- return os.listdir(dir)
-
- for item in os.listdir(dir):
- if relate_dir == None:
- relate_dir == os.curdir
-
- if os.path.isfile(os.path.join(dir, item)):
- if item.find(search_str) != -1:
- paths.append(os.path.join(relate_dir, item))
- elif os.path.isdir(os.path.join(dir, item)):
- paths = get_paths(os.path.join(dir, item), os.path.join(relate_dir, item), search_str)
-
- return paths
-
-
- dir = os.getcwd()
- search_str = "fun"
- print(get_paths(dir, search_str=search_str))
-
- def f(str1, *args, **kwargs):
- print(str1, args, kwargs)
-
-
- l = [1, 2, 3]
- t = [4, 5, 6]
- d = {"a": 7, "b": 8, "c": 9}
-
- f(1, 2)
- f(1, 2, 3, "python")
- f("python", l, d)
- f("python", *t)
- f("python", *l, **d)
- f("python", q="winning", **d)
执行结果
1 (2,) {} 1 (2, 3, 'python') {} python ([1, 2, 3], {'a': 7, 'b': 8, 'c': 9}) {} python (4, 5, 6) {} python (1, 2, 3) {'a': 7, 'b': 8, 'c': 9} python () {'q': 'winning', 'a': 7, 'b': 8, 'c': 9}
-
- import copy
-
- a = [1, 2, 3, 4, ["a", "b"]]
- b = a
- c = copy.copy(a)
- d = copy.copy(a)
- a.append(5)
- a[4].append("c")
-
- # 请根据以上规则填写一下输出内容
- print('a=', b)
- print('b=', b)
- print('c=', c)
- print('d=', d)
执行结果:
a= [1, 2, 3, 4, ['a', 'b', 'c'], 5] b= [1, 2, 3, 4, ['a', 'b', 'c'], 5] c= [1, 2, 3, 4, ['a', 'b', 'c']] d= [1, 2, 3, 4, ['a', 'b', 'c']]
keys = ["A", "B", "C"] values = ["1", "2", "3"] print(dict(zip(keys, [int(x) for x in values])))
list_1 = ["a", "b", "c", "1", "A", "winning"] list_2 = ["a", "python", "string"] print(set(list_1 + list_2)) # 执行结果:{'c', 'winning', '1', 'string', 'b', 'a', 'python', 'A'}
a = [{"x": 1, "y": 2}, {"x": 2, "y": 3}, {"x": 3, "y": 4}] aa1 = sorted(a, key=lambda item: item["x"], reverse=True) print(aa1) # 执行结果:[{'x': 3, 'y': 4}, {'x': 2, 'y': 3}, {'x': 1, 'y': 2}]
-
- <html>
- <head>
- <title>两行量列的表格</title>
- -<head>
- <body>
- <tatle width="200" border="1">
- <tr><td> </td>
- <td> </td>
- -</tr>
- <td> </td>
- <td> </td>
- -</tr>
- -</table>
- -</body>
-
- </html>
print("\"let's go\",she said")
-
- import random
-
- fs = open("num.txt", "a")
- list1 = []
- for index in range(10):
- num = random.randint(0, 10)
- list1.append(str(num))
-
- print(list1)
- fs.write(",".join(list1))
- fs.close()
执行结果:
- a = 1
-
-
- def fun(a):
- a = 2
-
-
- fun(a)
- print(a)
- # 执行结果:1
===============
-
- b = []
-
-
- def fun(b):
- b.append(1)
-
-
- fun(b)
- print(b)
-
- 执行结果:[1]
Token之所以可以用来做鉴权,原理如下:
常用注解:
熟悉的工具:
Get :
Post :
强制等待:
隐式等待:
显式等待:
-
- def f(x, l=[]):
- for i in range(x):
- l.append(i * i)
- print(l)
- f(2)
- f(3, [3, 2, 1])
- f(3)
-
- 结果:
- [0]
- [0, 1]
- [3, 2, 1, 0]
- [3, 2, 1, 0, 1]
- [3, 2, 1, 0, 1, 4]
- [0, 1, 0]
- [0, 1, 0, 1]
- [0, 1, 0, 1, 4]
使用ActionChains类
ac= ActionChains(driver);
ac.context _click(元素对象).perform()
- for i in range(1, 1000):
- if i % 7 == 0 and i % 5 ==3:
- print(i)
-
-
- 执行结果:
- 28
- 63
- 98
- 133
- 168
- ........
根据题意是要买一百本书,一百块钱,看有多少种买法(钱不一定非要花完 ,只要能买到一百本书) :三本书的价格分别为: 5,3,0.5,那么能买到最多的数学书为20本,英语书33本,语文书200本。但是也有组合的买法,所以可以通过一个三重for循环来实现, ijk分别代表购买数学,英语,语文书的数量,循环条件分别是i<=20,j<=33,k<=200,那么只要满足 i+j+k == 100,5*i+3*j+0.5*k<= 100即可。最后统计所有的组合就是买法的数量。
-
- n = 0
- for i in range(21):
- for j in range(34):
- for k in range(201):
- if 5 * i + 3 * j + k * 0.5 <= 100 and i + j + k == 100:
- n += 1
- print(n)
A、自动化测试可以完全取代手工测试
B、自动化测试可以大幅度减少测试团队的工作量
C、性能测试不能自动化
D、自动化测试能够发现大量的新缺陷
- L = ["a", "b", "c", "d", "e", "F", "g"]
-
- print(L[3])
- print(L[::5])
- print(L[::2])
-
- 执行结果:
- d
- ['a', 'F']
- ['a', 'c', 'e', 'g']
x = [1, 2] a1 = list(enumerate(x)) print(a1) # 执行结果:[(0, 1), (1, 2)]
print(sum(range(1, 10, 2))) # 各两位取一个之和:1,3,5,7,9 # 执行结果:25
小飞飞买好多瓶子,每对瓶子上会有一个相同的数字。小飞飞数了一遍自己的瓶子发现瓶子的个数是奇数N ,也就是有-个没有配对的瓶子。现在小飞飞要去再拿一个新瓶子,请问他给新瓶子要标几号才能让所有瓶子都有配对的。比如说他有七个瓶子( N=7 ),那么标号可以是:1,6,13,1,6,13,13
那么新瓶子就要标记为13,加入后就有(1,1),(6,6),(13,13),(13,13 )这4对
输入:测试数据的第一行一个正整数 N (1<=N<=1000 )表示有N个数, N保证是奇数,第二行N个自然数,每个数都小于10^9
输出:输出一行一个整数,新瓶子的号码
样例:
- def func(n, data):
- if 0 <= n <= 1000 and n % 2 == 1 and len(data) == n:
- for i in data:
- if 0 < i < (10 ** 9):
- if data.count(i) % 2 != 0:
- return i
- else:
- print("瓶子的号码值为自然数,并且小于10^9")
- else:
- print("传入的参数有误")
-
-
- res = func(7, [1, 6, 1, 6, 13, 13, 13])
- print(res)
63、用python写一个函数,将一个字符串中给定的子串字符按从小到大的输出,第一个字符的位置为0
- def my_order(s, start, len):
- # 先切片
- s = s[start:start + len]
- # 强制转换为列表
- li = list(s)
- # 排序
- li.sort()
- # 在拼接为字符串
- res = "".join(li)
- print("输出结果为:", res)
-
-
- my_order("abcedfgh", 2, 4)
-
- 结果为:cdef
64、对输入的整数数组,输出数组元素中的最大值、最大值的个数、最小值和最小值的个数
函数名称: max_ and_ min(list)
输入参数: list整数数组
输出: list整数数组,有四个值,分别表示最大值、最大值的个数、最小值和最小值的个数
- def max_and_min(a):
- b = sorted(a, reverse=True)
- max = b[0]
- max_num = b.count(max)
- min = b[-1]
- min_num = b.count(min)
- return [max, max_num, min, min_num]
-
-
- a = [5, 5, 5, 4, 3, 2, 2]
- print(max_and_min(a))
- # 执行结果:[5, 3, 2, 2]
函数名称: rpad(src,len,str)
输入参数::src原字符串, len目标字符串长度, str用来填充的字符串
输出:补齐后的字符串
示例:
- def rpad(src, length,string):
- while len(src) < length:
- for i in string:
- src += i
- if len(src) >= length:
- return "" + src
- else:
- return "" + src
-
-
- print(rpad("abcd", 10, "12")) # 10位
- print(rpad("bbbb", 11, "12")) # 11位
- print(rpad("cccc", 12, "12")) # 12位
- print(rpad("dddd", 13, "0")) # 13位
- print(rpad("ffff", 13, " ")) # 13位有空格
-
- # 执行结果:
- abcd121212
- bbbb1212121
- cccc12121212
- dddd000000000
- ffff
参考:
参考:
基于用户登陆的自动化测试思路: