How to flirt a checkbox via webbrowser?

Asked

Viewed 148 times

1

I’m trying to book a checkbox via webbrowser, but I’m not getting through.

HtmlElementCollection theElementCollection = default(HtmlElementCollection);
theElementCollection = doc.GetElementsByTagName("div");
foreach (HtmlElement curElement in theElementCollection)
{
    if (curElement.GetAttribute("className").ToString() == "checkbox")
    {
        foreach (var item in curElement.All)
        {
            if (((HtmlElement)item).InnerText == null)
                continue;

            if (((HtmlElement)item).InnerText.Contains(" Windows 10") || ((HtmlElement)item).InnerText.Contains(" Windows XP")
                || ((HtmlElement)item).InnerText.Contains(" MS-DOS"))
            {
                ((HtmlElement)item).SetAttribute("checked", "true");
            }
        }
    }

    if (curElement.GetAttribute("className").ToString() == "radio")
    {
        foreach (var item in curElement.All)
        {
            if (((HtmlElement)item).InnerText == null)
                continue;

            if (((HtmlElement)item).InnerText.Contains("Masculino"))
            {
                ((HtmlElement)item).SetAttribute("checked", "true");
            }
        }
    }
}

inserir a descrição da imagem aqui

When I check the html page it is setting the label checked="true

inserir a descrição da imagem aqui

  • you’re catching the HtmlElement wrong only

  • managed to fix with ((Htmlelement)item).FirstChild.Setattribute("checked", "true");

1 answer

0


As requested the solution was thus.

if (curElement.GetAttribute("className").ToString() == "checkbox")
    {
        foreach (var item in curElement.All)
        {
            if (((HtmlElement)item).InnerText == null)
                continue;

            if (((HtmlElement)item).InnerText.Contains(" Windows 10") || ((HtmlElement)item).InnerText.Contains(" Windows XP")
                || ((HtmlElement)item).InnerText.Contains(" MS-DOS"))
            {
                ((HtmlElement)item).FirstChild.SetAttribute("checked", "true");
            }
        }
    }

It was enough to use the FirstChild.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.