自动化强化训练

1、用python代码获取当前操作系统名称,平台以及版本信息

      import platform
    import os
    print(os.name)
    print(platform.system())
    print(platform.release())

2、使用Python获取指定文件的扩展名,如abc.java的扩展名是java,s1.txt.bak的扩展名是bak

  filename = input("Input the Filename: ")
  f_extns = filename.split(".")[-1]
  print('扩展名:%s'%f_extns)

3、使用python检查给定文件路径是普通文件还是文件夹

  import os
path="abc.txt"
if os.path.isdir(path):
print("\nIt is a directory")
elif os.path.isfile(path):
print("\nIt is a normal file")
else:
print("It is a special file (socket, FIFO, device file)" )
print()

4、使用python列出自定义目录的所有文件(不包括子目录)

  from os import listdir
from os.path import isfile, join
dst_dir=r'C:\Users\Administrator\Downloads'
files_list = [f for f in listdir(dst_dir) if isfile(join(dst_dir, f))]
[print(f) for f in files_list]

 

5、使用python列出指定目录的所有文件(包括子目录)

  from os import listdir
from os.path import isfile, join,isdir
dst_dir=r'C:\Users\Administrator\Downloads'

files_list=[]
def list_files(path):
for f in listdir(path):
if isdir(join(path,f)):
list_files(join(path,f))
elif isfile(join(path,f)):
files_list.append(f)

list_files(dst_dir)
[print(f) for f in files_list]

 

6、使用python程序统计字符串中出现指定字符的次数 如"This is a String" 统计i出现的次数为3

   s = "The quick brown fox jumps over the lazy dog."
print(s.count("q"))

7、编写一个程序,找出 2000 到 3200之间 所有课被7整除,但是 不能被5整除的数字 打印结果 数字之间用逗号隔开

   # 使用range(#begin, #end)方法
  l=[]
  for i in range(2000, 3201):
  if (i%7==0) and (i%5!=0):
  l.append(str(i))
      print( ','.join(l))

 

8、定义一个类有如下两个方法: getString: 从终端获取用户输入字符串保存在一个属性中 printString: 将用户输入的字符串打印出来, 并且字符串中的英文字母区别转成大写. 然后再写一段代码,测试这个类和两个方法.

 

class InputOutString(object):
def __init__(self):
self.s = ""

def getString(self):
self.s = input()

def printString(self):
print( self.s.upper())

strObj = InputOutString()
strObj.getString()
strObj.printString()

 

9、写一个程序,让用户输入字符串, 然后计算出该字符串中 数字有几个 字母有几个。 比如用户输入: hello world! 123 该程序应该打印结果为: LETTERS 10 DIGITS 3

   s = input()
d={"DIGITS":0, "LETTERS":0}
for c in s:
if c.isdigit():
d["DIGITS"]+=1
elif c.isalpha():
d["LETTERS"]+=1
else:
pass
print( "LETTERS", d["LETTERS"])
print( "DIGITS", d["DIGITS"])

10、写一个程序,计算某个用户 在银行账户的 余额。

比如用户输入:Write a program that computes the net amount of a bank account based a transaction log from console input.

用户输入的格式如下:
D 100
W 200

D 表示存钱, W 表示 取钱.

用户每次输入一行,比如,用户输入如下4行:
D 300
D 300
W 200
D 100
输出结果应该是:
500
注意,用户输入空行表示 不再输入,程序就可以进行计算了。

 netAmount = 0
while True:
s = input()
if not s:
break
values = s.split(" ")
operation = values[0]
amount = int(values[1])
if operation=="D":
netAmount+=amount
elif operation=="W":
netAmount-=amount
else:
pass
print( netAmount)

11、写一个程序,用户输入起始日期, 该程序可以计算后续的 120天中, 哪些是周 1, 周3, 周5, 周日。 并且把这些天打印出来。 关于日期的相关计算,大家自己从网上查询资料

  from datetime import timedelta, date

# 打印出每周的1357是几号
def printLessonDay():
start_date = date(2019,4,20)
for oneday in (start_date + timedelta(n) for n in range(120)):
wd = oneday.weekday() + 1
if wd in [1,3,5,7]: # [1,2,4,6]
print (f'{oneday} : 星期{wd}')
printLessonDay()

 

12、从百度网盘如下日志文件中,找出所有的包含 关键字 Error 的行,并且同时打印出 该行 前后各10行内容

with open(‘xxxx’) as f:
lines=f.readlines()
[print(lines[lines.index(line)-10:lines.index(line)+11]) for line in lines if ‘Error’ in line]

 

 

13、请写一个程序,访问https://www.51job.com/ 抓取关键字python,地点杭州的工作中,前3页的职位中,月薪在10000元以上的工作

  from selenium import webdriver
import time

driver = webdriver.Chrome(r"d:\tools\webdrivers\chromedriver.exe")
driver.implicitly_wait(10)

driver.get('http://www.51job.com')

