Reputation: 125
Trying to select a word inside a dropdown menu.
In Webdriver IDE appear this to click the dropdown (which Id is "p" and the to click the word "Barcelona" inside the dropdown.:
I just can open the dropdown menu using:
driver.FindElement(By.Id("p")).Click();
Now I'm trying to select a word inside this dropdown menu, using "selectelement" and "select visibleText" but does not work in C# Webdriver for me.
SelectElement selector = new SelectElement.selectByVisibleText("Barcelona");
Any helps please?
Using C# Webdriver and not java.
Upvotes: 1
Views: 1971
Reputation: 21
In WebDriver.Support.dll Version: 3.1.0 we have SelectElement class. SelectElement mainly three methods.
Please find the methods:
Code samples: SelectByIndex
SelectElement selectObj = new SelectElement(driver.FindElement(By.Id("year")));
selectObj.SelectByIndex(4);
Code samples: SelectByText
SelectElement selectObj = new SelectElement(driver.FindElement(By.Id("year")));
selectObj.SelectByText("1990");
Code samples: SelectByValue
SelectElement selectObj = new SelectElement(driver.FindElement(By.Id("year")));
selectObj.SelectByValue("1990");
Please find attached photo for your reference Sample Image
Further clarification and other methods Refer
Upvotes: 0
Reputation: 1713
I think the problem is in the SelectElement initialization. You can try the following code:
SelectElement selectElement = new SelectElement(driver.FindElement(By.Id("p")));
selectElement.SelectByText("Germany");
If you are new in the C# WebDriver API, you can find the following article useful: http://automatetheplanet.com/getting-started-webdriver-c-10-minutes/
Upvotes: 1