1

I am trying to select an item from a drop down list. I have put the code in the section below. My research has not been conclusive though:

Here is the code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Support.UI;
using System.Globalization;
using System.Threading.Tasks;

namespace CV_Lib_Do_Not_Use
{
    class Program
    {
        static void Main(string[] args)
        {
            //Instantiate Firefox Driver
            //Go to CV Library Website
            var driver = new FirefoxDriver();
            driver.Navigate().GoToUrl("https://www.cv-library.co.uk/");

            //Wait for web element to become visible
            System.Threading.Thread.Sleep(15000);


            //Click on Search Jobs tab
            driver.FindElement(By.Id("header-link-searchjobs")).Click();

            //Use the 'Keywords / Job Title / Job Ref' search field to search for a Test Analyst position 
            var position = driver.FindElement(By.XPath(".//*[@id='searchjobs_form']/div[1]/div[1]/input"));

            //Type Test Analyst in the search box
            position.SendKeys("Test Analyst");

            //Use the 'Keywords / Job Title / Job Ref' search field to search for a Test Analyst position 
            var location = driver.FindElement(By.XPath(".//*[@id='location']"));

            //Type Manchester in the search box
            location.SendKeys("Manchester");


            //Identify xpath for distance from desired location 
            var distance = driver.FindElement(By.XPath(".//*[@id='distance_adv']"));


            //Click on 10 miles from the drop-down
            driver.FindElement(By.XPath(".//*[@id='distance_adv']/option[6]")).Click();



        }
    }
}
2

You can/should utilize the SelectElement class which conveniently abstracts away the "select-option" HTML blocks:

var distance = driver.FindElement(By.Id("distance_adv"));
var selectDistance = new SelectElement(distance);

selectDistance.SelectByText("10 miles");

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.