Choosing an automatically generated choice from a C# web browser control

Using a c# web browser control, I am navigating a commercial website to access a list of potential jobs. However, I encountered an issue while trying to bid on these jobs through some links.

The problem arises when dealing with forms that contain two select elements (drop-down lists) where the options are dynamically generated by JavaScript scripts available in the page source code.

In the given form snippet below, you can observe that there are no options under the select elements initially:

<form name="frm1" id="frm1" action="/tab/Transport/LoadAssigned2.asp" method="post">

    <table class="section">
        <tr>
            <td>Name</td>
            <td>
                <input type="text" name="s_name" id="s_name" size="25" maxlength="50"></td>
        </tr>
        <tr>
            <td>Fax</td>
            <td>
                <input type="text" name="txtFaxNumber" id="txtFaxNumber" size="25" maxlength="15" value="1234567890"></td>
        </tr>
        <tr>
            <td>Email</td>
            <td>
                <input type="text" name="txtEmail" id="txtEmail" size="25" maxlength="225"></td>
        </tr>
        <tr>
            <td>Pickup will occur on or before</td>
            <td>
                <select name="stransp_pickup_date" id="stransp_pickup_date" style="width: 173px;" onchange="setDeliveryDate()">
                </select>
            </td>
        <tr>
        </tr>
            <td>Delivery will occur on or before</td>
            <td>
                <select name="stransp_delivery_date" id="stransp_delivery_date" style="width: 173px;">
                </select>
            </td>
        </tr>
    </table>
    <input type="hidden" name="nload_id" id="nload_id" value="123456789">

</form>

The script named "setDeliveryDate" dynamically populates the options based on the selected date from the first select element and the distance criteria provided.

Given the dynamic generation nature, the challenge lies in selecting specific date options from both lists despite not having them visible in the source code. The number of options displayed varies depending on the distance parameter as per the mentioned script.

If required, additional script details can be provided for further understanding. Any assistance or guidance on how to access these dynamically generated elements would be greatly appreciated.

Answer №1

For those who may come across this in the future, I managed to solve the issue on my own and wanted to share a simple solution.

Firstly, take note that the initial "select" element has an onchange function called setDeliveryDate. To begin, I trigger this script function:

webBrowser1.Document.InvokeScript("setDeliveryDate");

Next, I gather the child nodes into a collection and select the last one:

HtmlElementCollection colOptions =
   webBrowser1.Document.GetElementById("stransp_pickup_date").Children;

int colCount = colOptions.Count;

colOptions[colCount - 1].SetAttribute("selected", "selected");

Then, I run the same script again to populate options for the second select element and repeat the process (collect option elements, select the last one):

webBrowser1.Document.InvokeScript("setDeliveryDate");

HtmlElementCollection colOptions2 =
    webBrowser1.Document.GetElementById("stransp_delivery_date").Children;

int colCount2 = colOptions2.Count;

colOptions2[colCount2 - 1].SetAttribute("selected", "selected");

Finally, the last step involves submitting the form:

webBrowser1.Document.GetElementById("frm1").InvokeMember("submit");

I hope this explanation will assist other beginners like myself.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Ways to determine the success of $wpdb->query and retrieve the outcome

Currently, I am in the process of developing a WordPress website, I have implemented a form that allows users to make modifications and update the database: Below is the HTML/PHP code snippet: echo '<form class="form-verifdoc" target=&q ...

Why does my chart.js disappear every time I perform a new render?

Hey there, I'm facing an issue with my code. Let me paste what I have... import React, { memo, useEffect } from 'react'; import Chart from "chart.js"; /* redux-hook */ import { useSelector } from 'react-redux' const lineChart = m ...

Are you experiencing difficulty loading ng-view in AngularJs?

I am new to AngularJs. I am currently using a wamp server and have successfully loaded the HTML page, but unfortunately the view is not being displayed. I have added ng-app to the body as well, but still unable to load the view. <!DOCTYPE html> ...

The toLowerCase method seems to be malfunctioning along with several other functions

