我打开了一个应用程序窗口。该窗口包含连接列表。我希望唯一使用 java 在 autoIt 的帮助下从列表中检索处于只读状态的文本(即连接名称) 。我在列表中有两个连接。
对于这个项目,我使用的工具和技术如下:
1) Java SE 8
2) AutoIt
虽然包含连接列表的窗口具有唯一ID,但列表中的各个元素都没有。 因此,不能选择按ID搜索和获取所需的唯一文本。 我使用了ControlListView 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"):
为解决上述错误,我提到了AutoItX.java,com.jacob.com.Variant Class和com.jacob.activeX.ActiveXComponent Class的源代码。 我更新了上面的代码如下:
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)现在检查列表中是否存在所需的连接,我总是输出为-1。即使我给出正确的连接名称,我也只输出-1。
谁能告诉我,我在这里做错了什么?
3)(输出3)要检查列表中是否选择了所需的连接,
我总是输出为1.这里即使我给出错误的连接名称,我只输出1,即选择所需的连接,这是错误的。
谁能告诉我,我在这里做错了什么?
4)(输出4)现在为了获得连接所需的文本,我总是得到空字符串作为输出。
谁能告诉我,我在这里做错了什么?