driver.find_element_by_id('kwdselectid').send_keys('python')
driver.find_element_by_id('work_position_input').click()

time.sleep(1)

# 选择城市,去掉非杭州的,选择杭州
selectedCityEles = driver.find_elements_by_css_selector(
'#work_position_click_center_right_list_000000 em[class=on]')

for one in selectedCityEles:
one.click()

driver.find_element_by_id('work_position_click_center_right_list_category_000000_080200').click()

# 保存城市选择
driver.find_element_by_id('work_position_click_bottom_save').click()
driver.find_element_by_css_selector('div.ush > button').click()

time.sleep(1)

# 搜索结果分析
for i in range(3):
jobs = driver.find_elements_by_css_selector('#resultList div[class=el]')
for job in jobs:
fields = job.find_elements_by_tag_name('span')
stringFilelds = [field.text for field in fields]

14、按照以下用例要求完成selenium代码,若密码不正确自行注册,公共账号供大家学习,请勿修改。1、打开移动端网站 http://a.4399en.com/ 2、登录,账号/密码为是: gfc@qq.com/123456 3、登录后,断言是否已经处于登录状态 4、关闭浏览器

编写脚本 login_success.py
# coding=utf-8

from selenium import webdriver
from selenium.webdriver.common.by import By

from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time

# 创建Chrome驱动实例,这里创建driver时,传入chrome_options参数,告诉服务器,我是用移动端浏览器访问的。
options = webdriver.ChromeOptions()
options.add_argument('User-Agent=Mozilla/5.0 (Linux; U; Android 4.0.2; en-us; Galaxy Nexus Build/ICL53F) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30')
driver = webdriver.Chrome(chrome_options=options)

# 设置浏览器大小,让它看起来跟手机的样式差不多。
driver.set_window_size("380", "680")

# 设置一个全局的等待超时时间 10s
driver.implicitly_wait(10)

# 打开首页
driver.get("http://a.4399en.com/")

# 点击右上角登录按钮打开登录窗口
driver.find_element_by_class_name("CNlogin").click()

# 在用户名输入框中输入正确的用户名
wait = WebDriverWait(driver,20,0.5)

account = wait.until(EC.visibility_of_element_located((By.ID,"modify-account")))
# account = driver.find_element_by_id("modify-account"

 

 15、按照以下用例要求完成selenium代码,若密码不正确自行注册,公共账号供大家学习,请勿修改。1、打开移动端网站 http://a.4399en.com/ 2、登录,账号/密码为是: gfc@123.com/123456 3、断言是否弹窗提示“User does not exist!” 4、关闭弹窗 5、关闭浏览器
 
编写脚本 login_fail.py:
# coding=utf-8

from selenium import webdriver
from selenium.webdriver.common.by import By

from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time

# 创建Chrome驱动实例,这里创建driver时,传入chrome_options参数,告诉服务器,我是用移动端浏览器访问的。
options = webdriver.ChromeOptions()
options.add_argument('User-Agent=Mozilla/5.0 (Linux; U; Android 4.0.2; en-us; Galaxy Nexus Build/ICL53F) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30')
driver = webdriver.Chrome(chrome_options=options)

# 设置浏览器大小,让它看起来跟手机的样式差不多。
driver.set_window_size("380", "680")

# 设置一个全局的等待超时时间 10s
driver.implicitly_wait(10)

# 打开首页
driver.get("http://a.4399en.com/")

# 点击右上角登录按钮打开登录窗口
driver.find_element_by_class_name("CNlogin").click()

# 在用户名输入框中输入正确的用户名
wait = WebDriverWait(driver, 20, 0.5)

account = wait.until(EC.visibility_of_element_located((By.ID, "modify-account")))
# account = driver.find_element_by_id("modify-account
 
16、获取开发者头条精选文章的前100篇文章标题,依次打印出来
  # 配置要获取多少条文章的标题
NUM = 100

from appium import webdriver
import time,traceback

desired_caps = {
'platformName': 'Android',
'platformVersion': '9',
'deviceName': 'xxx',
'automationName': 'UIAutomator2',
'appPackage': 'io.manong.developerdaily',
'appActivity': 'io.toutiao.android.ui.activity.LaunchActivity',
# 'unicodeKeyboard': True,
# 'resetKeyboard': True,
'noReset': True,
'newCommandTimeout': 6000,
}


#启动Remote RPC
driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)
driver.implicitly_wait(10)



try:

allTitles = []
driver.find_element_by_id("home_feature_recycler_view")
time.sleep(1)

screenSize = driver.get_window_size()
screenW = screenSize['width']
screenH = screenSize['height']
print(screenW,screenH)

x = screenW / 2
y1 = int(screenH * 0.8)
y2 = int(screenH * 0.4)


while True:
pageTitleEles = driver.find_elements_by_id("tv_title")
pageTitles = [one.text for o

 

posted @ 2022-03-14 16:25  大熊童鞋  阅读(77)  评论(0编辑  收藏  举报