Frank
Frank

Reputation: 75

Trying to select item in drop down in Selenium

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.

Upvotes: 2

Views: 3841

Answers (2)

Raavan
Raavan

Reputation: 107

  • First select the drop down and then select by value or Index-

    Select drpdown = new Select(driver.findElement(By.xpath("Locator of the dropdown")); drpdown.SelectByValue("Value mentioned in the DOM");

  • If "Earning Report" is a visible Text then

    drpdown.selectByVisibleText("visible text of the value");

Upvotes: 0

Saifur
Saifur

Reputation: 16201

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();

Upvotes: 3

Related Questions