2011-11-26 1 views
0

요소에 모든 속성을 추가 할 수 있습니까?Watin을 사용하여 태그에서 Attributes 컬렉션을 얻는 방법은 무엇입니까?

요소의 모든 속성을 반복하고 값을 가져 오려면이 코드가 필요합니다!

내가 요소 클래스에서 검색했지만 내가 반복하고 GetAttributeValue 수 속성 문자열 이름의 컬렉션을 반환 할 특정 재산이나 방법을 볼 수 없습니다 ....

감사 어떤 도움.

감사합니다.

답변

0

내가 알고있는 한 WatiN에는 아무 것도 내장되어 있지 않기 때문에이 작업을 수행하는 방법을 썼다. 이후로이 코드에 아무런 문제가 없었지만 여전히 끔찍한 해킹이라고 생각합니다! 어쩌면 여기서 더 똑똑한 포스터 중 하나가 그것을 향상시키는 데 도움이 될 수 있습니다! HTH!

private void button1_Click(object sender, EventArgs e) 
{ 
    using (IE browser = new IE("www.google.co.uk")) 
    { 
     Div div = browser.Div("hplogo"); 
     Dictionary<string, string> attrs = GetAllAttributeValues(div); 
    } 
} 

private Dictionary<string, string> GetAllAttributeValues(Element element) 
{ 
    if (element == null) 
     throw new ArgumentNullException("Supplied element is null"); 
    if (!element.Exists) 
     throw new ArgumentException("Supplied element does not exist"); 

    string html = element.OuterHtml; // element html (incl children) 
    int idx = html.IndexOf(">"); 
    Debug.Assert(idx != -1); 
    html = html.Substring(0, idx + 1).Trim(); // element html without children 

    Dictionary<string, string> result = new Dictionary<string, string>(); 
    while ((idx = html.IndexOf('=')) != -1) 
    { 
     int spaceIdx = idx - 1; 
     while (spaceIdx >= 0 && html[spaceIdx] != ' ') 
      spaceIdx--; 
     Debug.Assert(spaceIdx != -1); 

     string attrName = html.Substring(spaceIdx + 1, idx - spaceIdx - 1); 
     string attrValue = element.GetAttributeValue(attrName); 
     result.Add(attrName, attrValue); 

     html = html.Remove(0, idx + 1); 
    } 
    return result; 
} 
+0

이 코드의 잠재적 인 문제점을 발견했습니다. 속성 값 중 하나에 '='이 있으면 다른 속성으로 구문 분석하려고 시도합니다. S 문자열을 XML로 구문 분석하여 키 - 값 쌍을 얻을 수 있습니다. –

0

동일한에 대한 HtmlAgilityPack를 사용할 수 있습니다. HtmlNode.AttributesHtmlAttributeCollection으로 제공하며 루프를 사용하여 속성 이름과 값을 가져올 수 있습니다.

+0

제공하는 솔루션에 대한 설명을 추가하여 답을 더 자세히 설명해 주시겠습니까? – abarisone