2010-08-08 11 views
5

पर क्लिक करें मैं HTML/AJAX में कक्षा द्वारा तत्व को पढ़ने के लिए इस कोड का उपयोग करने का प्रयास कर रहा हूं, GetElementByClass WebBrowser.Document में कोई विकल्प नहीं है। मुझे रिटर्न वैल्यू नहीं मिल रहा है तो सदस्य को बुलाओ। क्या इसके आसपास कोई कार्य है?GetElementByClass द्वारा कक्षा का चयन कैसे करें और प्रोग्राम पर

संदर्भ: Getting HTMLElements by Class Name

उदाहरण:

<span class="example">(<a href="http://www.test.com/folder/remote/api?=test" onclick=" return do_ajax('popup_fodder', 'remote/api?=test', 1, 1, 0, 0); return false; " class="example">test</a>)</span> 

उदाहरण कोड:

HtmlElementCollection theElementCollection = default(HtmlElementCollection); 
    theElementCollection = webBrowser1.Document.GetElementsByTagName("span"); 
    foreach (HtmlElement curElement in theElementCollection) 
    { 
     //If curElement.GetAttribute("class").ToString = "example" It doesn't work. 
     // This should be the work around. 
     if (curElement.OuterHtml.Contains("example")) 
     { 
      MessageBox.Show(curElement.GetAttribute("InnerText")); // Doesn't even fire. 
      // InvokeMember(test) after class is found. 
     } 
    } 

उत्तर

0

क्यों आप इस के लिए qjuery के चयनकर्ता इंजन का उपयोग नहीं करते। और साथ ही, आप संदेशबॉक्स की अपेक्षा कहां कर रहे हैं। दिखाने के लिए दिखाएं?

+3

बहुत बढ़िया, का एक कांटा का उपयोग करने का काम करता है के बजाय GetAttribute("className") उपयोग करने की आवश्यकता jQuery? ;) –

+0

संदेशबॉक्स। दिखाओ मुझे यह पाठ दिखाया गया था जो इसे पुनर्प्राप्त किया गया था। JQuery मैं परिचित नहीं हूँ। इस मामले में एक उदाहरण मिला? – Nightforce2

4

यह एक उदाहरण है कि मैंने क्लास विशिष्ट तत्वों को खोजने और अंदर आने वाले लिंक पर क्लिक करने के लिए वेबब्रोसर नियंत्रण का उपयोग कैसे किया।

सरलीकृत>

foreach (HtmlElement item in webBrowser1.Document.GetElementsByTagName("li")) 
     { 
      // use contains() if the class attribute is 
      // class="page_item page-item-218 current_page_item" 
      //this to be more on spot! >> if (item.OuterHtml.Contains("class=\"page_item")) 
      // or 
      if (item.OuterHtml.Contains("page_item")) 
      { 
       foreach (HtmlElement childItem in item.Children) 
       { 
        if (childItem.TagName == "A") 
        { 
         //Click the link/Current element 
         childItem.InvokeMember("Click"); 
         break; 
        } 
       } 
       break; 
      } 
     } 

इस तरह से काम करता है? ..

यह यहीं मेरे लिए काम करता है।

या शायद मैं आपके प्रश्न को गलत समझ सकता हूं?

+0

यह घटना लिंक में ईवेंट को आग नहीं करता है। अगर आप फिर से लिंक पर नजर डालें तो मैं अजैक्स कार्यक्रम "टेस्ट" को आग लगाने की कोशिश कर रहा हूं। =) – Nightforce2

12

मैं मानता हूं कि यह बहुत सहज नहीं है, लेकिन आप GetAttribute("class")

HtmlElementCollection theElementCollection = default(HtmlElementCollection); 
theElementCollection = webBrowser1.Document.GetElementsByTagName("span"); 
foreach (HtmlElement curElement in theElementCollection) 
{ 
    if (curElement.GetAttribute("className").ToString() == "example") 
    { 
     MessageBox.Show(curElement.GetAttribute("InnerText")); // Do something you want 
    } 
} 
0
Dim HtmlElementcolltwo As HtmlElementCollection = WebBrowser1.Document.GetElementsByTagName("button") 
     For Each eleme As HtmlElement In HtmlElementcolltwo 
      ' Check the attributtes you want 
      If eleme.GetAttribute("className") = "derrt_submit feed-zed-bff" Then 
       'Check even the text if you want 
       ' If elem.InnerText = "Sign In" Then 
       'Invoke your event 
       eleme.InvokeMember("click") 
       'End If 
      End If 
     Next 

यह भी बजाय "वर्ग" उपयोग "className"

संबंधित मुद्दे