I want to pull all the items in the drop-down menu one by one into the datagridview rows using Selenium in C#. Can you help me on this issue?
Loading
I want to pull all the items in the drop-down menu one by one into the datagridview rows using Selenium in C#. Can you help me on this issue?
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Mehmet FatihPosted Jan 18, 2024, 8:51 PM
The problem was largely solved by Ali Imran Ansari. I express my special thanks for what it contains. However, there is only one problem: the first line lists all dropdown elements on a single line. How can we prevent this? Please take a look at the picture to understand what I mean.
Mehmet FatihPosted Jan 18, 2024, 8:10 AM
First of all, thank you very much for your interest. However, these codes give the same results as I got before. Instead of listing the courses one by one, it only gives the number of dropdown elements.
Jithu ThomasPosted Jan 18, 2024, 4:04 AM
Please try to follow this basic example and try: -
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using System;
using System.Collections.Generic;
using System.Windows.Forms;
class Program
{
static void Main()
{
// Initialize the WebDriver (assuming Chrome in this example)
IWebDriver driver = new ChromeDriver();
// Navigate to the webpage with the dropdown menu
driver.Navigate().GoToUrl("your_website_url_here");
// Locate the dropdown element (replace "your_dropdown_id" with the actual ID or other locator)
IWebElement dropdown = driver.FindElement(By.Id("your_dropdown_id"));
// Get all options from the dropdown options = new List(dropdown.FindElements(By.TagName("option")));
List
// Create a DataGridView
DataGridView dataGridView = new DataGridView();
dataGridView.Columns.Add("ColumnHeader", "ColumnHeader");
// Iterate through options and populate DataGridView
foreach (IWebElement option in options)
{
string optionText = option.Text;
// Add the option text to the DataGridView
dataGridView.Rows.Add(optionText);
}
// Display the populated DataGridView (you might want to add this to a form or handle it according to your application)
MessageBox.Show("Dropdown options retrieved and added to DataGridView!");
// Close the browser
driver.Quit();
}
}
Please accept the answer if you find it useful.