2013-09-05 3 views
0

저는 HTML 민첩성 팩을 사용하고 class="fileHeader"div을 가지고 있으며 하위 h4 요소에 "RelayClinical Patient Education with Animations Install zip"을 가지고 있습니다. 발견되면 특정 블록의 앵커 태그 안에 "href" 속성을 캡처하고 싶습니다. 그것을 어떻게 얻을 수 있습니까?HTML 민첩성 팩을 사용하여 다음 요소 선택

HTML 소스

<div class="fileHeader" id="fileHeader_7311111"> 
    <h4 class="collapsed">RelayClinical Patient Education with Animations Install zip</h4> 
    <div class="defaultMethod"> 
     <a class="buttonGrey" href="https://mckc-esd.subscribenet.com/cgi-bin/download?rid=2511740931&amp;rp=DTM20130905162949MzcyODIwNjM0" title="Clicking this link will open a new window." rel="noreferrer"> 
      HTTPS Download 
     </a> 
    </div> 
</div> 

코드

HtmlNodeCollection fileHeaderNodes = bodyNode.SelectNodes("//div[@class='fileHeader']//h4"); 
foreach (HtmlNode fileHeader in fileHeaderNodes) 
{ 
    if (fileHeader.InnerText.Trim() == "RelayClinical Patient Education with Animations Install zip") 
    { 
     HtmlNodeCollection fileHeaderNodes = bodyNode.SelectNodes("//div[@class='fileHeader']//h4"); 
     foreach (HtmlNode fileHeader in fileHeaderNodes) 
     { 
      if (fileHeader.InnerText.Trim() == "RelayClinical Patient Education with Animations Install zip") 
      { 
       foreach (HtmlNode link in fileHeader.SelectNodes("//a[@href]")) 
       { 
        // extract the link and put in dataUrl var 
        if ((link.InnerText.Trim() == "HTTPS Download") && isFound == true) 
        { 
         count++; 
         // select all a tags (html anchor tags) that have a href attribute 
         HtmlAttribute att = link.Attributes["href"]; 
         dataUrl = att.Value; 
        } 
       } 
      } 
     } 
    } 
} 

답변

0

보다는 h4 요소를 선택, 직접 a 요소를 선택합니다. 그런 다음 href 속성을 가져올 수 있습니다.

var h4Text = "RelayClinical Patient Education with Animations Install zip"; 
var xpath = String.Format(
    "//div[@class='fileHeader' and h4='{0}']/div[@class='defaultMethod']/a", 
    h4Text 
); 
var anchor = doc.DocumentNode.SelectSingleNode(xpath); 
if (anchor != null) 
{ 
    var attr = anchor.GetAttributeValue("href", null); 
    // do stuff with attr 
} 
+0

나는 쉬운 해결책이 있다는 것을 알고있었습니다. 제프가 대답 해 주셔서 감사합니다. 황금 티켓이었습니다. – user2751629

관련 문제