Pywinauto:找不到click()方法

时间:2018-12-19 00:28:28

标签: python pycharm automated-tests pywinauto

我正在PyCharm上使用Pywinauto开始一个项目。这是我的项目结构:

mybeautifulproject
    utils
        Utils
    pages
        LoginPage
    tests
        MyTest

这是我的Utils文件:

from __future__ import print_function
import logging
from pywinauto import actionlogger
from pywinauto import Application

import argparse

class Test:
    app = Application(backend='uia')

    def __init__(self):
        parser = argparse.ArgumentParser()
        parser.add_argument("--log", help="enable logging", type=str, required=False)
        args = parser.parse_args()

        actionlogger.enable()
        logger = logging.getLogger('pywinauto')
        if args.log:
            logger.handlers[0] = logging.FileHandler(args.log)

        self.app = Application(backend='uia').start(r'mybeautifulapp.EXE')

因此,基本上,将始终使用该文件(self.app是测试过程中经过测试的应用程序。)

我的MyTest文件:

from __future__ import print_function
from utils import Utils
from pages import LoginPage

test = Utils.Test()

class MyTest:
    loginPage= LoginPage.LoginPage(test)
    loginPage.connexion("login", "password")

最后一个LoginPage

from __future__ import print_function

class LoginPage:

    def __init__(self, test):
        self.FENETRE_AUTHENTIFICATION = test.app.window(auto_id='UserAuthentication')
        self.INPUT_NOM = self.FENETRE_AUTHENTIFICATION.child_window(auto_id="tbLogin")
        self.INPUT_MOT_DE_PASSE = self.FENETRE_AUTHENTIFICATION.child_window(auto_id="tbPassword")
        self.BTN_VALIDER = self.FENETRE_AUTHENTIFICATION.child_window(title="Mot de Passe", found_index=0)

    def connexion(self, login, password):
        self.INPUT_NOM.set_text(login)
        self.INPUT_MOT_DE_PASSE.set_text(password)
        self.BTN_VALIDER.click()

当我启动MyTest时,应用程序打开,字段已正确填写,但随后出现错误:

AttributeError: Neither GUI element (wrapper) nor wrapper method 'click' were found (typo?)

我不知道为什么无法单击该按钮。我知道已找到它,因为当我输入错误的标识符时,会出现一个错误,明确指出找不到该对象。

我想念什么?

谢谢。

1 个答案:

答案 0 :(得分:1)

如果该按钮未被识别为ButtonWrapper,则正确的方法是.invoke().select().toggle(),具体取决于按钮的类型。

要检查如何识别它,请使用self.BTN_VALIDER.wrapper_object()进行调试。此外,内置的Python函数dir()可以帮助您列出返回的包装器对象的所有可用属性。示例:

print(dir(self.BTN_VALIDER.wrapper_object()))