How to select a class by GetElementByClass and click on it programmatically

前端 未结 4 909
无人及你
无人及你 2020-12-11 06:23

I have been trying to use this code to read the element by class in html/ajax knowing GetElementByClass is not a option in webBrowser.Document. I can\'t seem to get a return

4条回答
  •  一个人的身影
    2020-12-11 06:50

    I admit it's not very intuitive but you need to use GetAttribute("className") instead of 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
        }
    }
    

提交回复
热议问题