1
我嘗試通過調用包含表單的網站爲我的客戶自動化一個簡單的過程,然後插入一些我已經知道的值。因此,用戶只需填寫缺失值並提交表單。 到目前爲止我所做的是啓動IE並導航到包含該表單的網站。 我甚至能夠檢索輸入元素,但我找不到爲它們設置值的方法。如果我嘗試使用「值」作爲屬性/方法名稱來設置值,我只收到「說明:80004001 /未實施」。 我被困在這一點。如何設置表單元素的值
使用C#和.NET我能夠做完成這一任務的以下內容:
SHDocVw.InternetExplorer IE = new SHDocVw.InternetExplorer();
IE.Navigate2("http://some.where");
var form = IE.Document.Forms(0);
form.Elements("foo").Value = "bar";
[...]
form.Submit();
但我真的不知道,如果我使用COM做這個或某種特殊.NET的東西有更多的可能性。 然而 - 使用COM(從Java - 但我不認爲這是相關性),我有這個至今:
ActiveXComponent xl = new ActiveXComponent("InternetExplorer.Application");
Dispatch ie = xl.getObject();
Dispatch.invoke(ie, "Navigate2", Dispatch.Method, new Object[] {"http://some.where"}, new int[1]);
// Now we're at http://some.where
xl.setProperty("Visible", new Variant(true));
// Getting the document
Dispatch document = Dispatch.get(ie, "Document").getDispatch();
// At this point I'm not able to call a property or method called "Elements"
// like I did with the c# example above. This makes me believe that my c#
// example is using a more 'integrated' IE-automation as the COM interface does.
// However, reading MSDN documentation I was a able to find a way to get a few sets further:
// Retrieving all input-elements
Dispatch elems = Dispatch.invoke(document, "getElementsByTagName", Dispatch.Method, new Object[] { "input" }, new int[1]).getDispatch();
// elems is now a pointer to a collection I can traverse
// To keep it simple I try to use the first element and do something with it:
Dispatch elem = Dispatch.invoke(elems, "item", Dispatch.Method, new Object[] { 0 }, new int[1]).getDispatch();
// 'elem' is now the first input-Element. To verify I can print out its name (foo):
System.out.println(Dispatch.get(elem, "name"));
// However - the following just fails with "Description: 80004001/Not implemented".
Dispatch.invoke(elem, "value", Dispatch.Get, new Object[] { "test" }, new int[1]).getDispatch();
是不是有辦法通過COM接口來操作HTML元素科技?如果不是,那麼我需要包裝的東西與.NET和調用這個從我的代碼,這使得客戶端必需的一個.NET運行時,我試圖避免..
謝謝,馬丁
非常好! 「SetAttribute」就像一個魅力。謝謝! – 2014-12-07 22:38:21
這個問題還在IE 10中 – 2015-06-17 09:12:10