0

在 NUnit 中运行时出现以下错误,

在此处输入图像描述

我正在查找一个元素并将其存储在一个变量中,并且在尝试选择该元素时,我得到了错误。试过用这种方式

IWebElement fromitem = WebDriver.FindElement(By.Id("from"));但同样的错误仍然存​​在。有什么办法可以选择元素吗?

注意:我用firebug验证了元素id,似乎没有问题。

下面的代码,

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

    }

  }
}
4

1 回答 1

2

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.

于 2013-10-24T11:02:27.410 回答