Vignesh Paramasivam
Vignesh Paramasivam

Reputation: 2480

Select element issue - webdriver - c#

I'm getting the below error while running in NUnit,

enter image description here

I'm finding an element and storing it in a variable, and while trying to select the element, i'm getting the error. Tried using in this way

IWebElement fromitem = WebDriver.FindElement(By.Id("from")); but same error persists. Is there any way i can select the element?

Note : I verified the element id with firebug, there seems to be no problem with it.

The code below,

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Support.UI;
using OpenQA.Selenium.Remote;
using OpenQA.Selenium.Support.Events;
using OpenQA.Selenium.Support.PageObjects;

namespace SeleniumTests
{
[TestFixture]  
public class Sel
{
    public static IWebDriver WebDriver;
    private String baseURL;

    [SetUp] 
    public void SetupTest()
    {
        WebDriver = new FirefoxDriver(); 
        baseURL = "http://www.x-rates.com/calculator.html";
    }

    [TearDown] 
    public void TeardownTest()
    {
        WebDriver.Quit(); 
    }

    [Test]
    public void newtest()
    {
        WebDriver.Navigate().GoToUrl(baseURL + "/");

        var fromitem = WebDriver.FindElement(By.Id("from"));
        var toitem = WebDriver.FindElement(By.Id("to"));

        var fromval = new SelectElement(fromitem); //Error occurs

        var toval = new SelectElement(toitem);
        fromval.SelectByText("USD - US Dollar");
        toval.SelectByText("INR - Indian Rupee");
        WebDriver.FindElement(By.LinkText("Currency Calculator")).Click();
        var curval =   WebDriver.FindElement(By.CssSelector("span.ccOutputRslt")).GetAttribute("Value");
        var expectedValue = 61.456825;


        Thread.Sleep(900);

        Assert.AreEqual(expectedValue, curval.Trim());

    }

  }
}

Upvotes: 0

Views: 2871

Answers (1)

JimEvans
JimEvans

Reputation: 27496

The SelectElement class can only be used with actual HTML <select> elements. In the page you provided, the element in question is an <input> element, with functionality added through CSS and JavaScript to enable it to act like a drop-down list. As such, attempting to use it with the SelectElement class will throw an exception indicating that the element is not of the correct type.

The "File does not exist" error message is a red herring. It's only there because NUnit is trying to show you the line of source code where the exception is thrown, which is a part of the WebDriver source code. The exception thrown by that line of code should be displayed somewhere within NUnit, which should contain the appropriate informational message.

Upvotes: 2

Related Questions