2013-02-20 82 views
0

我擴展了Selenium命名空間。但它仍然不能識別GetXpathCount()函數。有誰知道解決方案?謝謝!有誰知道爲什麼GetXpathCount()在C#中不起作用?

int count = Selenium.GetXpathCount("ctl00_ContentPlaceHolder1_TVNCategoryGridView"); 

我得到了以下錯誤消息:

類型或命名空間名稱GetXPathCount'不存在命名空間「硒」存在(?是否缺少程序集引用)

以下是整個代碼結構:

using System; 
using System.Text; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text.RegularExpressions; 
using Microsoft.VisualStudio.TestTools.UnitTesting; 
using Selenium; 
using OpenQA.Selenium; 
using OpenQA.Selenium.Firefox; 
using OpenQA.Selenium.Support.UI; 
using System.Threading; 
using NUnit.Framework; 

    .......(test class extending base test) 


    public void TestSetup() 
     { 

      Driver = CreateDriverInstance(BaseUrl); 
      Driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(5)); 
      Driver.SwitchTo().Window(Driver.CurrentWindowHandle); 



     } 
     [TestCleanup()] 
     public void TestCleanup() 
     { 
      Driver.Quit(); 
     } 



[Priority(1), TestMethod] 
     public void NewShowTest() 
     { 

      Open("~/NewShow.aspx"); 
      Random rnd = new Random(DateTime.Now.Second); 
      string shownum = DateTime.Now.ToShortDateString() + " " + DateTime.Now.ToShortTimeString() + " " + rnd.Next(0, 10000).ToString(); 
      testShowName = "Test Show " + shownum; 
      int count = Selenium.GetXpathCount("ctl00_ContentPlaceHolder1_TVNCategoryGridView"); 

      .......... 

     } 
+0

你正在使用哪個版本的硒?該xpath是否正確? xpath應該是// div的格式。請參閱以下鏈接:http://selenium.googlecode.com/git/docs/api/dotnet/html/M_Selenium_ISelenium_GetXpathCount.htm – Santoshsarma 2013-02-20 04:42:13

+0

向我們展示您的整個代碼,包括您首先啓動Selenium的位置。你看起來像你正在使用RC或WebDriverBackedSelenium,但你已經用'webdriver'標記了這個問題......所以你在用什麼? – Arran 2013-02-20 09:34:25

+0

我加了上面的代碼結構 – 2013-02-20 18:16:28

回答

1

您似乎在使用Selenium WebDriver和Selenium RC的混合。

我相信這是由於在這裏,您將創建一個新的驅動程序(webdriver的API):

Driver = CreateDriverInstance(BaseUrl); 

然後在這裏,您使用的是RC API(該Selenium類是RC API的一部分):

int count = Selenium.GetXpathCount("ctl00_ContentPlaceHolder1_TVNCategoryGridView"); 

你也有一個使用指令兩個OpenQA.SeleniumSelenium。這也是另一個跡象,你正在這樣做非常非常錯誤

三件事:

  1. 決定是否要使用驅動程序API或RC API。不要在兩者之間混淆,它會變得混亂,並通過非常奇怪的問題讓你失去頭髮。
  2. 即使您選擇使用RC API,GetXPathCount方法不是靜態方法,這就是爲什麼你會得到你的原始錯誤。
  3. 您的XPath不正確無論如何...我會假設這是某個東西的ID,但我會建議正確學習XPath查詢。

建議:

推薦:因爲你使用的是C#,你可以使用LINQ的真棒權力對象,模仿正是什麼GetXPathCount一樣。通過這樣的:

Driver.FindElement(By.XPath("//*[@id='ctl00_ContentPlaceHolder1_TVNCategoryGridView']")).Count; 

雖然如果這是真的只是一個ID,你可以把它簡單地說:

Driver.FindElement(By.Id("ctl00_ContentPlaceHolder1_TVNCategoryGridView")).Count; 

不建議在所有:選擇使用RC API並使用DefaultSelenium類到正確實例化Selenium類:

ISelenium selenium = new DefaultSelenium("localhost", 4444, "*chrome", "http://www.google.com"); 
selenium.Start(); 
int amountOfElementsMatchingXPath = selenium.GetXpathCount("//*[@id='ctl00_ContentPlaceHolder1_TVNCategoryGridView']"); 
selenium.Stop(); 

也不推薦:選擇使用WebDriverBackedSelenium API,它會給你的老RC API,同時允許您使用webdriver的後盾。

var webDriverBackedSelenium = new WebDriverBackedSelenium(Driver, "http://www.google.com"); 
int amountOfElementsMatchingXPath = webDriverBackedSelenium.GetXpathCount("//*[@id='ctl00_ContentPlaceHolder1_TVNCategoryGridView']"); 

另一種看法:

你使用的爲既包括 NUnit的和MSTest的(NUnit.FrameworkMicrosoft.VisualStudio.TestTools.UnitTesting),但你似乎可以用MSTest的。

如果你堅持MSTest,刪除你的NUnit引用,它只會增加混淆,增加編譯時間和建立不必要的引用。

相關問題