2013-10-24 43 views
0

在NUnit的運行時,我得到下面的錯誤,選擇元素的問題 - webdriver的 - C#

enter image description here

我發現一個元素,並將其存儲在一個變量,而試圖選擇元素,我收到錯誤。試圖用這種方式使用

IWebElement fromitem = WebDriver.FindElement(By.Id("from"));但同樣的錯誤依然存在。 有什麼辦法可以選擇元素?

注意:我用螢火蟲驗證了元素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()); 

    } 

    } 
} 
+0

給我聽起來像你的引用搞砸了。您是否在應用程序中引用了支持庫?複製本地設置爲真?在你正在啓動測試的文件夾中,在'bin'文件夾中,這兩個WebDriver DLL都在那裏? – Arran

+0

存在兩個webdriver DLL的Bin文件夾。支持庫被引用。對於nunit框架引用,Copy-Local設置爲true。現在我設置Copy-Local true爲webdriver引用,但仍然無效。 –

+0

@Arran - 你想讓我爲所有引用(包括系統引用)設置Copy-Local爲True嗎? –

回答

2

SelectElement類只能與實際HTML <select>元件一起使用。在您提供的頁面中,有問題的元素是<input>元素,通過CSS和JavaScript添加了功能,使其能夠像下拉列表一樣操作。因此,試圖與SelectElement類一起使用它將引發異常,表明該元素不是正確的類型。

「文件不存在」錯誤消息是一個紅色的鯡魚。它只是在那裏,因爲NUnit試圖向您展示拋出異常的源代碼行,這是WebDriver源代碼的一部分。該行代碼引發的異常應顯示在NUnit中的某個位置,該位置應包含適當的信息性消息。