반응형
C# -- IE automation #5 -- Attribute 제어하기
참고 : https://msdn.microsoft.com/ko-kr/library/aa752279(v=vs.85).aspx --> IHTMLElement interface
http://blog.clockahead.com/2015/06/cie-6.html --> 페이지 로딩 완료 기다리는 확장메소드
https://msdn.microsoft.com/ko-kr/library/aa752330(v=vs.85).aspx --> setAttribute()
부제 : HTML 태그 속성 제어하기
-- getAttribute()
-- setAttribute()
-- 버튼 클릭하기
IHTMLElement.getAttribute() 에서는 "class" 사용못함. 대신에 "className" ( 또는 "classname")사용해야함.
** 확장 메소드 사용법을 추가로 익혀두자.
< 소스 예 1 >
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
namespace IE_ex1 | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
test1(); | |
test2(); | |
} | |
// mshtml.IHTMLElement 사용법 익히기 | |
static void test1() | |
{ | |
var IE = new SHDocVw.InternetExplorer(); | |
// IE.Visible = true; | |
Object URL = "http://example.com/"; | |
IE.Navigate2(ref URL); | |
IE.Wait(); // 페이지의 요소를 반영하기 위해서는 페이지가 표시 될 때까지 기다릴 필요 | |
var doc = IE.Document as mshtml.IHTMLDocument3; | |
mshtml.IHTMLElementCollection elemColl = null; | |
elemColl = doc.getElementsByTagName("a"); | |
Console.WriteLine("a 태그 갯수 == " + elemColl.length); | |
foreach (mshtml.IHTMLElement elem in elemColl) | |
{ | |
Console.WriteLine(elem.getAttribute("href")); | |
Console.WriteLine(elem.innerHTML); | |
Console.WriteLine(elem.innerText); | |
} | |
Console.ReadLine(); // Enter 입력해야 다음으로 넘아감. | |
} | |
// mshtml.IHTMLElement 사용법 익히기 | |
static void test2() | |
{ | |
var IE = new SHDocVw.InternetExplorer(); | |
IE.Visible = true; | |
dynamic url = "http://www.daum.net/"; | |
IE.Navigate2(ref url); | |
IE.Wait(); // 페이지의 요소를 반영하기 위해서는 페이지가 표시 될 때까지 기다릴 필요 | |
var doc = IE.Document as mshtml.IHTMLDocument3; | |
mshtml.IHTMLElementCollection tt = doc.getElementsByTagName("input"); | |
Console.WriteLine("input tag 갯수 == " + tt.length); | |
int count = 0; | |
foreach (mshtml.IHTMLElement elem in tt) | |
{ | |
// getAttribute() 에서는 "class" 사용못함. 대신에 "className" 사용해야함. | |
Console.Write(" {0} : class == " + elem.getAttribute("classname") , count++); | |
Console.Write(" type == {0} ", elem.getAttribute("type")); | |
Console.Write(" value == {0} ", elem.getAttribute("value")); | |
Console.WriteLine(" name == " + elem.getAttribute("name")); | |
// Console.WriteLine(" outerHTML == " + elem.outerHTML); | |
} | |
Console.ReadLine(); // Enter 입력해야 다음으로 넘아감. | |
} | |
} | |
// 페이지 로딩 완료까지 대기하는 확장 메서드 | |
public static class SHDovVwEx | |
{ | |
public static void Wait(this SHDocVw.InternetExplorer ie, int millisecond = 0) | |
{ | |
while (ie.Busy == true || ie.ReadyState != SHDocVw.tagREADYSTATE.READYSTATE_COMPLETE) | |
{ | |
System.Threading.Thread.Sleep(100); | |
} | |
System.Threading.Thread.Sleep(millisecond); | |
} | |
} | |
} |
< 소스 예 2 >
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using SHDocVw; | |
using mshtml; | |
namespace IE_setAttr | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
test(); | |
} | |
// mshtml.IHTMLElement 사용법 익히기 | |
static void test() | |
{ | |
var IE = new SHDocVw.InternetExplorer(); | |
IE.Visible = true; | |
dynamic url = "http://www.w3schools.com/html/html_form_input_types.asp"; | |
IE.Navigate2(ref url); | |
IE.Wait(); // 페이지의 요소를 반영하기 위해서는 페이지가 표시 될 때까지 기다릴 필요 | |
var doc = IE.Document as mshtml.IHTMLDocument3; | |
mshtml.IHTMLElementCollection tt = doc.getElementsByTagName("input"); | |
Console.WriteLine("input tag 갯수 == " + tt.length); | |
foreach (mshtml.IHTMLElement elem in tt) | |
{ | |
if (elem.getAttribute("name") == "lastname") | |
{ | |
Console.WriteLine(" name == " + elem.getAttribute("name")); | |
Console.WriteLine(" value == {0} ", elem.getAttribute("value")); | |
elem.setAttribute("value", "홍길동"); | |
System.Threading.Thread.Sleep(500); // value 변경할 시간 필요함. | |
// 변경한 값 '홍길동' 가져오기. | |
Console.WriteLine(" new value == {0} ", elem.getAttribute("value")); | |
} | |
} | |
foreach (mshtml.IHTMLElement elem in tt) | |
{ | |
if (elem.getAttribute("type") == "submit") | |
{ | |
Console.WriteLine(" type == " + elem.getAttribute("type")); | |
Console.WriteLine(" value == {0} ", elem.getAttribute("value")); | |
elem.click(); // submit 버튼 클릭하기 | |
} | |
} | |
Console.ReadLine(); // Enter 입력해야 다음으로 넘아감. | |
} | |
} | |
// 페이지 로딩 완료까지 대기하는 확장 메서드 | |
public static class SHDovVwEx | |
{ | |
public static void Wait(this SHDocVw.InternetExplorer ie, int millisecond = 0) | |
{ | |
while (ie.Busy == true || ie.ReadyState != SHDocVw.tagREADYSTATE.READYSTATE_COMPLETE) | |
{ | |
System.Threading.Thread.Sleep(100); | |
} | |
System.Threading.Thread.Sleep(millisecond); | |
} | |
} | |
} |
반응형
'C# Web Scraping' 카테고리의 다른 글
C# -- IE automation #7 -- iframe 접근하기 (2) | 2016.02.09 |
---|---|
C# -- IE automation #6 -- 구글 검색창 제어하기 (0) | 2016.02.09 |
C# -- IE automation #4 -- html 구조 파헤치기 (0) | 2016.02.09 |
C# -- IE automation #3 -- internet explorer 제어하여 html 소스 가져오기 (4) | 2016.02.03 |
C# -- IE automation #2 -- Internet Explorer Architecture (0) | 2016.02.03 |