2013-02-20 80 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引用,它只会增加混淆,增加编译时间和建立不必要的引用。

相关问题