Try to select the item from the drop-down list in Selenium

advertisements

I've been trying to select the sexual orientation from the website here: https://www.okcupid.com/ but I can't seem to get it. I've tried everything from clicking on the element that I want in the drop down with the .Click() method, and then to the support class that's for this (SelectElement) like so:

driver.Navigate().GoToUrl("https://www.okcupid.com/");
new SelectElement(driver.FindElementByCssSelector("#orientation_dropdownContainer")).SelectByValue("2");

I've tried SelectbyText() as well, and I have tried on different elements (honestly all of them I think) that I could think to use this one and it still stays on the default option, any ideas guys? Using Selenium - Firefox.


The selector seems wrong to me. Use id for the select element orientation_dropdown

driver.Navigate().GoToUrl("https://www.okcupid.com/");
new SelectElement(_driver.FindElement(By.Id("orientation_dropdown"))).SelectByValue("2");

Edit

This is one of the weirdest select list I have ever seen. However, the code above will not work and found using Actions class can be useful and it will work

string option = "Gay";
By xPath = By.XPath("//li[contains(text(),'"+option+"')]");

Actions actions = new Actions(_driver);
actions.MoveToElement(_driver.FindElement(By.Id("orientation_dropdown_chosen"))).Click().Build().Perform();
_driver.FindElement(xPath).Click();