Issue
I am trying to populate a Combobox in a WinForms Application with text and values extracted from a website.
CODE - Selenium using Visual Studio
List<IWebElement> options = driver.FindElements(By.XPath("//*[@id='id_object']/option")).ToList();
comboBox1.Items.AddRange(options.ToArray());
HTML CODE
<select name="id_object" id="object" style="width:100%;">
<option selected="selected" value="0">-- Pick one --</option>
<option value="123">First</option>
<option value="1245">Second</option>
<option value="157">Third</option>
....
</select>
However, the combobox gets populated with the following values and it doesn't seem to relate to the values in html code.
It seems to be a kind of formatting issue.
How can I populate the combobox, with text and values?
Solution
You need to get the text and attribute value and then add those into ComboBox
.
List<IWebElement> options = driver.FindElements(By.XPath("//*[@id='id_object']/option"));
for (int i = 0; i < options.Count; i++)
{
comboBox1.Items.Add(options.ElementAt(i).Text + " , " + options.ElementAt(i).GetAttribute("value"));
}
Answered By - KunduK
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.