Dealing with dropdown menus and comboboxes in Selenium using C#

I'm having trouble selecting an item from a custom dropdown menu. I've tried using XPath, CssSelector, and Id with no success.

Here is the link to the code snippet:

Code Screenshot

I believe I need to target the element with class="SelectBox" in order to access id='ctl00_ctl00_ctl00_MP_Blank_Body_MP_Base_Body_MP_TopSideMenu_Body_ctl00_cboBehandlingstype'

However, I keep encountering errors.

This is my current attempt, which isn't working:

IWebElement test = driver.FindElement(By.XPath("//div[@class='input']//div[@id='ctl00_ctl00_ctl00_MP_Blank_Body_MP_Base_Body_MP_TopSideMenu_Body_ctl00_cboBehandlingstype']"));

Could someone provide me with guidance on how to access the items within the dropdown list?

Thank you! :)

Answer №1

Make sure to utilize the "SelectElement" class rather than "IWebElement".

SelectElement dropdown = new SelectElement(driver.FindElement(By.Id("ctl00_ctl00_ctl00_MP_Blank_Body_MP_Base_Body_MP_TopSideMenu_Body_ctl00_cboBehandlingstype")));

dropdown.SelectByText("510111 Normalbehandling");

Answer №2

Give this a try

var dropdown = driver.FindElementById("ctl00_ctl00_ctl00_MP_Blank_Body_MP_Base_Body_MP_TopSideMenu_Body_ctl00_cboBehandlingstype");

var options = dropdown.Text.Split(new string[] { "\r\n" }, StringSplitOptions.None);

((IJavaScriptExecutor)driver).ExecuteScript("var select = arguments[0]; for(var i = 0; i < select.options.length; i++){ if(select.options[i].text == arguments[1]){ select.options[i].selected = true; } }", dropdown, options[0]);

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

Activating hardware acceleration: utilizing translate3d

In a recent discussion by David Walsh, he mentions the potential for hardware acceleration through the use of the translate3d property in CSS. Here's how it operates: .animClass { -webkit-transform: translate3d(0, 0, 0); -webkit-backface-vis ...

Steps to confirm a number on a webpage using Robot Framework

Could someone assist me with verifying a number on a website? Should I obtain the ID of the number, convert it to an integer, and then log it to the console? Log To Console ...

Troubleshooting Laravel 5.4 and Elixir Bugs

Is there a simple way to add new CSS tags into an imported SCSS file like the example below? @import "general/global" with the following content: body { background-color: #999; } Although it compiles successfully with Elixir (using npm run dev and ...

How to create a calendar selection input using PHP?

Before I start writing the code myself, I am searching to see if there is already a solution available or if someone has previously outsourced the code. Below is an example of my date selection: https://i.sstatic.net/KqIDH.png I want to create a feature ...

Tips for properly formatting JSON in JavaScript

View Model function CustomerInformation() { var vm = this; vm.CustomerInfo = { "customerId": "", "customerTitle": "", "customerName": "" }; return vm; } Global Variables (function () { 'use strict'; v ...

When attempting to submit the form, the page does not refresh as expected. The preventDefault() function also

I am facing an issue where, upon submitting a form, the page reloads. However, my requirement is to submit the form without reloading the page and send the data to a specific action URL. This may seem like a duplicate question but here are the details: F ...

Search for "conditions" in a basic HTML DOM parser

I encountered an issue when trying to parse information from a website using simple HTML DOM parser. My code looks something like this: <li> <p class="x"></p><p>..</p> <p>..</p> <p>..</p> <p class ...

Make sure to load the HTML content before requesting any input from the user through the prompt function

Could you please help me with a question? I'm looking to load HTML content before prompting the user for input using JavaScript's prompt() function. However, currently the HTML is only loaded after exiting the prompt. Below is the code that I hav ...

One effective method for populating my form following the retrieval of JSON data

Currently, I am working on a VueJs project where I am fetching data from a Mysql database using Axios. My next step is to implement an edit option for user profiles. I am wondering what the most efficient method is for populating the form with the fetche ...

Having difficulty running my selenium test on the virtual machine

My goal is to execute my Selenium test using selenium grid. To achieve this, I have set up the hub on my local Ubuntu machine and a node on a Centos VM. I ran the following commands: java -jar selenium-server-standalone-3.141.59.jar -role hub (on local ma ...

Tips on sprucing up the edges of a Bootstrap grid design using row alignment

The layout of the page is built on a simple two-column Bootstrap structure: <div class="container"> <div class="row"> <div class="col-sm"> column 1 </div> <div class="col-sm& ...

Selenium Net::ReadTimeout encountered in rapid tests without @javascript tag - Ruby Cucumber tests

After multiple attempts to fix the issue, I resorted to performing a complete reinstallation of WSL2 which resolved the problem successfully. If you're facing a similar issue, consider a hard reset as a potential solution. I have already reviewed num ...

I have successfully added an image to my CSS file, and it is appearing in Chrome's inspect feature. However, the image is not visible on the actual website

Here is the code snippet: .nav-link-apple { width: 1.6rem; background: 4.4rem; background: url(./Logo.jpg) center no-repeat; } Although the image I added can be seen in inspect on Chrome, it is not visible on the actual webpage. I have tried s ...

Guide to displaying numerous div elements and their corresponding values utilizing Selenium WebDriver with Java

1). Is it possible to retrieve all element values and store them in an array or list? 2). How can I click on an element based on its value? <div class="Choice" style="margin-top: -483px;> <div class="ChoiceEntry Choice_1"> 5</div> <d ...

Automatically update a Django table with JSON data

I am looking for a way to automatically append JSON data to my Django website without needing to refresh the page. This JSON data is received when certain asynchronous tasks are completed with Celery. Do you have any ideas on how I can achieve this? Than ...

Developed a query, seeking information for populating a dropdown menu

I am currently in the process of editing a webpage that utilizes both PHP and HTML. Essentially, I have a dynamic list of items being generated where the number of values can vary based on the row length. Next to each item in the list, there is a "delete ...

Position the image in the exact center both vertically and horizontally

I am trying to center a list of images both vertically and horizontally within their respective li. I want to achieve this using jQuery. How can I align the images in the center of each li element both horizontally and vertically? You can view a demo at c ...

Employing YAML and Ruby together

Currently, I am in the process of creating a testcase using Selenium WebDriver paired with Ruby. It has been just a short while since I started learning Ruby. Here is the initial setup for my testcase: require "test/unit" require "selenium-webdriv ...

Utilizing Python Selenium to implement CPU throttling in Chrome

Can CPU throttling be controlled in Chrome's devtools using Python Selenium? If yes, what is the method to do so? I noticed that the driver has a method called execute_cdp_cmd, which stands for "Execute Chrome Devtools Protocol command", but I am uns ...

Exception thrown when Selenium encounters an error with IWebBrowser2::Navigate2() in GitHub actions

Currently, I am utilizing Selenium to automate tasks on my Internet Explorer application within Github actions. However, upon running the script, an error message appears: It seems that there is a failure in calling the COM method IWebBrowser2::Navigate2( ...