如何在autoit的帮助下使用java从应用程序窗口中的列表中获取只读文本?

如何在autoit的帮助下使用java从应用程序窗口中的列表中获取只读文本?,java,autoit,jacob,Java,Autoit,Jacob,我打开了一个应用程序窗口。此窗口包含连接列表。我想使用java在autoIt的帮助下,从列表中唯一地检索处于只读状态的文本(即连接名)。我在列表中有两个连接。对于这个项目,我使用的工具和技术如下: 1) JavaSE8 2) AutoIt 尽管包含连接列表的窗口具有唯一ID,但列表中的单个元素没有任何ID。 因此,按ID搜索和获取所需的唯一文本不是一个选项。 我使用了autoIt的方法来获得期望的结果。 在autoIt的帮助下,在java中使用上述方法,我成功地使用了方法,即controlLis

我打开了一个应用程序窗口。此窗口包含连接列表。我想使用javaautoIt的帮助下,从列表中唯一地检索处于只读状态的文本(即连接名)。我在列表中有两个连接。
对于这个项目,我使用的工具和技术如下:
1) JavaSE8
2) AutoIt

尽管包含连接列表的窗口具有唯一ID,但列表中的单个元素没有任何ID。 因此,按ID搜索和获取所需的唯一文本不是一个选项。 我使用了autoIt的方法来获得期望的结果。 在autoIt的帮助下,在java中使用上述方法,我成功地使用了方法,即controlListViewSelectClear()、controlListViewSelectAll()。 但是我得到了方法的错误,即controlListViewGetItemCount(),controlListViewFindItem(),controlListViewIsSelected()。还有方法,即controlListViewGetText()

下面是代码示例及其输出:

public class TestAutoIt {

    /*Choose the 'JACOB' dll based on the JVM bit version.*/
    .
    .
    .
    final String APPLICATION_TITLE = "ABC";
    final String CONNECTION_NAME = "APP Connection 1";

    private AutoItX control;

    {
        /*Load the jacob dll.*/
        .
        .
        .
        control = new AutoItX();
    }

    public static void main(String[] args) {
        try {
            TestAutoIt obj = new TestAutoIt();

            /*Returns the ControlRef# of the control that has keyboard focus within a specified window.*/
            String stringFocus = obj.control.controlGetFocus(APPLICATION_TITLE);
            System.out.println("stringFocus: " + stringFocus);
            if(stringFocus == "") {
                System.out.println("Unable to get keyboard focus.");
                throw new Exception("Unable to get keyboard focus.");
            }

            /*Get total connections present in the list*/
            System.out.println("totalConnections: " + obj.control.controlListViewGetItemCount(APPLICATION_TITLE, "", stringFocus)); // Error

             /*Check if required connection is present or not in the connection list. If present, returns index position of element else -1.*/
            System.out.println("Index of desired connection: " + obj.control.controlListViewFindItem(APPLICATION_TITLE, "", stringFocus, CONNECTION_NAME, "")); // Error

            /*Check if desired connection in the list is selected or not*/
            System.out.println("Connection selected: " + obj.control.controlListViewIsSelected(APPLICATION_TITLE, "", stringFocus, CONNECTION_NAME)); // Error

            /*Get text of required connection*/ // not working
            obj.control.sleep(2000);
            System.out.println("controlListViewGetText(APPLICATION_TITLE, \"\", stringFocus, \"\", \"\"): " + obj.control.controlListViewGetText(APPLICATION_TITLE, "", stringFocus, "", ""));
            System.out.println("controlListViewGetText(APPLICATION_TITLE, \"\", stringFocus, \"0\", \"\"): " + obj.control.controlListViewGetText(APPLICATION_TITLE, "", stringFocus, "0", ""));
            System.out.println("controlListViewGetText(APPLICATION_TITLE, \"\", stringFocus, \"0\", \"0\"): " + obj.control.controlListViewGetText(APPLICATION_TITLE, "", stringFocus, "0", "0"));
            System.out.println("controlListViewGetText(APPLICATION_TITLE, \"\", stringFocus, \"0\", \"1\"): " + obj.control.controlListViewGetText(APPLICATION_TITLE, "", stringFocus, "0", "1"));
            System.out.println("controlListViewGetText(APPLICATION_TITLE, \"\", stringFocus, \"1\", \"\"): " + obj.control.controlListViewGetText(APPLICATION_TITLE, "", stringFocus, "1", ""));
            System.out.println("controlListViewGetText(APPLICATION_TITLE, \"\", stringFocus, \"1\", \"1\"): " + obj.control.controlListViewGetText(APPLICATION_TITLE, "", stringFocus, "1", "1"));
            obj.control.sleep(2000);

            /*Change view to list view*/ // not working
            obj.control.controlListViewSelectViewChange(APPLICATION_TITLE, "", stringFocus, "list"); // No error but there was no change in the view

            /*Clear list selection*/
            obj.control.controlListViewSelectClear(APPLICATION_TITLE, "", stringFocus); // Worked successfully

            /*Select All*/
            obj.control.controlListViewSelectAll(APPLICATION_TITLE, "", stringFocus, "0", "1"); // Worked successfully

        } catch (Exception e) {
            System.out.println("Exception: " + e);
            e.printStackTrace();
        }
    }
}
上述代码的输出如下:

/*Output for controlListViewGetItemCount() method*/
Exception: java.lang.IllegalStateException: getInt() only legal on Variants of type VariantInt, not 8
java.lang.IllegalStateException: getInt() only legal on Variants of type VariantInt, not 8
    at com.jacob.com.Variant.getInt(Variant.java:650)
    at autoitx4java.AutoItX.controlListViewInt(AutoItX.java:1492)
    at autoitx4java.AutoItX.controlListViewGetItemCount(AutoItX.java:1518)
    at autoit.AutoItExample2maven.TestAutoIt.main(TestAutoIt.java:88)

/*Output for controlListViewFindItem() method*/
Exception: java.lang.IllegalStateException: getInt() only legal on Variants of type VariantInt, not 8
java.lang.IllegalStateException: getInt() only legal on Variants of type VariantInt, not 8
    at com.jacob.com.Variant.getInt(Variant.java:650)
    at autoitx4java.AutoItX.controlListViewInt(AutoItX.java:1492)
    at autoitx4java.AutoItX.controlListViewFindItem(AutoItX.java:1488)
    at autoit.AutoItExample2maven.TestAutoIt.main(TestAutoIt.java:101)

/*Output for controlListViewIsSelected() method*/   
Exception: java.lang.IllegalStateException: getInt() only legal on Variants of type VariantInt, not 8
java.lang.IllegalStateException: getInt() only legal on Variants of type VariantInt, not 8
    at com.jacob.com.Variant.getInt(Variant.java:650)
    at autoitx4java.AutoItX.controlListViewInt(AutoItX.java:1492)
    at autoitx4java.AutoItX.controlListViewIsSelected(AutoItX.java:1567)
    at autoit.AutoItExample2maven.TestAutoIt.main(TestAutoIt.java:129)

/*Output for controlListViewGetText() method*/
controlListViewGetText(APPLICATION_TITLE, "", stringFocus, "", ""): 
controlListViewGetText(APPLICATION_TITLE, "", stringFocus, "0", ""): 
controlListViewGetText(APPLICATION_TITLE, "", stringFocus, "0", "0"): 
controlListViewGetText(APPLICATION_TITLE, "", stringFocus, "0", "1"): 
controlListViewGetText(APPLICATION_TITLE, "", stringFocus, "1", "")): 
controlListViewGetText(APPLICATION_TITLE, "", stringFocus, "1", "1"): 
为了解决上述错误,我参考了com.jacob.com.Variant类和com.jacob.activeX.ActiveXComponent类的源代码。 我将上述代码更新如下:

public class TestAutoIt {

    /*Choose the 'JACOB' dll based on the JVM bit version.*/
    .
    .
    .
    final String APPLICATION_TITLE = "ABC";
    final String CONNECTION_NAME = "APP Connection 1";

    private AutoItX control;

    {
        /*Load the jacob dll.*/
        .
        .
        .
        control = new AutoItX();
    }

