在Python中自动“输入”键(在Mac上)

时间:2011-06-17 22:08:17

标签: python macos automation automated-tests

我正在为应用程序创建测试自动化。我正在使用测试工具来完成大部分测试,但为了达到这一点,我首先需要在Python中自动执行一次'enter'键点击。我正在使用mac,因此pywin32不可用。有什么建议吗?

3 个答案:

答案 0 :(得分:1)

Appscript让这很简单:

from appscript import app
app('System Events').keystroke('\r')

这会将击键发送到前面的任何一个应用程序。

答案 1 :(得分:0)

环顾四周,我发现answer to your question在另一个类似于你的问题中。{/ p>

你将不得不稍微更改代码,以便它不是Ctrl-r的'Enter',但它应该很容易。

希望这有帮助!

答案 2 :(得分:0)

timeList = [
    ["apple","24/03/2019","10:00 AM","10:30 AM","11:30 AM","12:30 PM"],
    ["pear","24/03/2019","09:23 AM","07:00 PM"],
    ["orange","24/03/2019","11:57 PM"],
    ["orange","25/03/2019","07:00 AM(O)","05:00 PM","06:00 PM"]
    ]

total = []

def count_total(list):
    for i in range(len(timeList)):
        if len(timeList[i])<2:
            return False
        name = timeList[i][0]
        date = timeList[i][1]
        timeSpent = 0
        startTime = ""

        for j in range(2,len(timeList[i])):
            time = timeList[i][j].split(" ")[0]
            zone = timeList[i][j].split(" ")[1]
            if zone == "PM":
                time = time.split(':')
                if time[0]!= "12":
                    temp = int(time[0])+12
                    time = str(temp)+":"+time[1]
                else:
                    time = time[0]+":"+time[1]
            if zone.__contains__("(O)"):
                hour = int(time.split(":")[0])
                minute = int(time.split(":")[1])
                if zone.__contains__("AM"):
                    timeSpent = timeSpent + (60*hour) + minute
                elif zone.__contains__("PM"):
                    timeSpent = timeSpent + (60*hour) + minute + (12*60)

            else:
                if startTime == "":
                    startTime = time
                else:
                    time = time.split(":")
                    startTime = startTime.split(":")
                    timeMin = int(time[0])*60+int(time[1])
                    startTimeMin= int(startTime[0])*60 + int(startTime[1])
                    temp = timeMin - startTimeMin
                    timeSpent = timeSpent+temp
                    startTime = ""
        if startTime != "":
            startTime = startTime.split(':')
            startTimeMin = int(startTime[0])*60 + int(startTime[1])
            dif = 24*60 - startTimeMin
            timeSpent = timeSpent + dif
        data = [name,date,timeSpent]
        total.append(data)

if __name__ == "__main__":
    count_total(timeList)