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

Guide to removing a particular textfield from a table by clicking a button with ReactJS and Material UI

I need assistance with deleting a textfield on a specific row within a table when the delete button for that row is clicked. Currently, I am able to add a text field by clicking an add button and delete it by clicking a remove button. However, the issue is ...

The function to refresh content is causing errors in the code

Creating a comment and replies form where the replies display underneath each comment. I've encountered an issue where I can post replies normally, but when attempting to delete a reply, I must refresh the page. After refreshing, I'm only able to ...

Tips for Elevating State with React Router Version 6

Looking for advice on sharing state between two routes in my project. I'm debating whether to lift state up from my AddContact component to either the Layout or App components in order to share it with the ContactList. The Layout component simply disp ...

submitting a collection of hashes to a server using jQuery

I'm facing an issue with sending data to the server. I have an array structured like this: places = new Array(); places.push({name: "ci", loc: "bo"}) places.push({name: "ae", loc: "ea"}) if I try to send this data to the server using the following J ...

Encountering a 400 Bad Request error while using the SharePoint 2013 Rest API

I am currently developing a web component that utilizes jQuery for making a REST API request to rename a SharePoint subsite. The functionality is working as expected when using a custom button on EditForm.aspx within a list, and it also functions correctly ...

"Encountered a problem while trying to insert data using JSON and Ajax

I've been struggling to figure out where the issue lies in my code. I've double-checked everything and it all seems correct, but I keep encountering errors. Below is the code snippet: Markup: <link href="http://ajax.googleapis.com/ajax/libs ...

Is employing setTimeout a legitimate technique for circumventing a stack overflow issue when implementing callbacks?

Let's imagine a scenario where I deliberately create a complex sequence of callbacks: function handleInput(callback) { ... } function fetchData(url, callback) { ... } function processResponse(callback) { .... } function updateDatabase ...

After deploying on Vercel, Next.js' getServerSideProps function is returning undefined

I am trying to create a Netflix-inspired website using next.js. I am able to fetch movie data from TMDB using getServerSideProps(). While everything works as expected in development mode, once deployed on Vercel (re-deployed multiple times), the props I re ...

Troubleshooting an expressjs server in parallel with electron

I have managed to successfully run an ExpressJS server alongside Electron by following the instructions provided in this post: Run Node.js server file automatically after launching Electron App However, I am facing an issue where there is no output from t ...

Steps for referencing a custom JavaScript file instead of the default one:

Currently, I am utilizing webpack and typescript in my single page application in combination with the oidc-client npm package. The structure of the oidc-client package that I am working with is as follows: oidc-client.d.ts oidc-client.js oidc-client.rs ...

How to enhance a class in JavaScript by adding a dynamic property

Within my code, I've defined a class which looks like this: class classA { id: number; name: string; constructor(data){ this.id = data?.id || 0; this.name = data?.name || ''; } } What I aim to do now is add ...

tool for showcasing data on a webpage with a specific layout

Which chart library is best suited for presenting data in the formats shown in the images below? Can HighCharts handle these formats? Does Google Charts allow for a combination of bubbles and lines? ...

Having trouble incorporating an API module into a nested component in Vue

I have encountered a perplexing issue when attempting to import an API module into a nested component within a Vue application. After simplifying the problem as much as possible, I created a codesandbox example to demonstrate it. The problem arises when ...

Creating a button that allows updates without refreshing the page can be achieved by implementing

Here are the items I have: <table> <tr> <th> id </th> <th> name </th> <th> update </th> </tr> <tr> ...

Optimal approach for verifying the POST request payload

My RESTful API has a POST endpoint for user creation, and I am looking to implement data validation before inserting it into the database. There are two approaches I'm considering: Approach 1: Incorporating model validation in the controller and repe ...

How to empty an array once all its elements have been displayed

My query pertains specifically to Angular/Typescript. I have an array containing elements that I am displaying on an HTML page, but the code is not finalized yet. Here is an excerpt: Typescript import { Component, Input, NgZone, OnInit } from '@angul ...

TypeScript and Next.js failing to properly verify function parameters/arguments

I'm currently tackling a project involving typescript & next.js, and I've run into an issue where function argument types aren't being checked as expected. Below is a snippet of code that illustrates the problem. Despite my expectation ...

Using a separate function to trigger the save_post action for custom posts in Wordpress

I have created a custom post using functions.php, which is working fine in the admin panel. Now, I want to manually create a custom post from another PHP file using AJAX. Here is the code snippet from functions.php: add_action('save_post', &apos ...

Creating interactive button groups with responsive design in a Material UI and ReactJS web application

Is it possible to make ButtonGroup Buttons responsive? I heard about an attribute called "Orientation" in material-ui's ButtonGroup, but I'm not sure how to use it with media queries for changing orientation based on the device width. I'm st ...

Leveraging IntersectionObserver to identify the video in view on the screen

Our Objective I aim to implement a swipe functionality for videos where the URL changes dynamically based on the ID of the currently displayed video. Challenges Faced Although I managed to achieve this with code, there is an issue where the screen flashe ...