    public static void main(String[] args) {
        try {
            TestAutoIt obj = new TestAutoIt();

            /*Returns the ControlRef# of the control that has keyboard focus within a specified window.*/
            String stringFocus = obj.control.controlGetFocus(APPLICATION_TITLE);
            System.out.println("stringFocus: " + stringFocus);
            if(stringFocus == "") {
                System.out.println("Unable to get keyboard focus.");
                throw new Exception("Unable to get keyboard focus.");
            }

            /*Get total connections present in the list*/
            Variant vTitle1 = new Variant(APPLICATION_TITLE);
            Variant vText1 = new Variant("");
            Variant vControl1 = new Variant(stringFocus);
            Variant vCommand1 = new Variant("GetItemCount");
            Variant vFrom1 = new Variant("");
            Variant vTo1 = new Variant("");
            Variant[] params1 = new Variant[]{vTitle1, vText1, vControl1, vCommand1, vFrom1, vTo1};
            Variant variantInst1 = activeXComponentInst.invoke("ControlListView", params1);
            //System.out.println("variantInst1.toString(): " + variantInst1.toString());
            int totalConnections = Integer.parseInt(variantInst1.toString());
            System.out.println("totalConnections: " + totalConnections);

            /*Check if required connection is present or not in the connection list. If present, returns index position of element else -1.*/
            Variant vTitle2 = new Variant(APPLICATION_TITLE);
            Variant vText2 = new Variant("");
            Variant vControl2 = new Variant(stringFocus);
            Variant vCommand2 = new Variant("FindItem");
            Variant vConnectionName2 = new Variant(CONNECTION_NAME);
            Variant vOption2 = new Variant("");
            Variant[] params2 = new Variant[]{vTitle2, vText2, vControl2, vCommand2, vConnectionName2, vOption2};
            Variant variantInst2 = activeXComponentInst.invoke("ControlListView", params2);
            System.out.println("variantInst2.toString(): " + variantInst2.toString());

            /*Check if desired connection in the list is selected or not*/
            Variant vTitle4 = new Variant(APPLICATION_TITLE);
            Variant vText4 = new Variant("");
            Variant vControl4 = new Variant(stringFocus);
            Variant vCommand4 = new Variant("IsSelected");
            Variant vConnectionName4 = new Variant(CONNECTION_NAME);
            Variant vOption42 = new Variant("");
            Variant[] params4 = new Variant[]{vTitle4, vText4, vControl4, vCommand4, vConnectionName4, vOption42};
            Variant variantInst4 = activeXComponentInst.invoke("ControlListView", params4);
            System.out.println("\nvariantInst4.toString(): " + variantInst4.toString());

            /*Get text of required connection*/
            Variant vTitle3 = new Variant(APPLICATION_TITLE);
            Variant vText3 = new Variant("");
            Variant vControl3 = new Variant(stringFocus);
            Variant vCommand3 = new Variant("GetText");
            Variant vOption31 = new Variant("0");
            Variant vOption32 = new Variant("0");
            Variant[] params3 = new Variant[]{vTitle3, vText3, vControl3, vCommand3, vOption31, vOption32};
            Variant variantInst3 = activeXComponentInst.invoke("ControlListView", params3);
            System.out.println("\nvariantInst3.toString(): " + variantInst3.toString());
            System.out.println("variantInst3.getString(): " + variantInst3.getString());

            /*Change view to list view*/ // not working
            obj.control.controlListViewSelectViewChange(APPLICATION_TITLE, "", stringFocus, "list"); // No error but there was no change in the view

            /*Clear list selection*/
            obj.control.controlListViewSelectClear(APPLICATION_TITLE, "", stringFocus); // Worked successfully

            /*Select All*/
            obj.control.controlListViewSelectAll(APPLICATION_TITLE, "", stringFocus, "0", "1"); // Worked successfully

        } catch (Exception e) {
            System.out.println("Exception: " + e);
            e.printStackTrace();
        }
    }
}
/*Output for "Get total connections present in the list"*/
totalConnections: 2

/*Output for "Check if required connection is present or not"*/
variantInst2.toString(): -1

/*Output for "Check if desired connection in the list is selected or not"*/
variantInst4.toString(): 1

/*Output for "Get text of required connection"*/
variantInst3.toString(): 
variantInst3.getString(): 
上述代码的输出如下所示:

public class TestAutoIt {

    /*Choose the 'JACOB' dll based on the JVM bit version.*/
    .
    .
    .
    final String APPLICATION_TITLE = "ABC";
    final String CONNECTION_NAME = "APP Connection 1";

    private AutoItX control;

    {
        /*Load the jacob dll.*/
        .
        .
        .
        control = new AutoItX();
    }