JS var score = 0 var yes = "yes" var pokemonName = []; var bg = []; var index = 0; document.getElementById('repete').style.visibility = 'hidden'; (function asyncLoop() { background = bg[num = Math.floor(Math.random() ...

Encountering a 500 server error while attempting to retrieve content from Google Images through the Web Speech API

My current project involves utilizing the Web Speech API to dynamically gather free images from Google. Here's how it works: I extract the search keyword using the Web Speech API in JavaScript. The keyword is then sent to the server (PHP) via an a ...

Issue with CORS Policy specifically in this scenario despite functioning properly for other assets

My API is built with Laravel and for the front-end, I am using Vue.js with Nuxt.js. I have successfully installed the CORS policy package, which is working well with all my other resources. However, I encountered an error for one of my features: ...

Navigating Users and Routing with Ionic Framework (AngularJS)

Currently, I am using Ionic for a new project and could use some guidance with routing (I'm relatively new to Angular). These are the states I have defined: $stateProvider.state('map', { url: '/map', views: { map: ...

Tips for establishing a fixed point at which divs cease to shrink as the browser size decreases

There are numerous dynamically designed websites where divs or images shrink as the browser size decreases. A great example of this is http://en.wikipedia.org/wiki/Main_Page The div containing the text shrinks proportionally to the browser size until it ...

The JSON Deserialization method returned a value of 0

Greetings! I'm facing some challenges in C# (Xamarin) after following various tutorials on parsing. My main focus is on extracting the value only. Does anyone have any insights on how to resolve this issue? Below is a snippet of my JSON data: { &q ...

How can you utilize Angular Signals in combination with HostBinding to dynamically update styles?

Within a component called app-test, the following code is present: @Input({ transform: booleanAttribute }) reverse: boolean = false; @HostBinding('style.flex-direction') direction: string = this.reverse ? 'column-reverse' : &ap ...

Click once to reveal, click twice to conceal the slider

I currently have a web page set up so that a single click on the text displays a slider for selecting a range of values. Now, I want to incorporate a feature where a double click will remove the slider. Below is the HTML code: <!DOCTYPE html> < ...

The XML response from Ajax is returning as empty

My goal is to retrieve XML data from an ajax request and extract information using the DOM. The ajax request itself seems to be working fine as I can access AjaxRequest.responseText without any issues. However, I am encountering an error message stating: ...

Animate specifically new items in a list rendered via ngFor in Angular animation

I am working with a list of array items in an ngFor loop. I have a basic button that adds an item to the array. My goal is to apply an animation only to the newly added items, but currently all the existing list items also receive the animation upon page l ...

Can you explain the inner workings of the sort function in JavaScript, including how it utilizes the compare

I'm curious about how the sort function operates in JavaScript, specifically in conjunction with the compare function. According to what I've read, if you have an array and use the code array.sort(compare), it's stated that if the compare fu ...

The ng-switch function is not generating the desired code output

In my Ionic app, I have the following code snippet that I am currently viewing with ionic serve. However, when the initial ng-switch statement triggers... <span ng-switch="post.enclosure"> <span ng-switch-when="[]"> <p>def&l ...

Guide on converting this function into a computed property within Vue

Is it possible to concatenate a fixed directory path, which is defined in the data property, with a file name that is determined using v-for? I encountered an issue when attempting to do this using a computed property. The error message displayed was: ...

How can we guide the user to a different page when a particular result is retrieved by AJAX?

Whenever my PHP function makes a database call, I receive multiple results. The ajax then displays these results in a div element. My question is: How can I redirect the user to the next page once I obtain a specific result from the PHP function? Current ...

The texture appearance becomes unusual when using a ThreeJS point light

I utilized ThreeJS, specifically React Three Fiber, to construct a 3D Canvas. After adding a glb model, point light, and ambient light, the outcome was disappointing. https://i.sstatic.net/6vHaOKBM.png Why does the texture appear so jagged? Could it be ...

What Makes Sports Illustrated's Gallery Pages Load So Quickly?

After thoroughly examining their code, I'm still puzzled. Are they loading complete new pages, or are they utilizing jQuery to modify the browser's URL while keeping most of the page unchanged? I'm currently inspecting the source of this pa ...

Combining the first name, last name, and code in Javascript

I'm attempting to combine the initial letter of both names with the randomly generated code. var firstname = prompt("Please input your first name."); var lastname = prompt ("Please input your last name."); if (amountCorrect >= 4){ ...