Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
有人知道为什么GetXpathCount()不';我不能在C#工作吗?_C#_Xpath_Selenium_Webdriver_Selenium Webdriver - Fatal编程技术网

有人知道为什么GetXpathCount()不';我不能在C#工作吗?

有人知道为什么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

我已经扩展了Selenium名称空间。但它仍然无法识别GetXpathCount()函数。有人知道解决办法吗?谢谢

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指令。这也是另一个迹象,表明您的操作非常错误

三件事:

  • 决定是使用驱动程序API还是RC API。不要将两者混为一谈,这会让你变得一团糟,并导致你因为不知从哪里冒出来的奇怪问题而掉头发
  • 即使您选择使用RC-API,
    GetXPathCount
    方法也不是一个静态方法,这就是为什么您会得到原始错误
  • 无论如何,您的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();
    

    也不推荐使用:选择使用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']");