    public static void main(String[] args) {
        try {
            TestAutoIt obj = new TestAutoIt();

            /*Returns the ControlRef# of the control that has keyboard focus within a specified window.*/
            String stringFocus = obj.control.controlGetFocus(APPLICATION_TITLE);
            System.out.println("stringFocus: " + stringFocus);
            if(stringFocus == "") {
                System.out.println("Unable to get keyboard focus.");
                throw new Exception("Unable to get keyboard focus.");
            }

            /*Get total connections present in the list*/
            Variant vTitle1 = new Variant(APPLICATION_TITLE);
            Variant vText1 = new Variant("");
            Variant vControl1 = new Variant(stringFocus);
            Variant vCommand1 = new Variant("GetItemCount");
            Variant vFrom1 = new Variant("");
            Variant vTo1 = new Variant("");
            Variant[] params1 = new Variant[]{vTitle1, vText1, vControl1, vCommand1, vFrom1, vTo1};
            Variant variantInst1 = activeXComponentInst.invoke("ControlListView", params1);
            //System.out.println("variantInst1.toString(): " + variantInst1.toString());
            int totalConnections = Integer.parseInt(variantInst1.toString());
            System.out.println("totalConnections: " + totalConnections);

            /*Check if required connection is present or not in the connection list. If present, returns index position of element else -1.*/
            Variant vTitle2 = new Variant(APPLICATION_TITLE);
            Variant vText2 = new Variant("");
            Variant vControl2 = new Variant(stringFocus);
            Variant vCommand2 = new Variant("FindItem");
            Variant vConnectionName2 = new Variant(CONNECTION_NAME);
            Variant vOption2 = new Variant("");
            Variant[] params2 = new Variant[]{vTitle2, vText2, vControl2, vCommand2, vConnectionName2, vOption2};
            Variant variantInst2 = activeXComponentInst.invoke("ControlListView", params2);
            System.out.println("variantInst2.toString(): " + variantInst2.toString());

            /*Check if desired connection in the list is selected or not*/
            Variant vTitle4 = new Variant(APPLICATION_TITLE);
            Variant vText4 = new Variant("");
            Variant vControl4 = new Variant(stringFocus);
            Variant vCommand4 = new Variant("IsSelected");
            Variant vConnectionName4 = new Variant(CONNECTION_NAME);
            Variant vOption42 = new Variant("");
            Variant[] params4 = new Variant[]{vTitle4, vText4, vControl4, vCommand4, vConnectionName4, vOption42};
            Variant variantInst4 = activeXComponentInst.invoke("ControlListView", params4);
            System.out.println("\nvariantInst4.toString(): " + variantInst4.toString());

            /*Get text of required connection*/
            Variant vTitle3 = new Variant(APPLICATION_TITLE);
            Variant vText3 = new Variant("");
            Variant vControl3 = new Variant(stringFocus);
            Variant vCommand3 = new Variant("GetText");
            Variant vOption31 = new Variant("0");
            Variant vOption32 = new Variant("0");
            Variant[] params3 = new Variant[]{vTitle3, vText3, vControl3, vCommand3, vOption31, vOption32};
            Variant variantInst3 = activeXComponentInst.invoke("ControlListView", params3);
            System.out.println("\nvariantInst3.toString(): " + variantInst3.toString());
            System.out.println("variantInst3.getString(): " + variantInst3.getString());

            /*Change view to list view*/ // not working
            obj.control.controlListViewSelectViewChange(APPLICATION_TITLE, "", stringFocus, "list"); // No error but there was no change in the view

            /*Clear list selection*/
            obj.control.controlListViewSelectClear(APPLICATION_TITLE, "", stringFocus); // Worked successfully

            /*Select All*/
            obj.control.controlListViewSelectAll(APPLICATION_TITLE, "", stringFocus, "0", "1"); // Worked successfully

        } catch (Exception e) {
            System.out.println("Exception: " + e);
            e.printStackTrace();
        }
    }
}
/*Output for "Get total connections present in the list"*/
totalConnections: 2

/*Output for "Check if required connection is present or not"*/
variantInst2.toString(): -1

/*Output for "Check if desired connection in the list is selected or not"*/
variantInst4.toString(): 1

/*Output for "Get text of required connection"*/
variantInst3.toString(): 
variantInst3.getString(): 
1) (输出1)我得到了正确的输出,即“获取列表中存在的总连接数”的输出2
2) (输出2)现在,为了检查所需的连接是否存在于列表中,我总是得到-1的输出。即使我给出了正确的连接名称,我也只得到-1的输出
谁能告诉我我做错了什么
3) (输出3)检查是否选择了列表中所需的连接, 我总是得到1的输出。在这里,即使我给出了错误的连接名称,我也只得到1的输出,即选择了所需的连接,这是错误的
谁能告诉我我做错了什么
4) (输出4)现在为了获取连接所需的文本,我总是将空字符串作为输出
谁能告诉我我做错了什么