Resourceful Resourceful - 3 months ago 21
C# Question

Selenium selecting a dropdown option using webdriver in c#

I am trying to click on a dropdown option selection and filter results. However, I cant seem to nail the syntax.

URL = "http://themeforest.net/item/realsite-material-real-estate-wordpress-theme/full_screen_preview/10917194"

Dropdown selection for Location : 'Palo Alto'

My attempt is below:

driver.FindElement(By.XPath("//div[@id='cover_widget-2']/div[3]/div/div/form/div/div/div/div/div/ul/li[3]/a")).Click();

driver.FindElement(By.XPath("//button[@type='submit']")).Click();


It says that the element doesnt exist but I cant figure out the syntax.

What do I seem to be doing wrong?

Answer
  1. The dropdown and button elements are in iframe name "preview-frame". You have to switch to the iframe before finding the elements.
  2. You have to click the dropdown then select item inside the list

Code below show how to find dropdown and list elements from the label Location.

driver.SwitchTo().Frame(driver.FindElement(By.Name("preview-frame")));
IJavaScriptExecutor jse = (IJavaScriptExecutor)driver;
jse.ExecuteScript("window.scrollBy(0,400)", "");
driver.FindElement(By.XPath("//label[contains(.,'Location')]/../div/button")).Click();
System.Threading.Thread.Sleep(1000);
driver.FindElement(By.XPath("//label[contains(.,'Location')]/../div/div/ul/li/a[contains(.,'Palo Alto')]")).Click();
driver.FindElement(By.XPath("//button[@type='submit']")).Click();