2014-12-07 3 views
1

양식이 포함 된 사이트를 호출하여 고객을위한 간단한 프로세스를 자동화 한 다음 이미 알고있는 몇 가지 값을 삽입하려고합니다. 따라서 사용자는 누락 된 값을 완성하고 양식을 제출하기 만하면됩니다. 내가 지금까지해온 것은 IE를 시작하고 양식이 포함 된 사이트로 이동하는 것입니다. 입력 요소를 검색 할 수도 있지만 값을 설정할 수있는 방법을 찾을 수 없습니다. 속성/메소드 이름으로 "값"을 사용하여 값을 설정하려고하면 "설명 : 80004001/구현되지 않음"만 수신됩니다. 나는이 시점에서 붙어있다. .NET과 C#을 사용양식 요소에 값을 설정하는 방법

나는 다음을 수행하여이 작업을 수행 할 수있어 :

SHDocVw.InternetExplorer IE = new SHDocVw.InternetExplorer(); 
IE.Navigate2("http://some.where"); 
var form = IE.Document.Forms(0); 
form.Elements("foo").Value = "bar"; 
[...] 
form.Submit(); 

하지만이 특수 .NET 물건의 어떤 종류를 수행하여 COM을 사용하는 경우 난 정말 모르겠어요 가능성이 더 많습니다. 그러나 - (자바에서 -하지만이 관련성로부터이라고 생각하지 않습니다) COM을 사용하여 지금까지이 작업을했습니다 :

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 요소를 조작 할 수있는 방법이없는 ? 그 다음이 아닌 경우 나는

답변

1

보십시오 .. .NET 내가 피하려고 고객 측 의무에 .NET 런타임을 만들어 내 코드에서이 호출에 마틴을

덕분에 그 물건을 포장 할 필요가 객체 대신 문헌 통해가는 getElementsByTagName (또는 getElementsById), HTML 요소 컬렉션에 루프를 사용하여 소자를 찾아 값을 지정 의 setAttribute을 사용하여 값을 설정 속성

var docu = IE.Document; 
var htmlElements = docu.GetElementsByTagName("inputTagName"); 

foreach (HtmlElement htmlElement in htmlElements) 
    { 
     var name = htmlElement.GetAttribute("name"); 
     if (name != null && name.Length != 0) 
     { 
      htmlElement.SetAttribute("value","Test"); 
     } 
    } 
+0

우수! "SetAttribute"는 매력처럼 작동합니다. 감사! –

+0

이 문제는 IE 10에서 계속 발생합니다. –

관련 문제