1

I have a question, how to change HTML input value?

<select name="sys_lenght" aria-controls="systable" class>
<option value="20">Value 1</option>
<option value="40">Value 2</option>
<option value="80">Value 3</option>
</select>

I used this code

For Each Elementz In WebBrowser1.Document.GetElementsByTagName("select")
        If Elementz.Name = "sys_lenght" Then
            Elementz.setAttribute("value", "80")
        End If
    Next

But it doesn't change the input value, only the text "Value 3". How can I solve this problem? Thanks

2 Answers 2

2

sys_lenght's value indicates which option with the specified value is selected. Thus if you set sys_lenght.value to "80" it will select Value 3.

To change the value of the currently selected option you have to get a reference to that first. You can do so by getting the selectedIndex of sys_lenght, and then get the specific item from that index.

For Each Elementz In WebBrowser1.Document.GetElementsByTagName("select")
    If Elementz.Name = "sys_lenght" Then

        'Get the index of the selected option.
        Dim SelectedIndex As Integer = Integer.Parse(Elementz.GetAttribute("selectedIndex"))
        If SelectedIndex >= 0 Then
            'Get the option element from the resulting index.
            Dim SelectedOption As HtmlElement = Elementz.Children(SelectedIndex)
            SelectedOption.setAttribute("value", "80")
        Else 'No option selected.
            MessageBox.Show("No option selected!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End If

        Exit For 'We found what we were looking for; stop looping.
    End If
Next
Sign up to request clarification or add additional context in comments.

6 Comments

It's possible create a new custom value? E.g: 300?
@Giuseppe : Yes, you can set it to anything you want (whether the website will accept that however is a different question).
Well, still not working for me, have no errors, but still not changing :\
@Giuseppe : What exactly is it you want to change? Am I right when I say you want to change the value of the selected option?
@Giuseppe : Are you really sure that it doesn't actually change, and that the case isn't that it just don't appear so because the webpage rejecting the value? (whatever it does with it) - Because I just tested the code and it works fine for me.
|
0

First you have to understand how html open and close tag, you forgot to close the html selection option open tag i mean the last greter sign > then use javascript query to find the value within the option example

WebControl1.ExecuteJavascript('document.querySelector('option [value='your value']').selected=true;") 

hope its help?

2 Comments

My mistake, fixed. But it doesn't affect nothing in this question
I saw that you have updated your answer, well... Still not working this javascript inject

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.