5

I need to select the innerText from the currently selected option in the webBrowser control.

Here is a representation of the html:

<SELECT ID="F8">
<OPTION VALUE="option1">option1</OPTION>
<OPTION VALUE="option2" selected>option2</OPTION>
<OPTION VALUE="option3">option3</OPTION>
</SELECT>

Here is what I am trying:

if (webBrowser1.Document.GetElementById("F8") != null)
{
    HtmlElement selectF8 = webBrowser1.Document.GetElementById("F8");
    foreach (HtmlElement item in selectF8.Children ) 
    {
        if (item.GetAttribute("selected") != null)
        {
            assigneeText.Text = item.InnerText;
        }
    }
}

... but it completely ignores the if statement and always assigns assigneeText.Text the value of option3 instead of the value of the selected option2.

Can someone please tell me what I am doing wrong?

3 Answers 3

8

The selected attribute doesn't get updated when you change the selection on the control. It is used to define the item that is selected (default option) when the control is first displayed.

To get the current selection you should invoke the selectedIndex method to find out which item is selected.

HtmlElement element = webBrowser1.Document.GetElementById("F8");
object objElement = element.DomElement;
object objSelectedIndex = objElement.GetType().InvokeMember("selectedIndex",
BindingFlags.GetProperty, null, objElement, null);
int selectedIndex = (int)objSelectedIndex;
if (selectedIndex != -1)
{
  assigneeText.Text = element.Children[selectedIndex].InnerText;
}

If you are using c# 4 you can also use the DLR to avoid having to use reflection.

var element = webBrowser1.Document.GetElementById("F8");
dynamic dom = element.DomElement;
int index = (int)dom.selectedIndex();
if (index != -1)
{
  assigneeText.Text = element.Children[index].InnerText;
}
1
  • 1
    Thanks.. this works! I found another solution below, but this is probably better than my solution...
    – jwitt98
    Dec 17, 2013 at 4:02
1

Shortly after I posted this, I figured out a way to make this work: My solution was

HtmlElement selectF8 = webBrowser1.Document.GetElementById("F8");
foreach (HtmlElement item in selectF8.Children)
{
    if (item.GetAttribute("value") == webBrowser1.Document.GetElementById("F8").GetAttribute("value"))
    {
        assigneeText.Text = item.InnerText;
    }
}

This seems to work, although being new to C# and .net, Fraser's answer works too and is probably the better solution.

1
foreach (HtmlElement item in elesex.Children)
{              
    if (item.GetAttribute("selected") == "True")
    {
        sex = item.InnerText;
    }
}
1
  • The selected attribute doesn't tell you if the given element is selected by the user - and doesn't change when a selection is made, it simply specifies that an option should be pre-selected when the page loads. It has no use outside that scope.
    – Fraser
    Jul 19, 2017 at 20:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.