有人知道为什么GetXpathCount()不';我不能在C#工作吗?
我已经扩展了Selenium名称空间。但它仍然无法识别GetXpathCount()函数。有人知道解决办法吗?谢谢有人知道为什么GetXpathCount()不';我不能在C#工作吗?,c#,xpath,selenium,webdriver,selenium-webdriver,C#,Xpath,Selenium,Webdriver,Selenium Webdriver,我已经扩展了Selenium名称空间。但它仍然无法识别GetXpathCount()函数。有人知道解决办法吗?谢谢 int count = Selenium.GetXpathCount("ctl00_ContentPlaceHolder1_TVNCategoryGridView"); 我收到以下错误消息: 命名空间“Selenium”中不存在类型或命名空间名称“GetXPathCount”(是否缺少程序集引用? 以下是整个代码结构: using System; using System.Tex
int count = Selenium.GetXpathCount("ctl00_ContentPlaceHolder1_TVNCategoryGridView");
我收到以下错误消息:
命名空间“Selenium”中不存在类型或命名空间名称“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");
..........
}
您似乎混合使用了SeleniumWebDriver和SeleniumRC 我相信这是因为您正在创建一个新的驱动程序(WebDriver API): 在这里,您使用的是RC API(Selenium类是RC API的一部分): 您还有一个用于
OpenQA.Selenium
和Selenium
的using指令。这也是另一个迹象,表明您的操作非常错误
三件事:
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();
或
也不推荐使用:选择使用WebDriverBackedElenium API,它将为您提供旧的RC API,同时允许您使用WebDriver备份
var webDriverBackedSelenium = new WebDriverBackedSelenium(Driver, "http://www.google.com");
int amountOfElementsMatchingXPath = webDriverBackedSelenium.GetXpathCount("//*[@id='ctl00_ContentPlaceHolder1_TVNCategoryGridView']");
另一项意见:
您已经为NUnit和MSTest(NUnit.Framework
和Microsoft.VisualStudio.TestTools.UnitTesting
)使用了,但您似乎正在使用MSTest
删除NUnit引用如果您坚持使用MSTest,它只会增加混乱、增加编译时间并生成不必要的引用。您使用的是哪个版本的selenium?这是正确的吗?xpath的格式应为//div。请参考此链接:向我们展示您的全部代码,包括您首先开始Selenium的位置。您看起来像是在使用RC或WebDriverBackedElenium,但您已将问题标记为
webdriver
…那么您在使用什么呢?我在上面添加了代码结构
ISelenium selenium = new DefaultSelenium("localhost", 4444, "*chrome", "http://www.google.com");
selenium.Start();
int amountOfElementsMatchingXPath = selenium.GetXpathCount("//*[@id='ctl00_ContentPlaceHolder1_TVNCategoryGridView']");
selenium.Stop();
var webDriverBackedSelenium = new WebDriverBackedSelenium(Driver, "http://www.google.com");
int amountOfElementsMatchingXPath = webDriverBackedSelenium.GetXpathCount("//*[@id='ctl00_ContentPlaceHolder1_TVNCategoryGridView']");