有谁知道为什么 GetXpathCount() 在 C# 中不起作用? [英] Does anyone know why GetXpathCount() doesn't work in C#?

查看:25
本文介绍了有谁知道为什么 GetXpathCount() 在 C# 中不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我扩展了 Selenium 命名空间.但它仍然无法识别 GetXpathCount() 函数.有谁知道解决方案?谢谢!

I've extended Selenium namespace. But it's still won't recognize GetXpathCount() function. Does anyone know a solution? Thanks!

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");

            ..........

        }

推荐答案

您似乎混合使用了 Selenium WebDriver 和 Selenium RC.

You seem to be using a mix of Selenium WebDriver and Selenium RC.

我相信这是因为这里,您正在创建一个新的驱动程序(WebDriver API):

I believe this due to here, you are creating a new Driver (WebDriver API):

Driver = CreateDriverInstance(BaseUrl);

那么在这里,您将使用 RC API(Selenium 类是 RC API 的一部分):

Then here, you are using the RC API (the Selenium class is part of the RC API):

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

您还有一个用于 OpenQA.SeleniumSelenium 的 using 指令.这也表明您的做法非常非常错误.

You also have a using directive for both OpenQA.Selenium and Selenium. This is also another indication that you are doing it very very wrong.

三件事:

  1. 决定您要使用驱动程序 API 还是 RC API.不要将两者混用,它会变得凌乱,并通过突然出现的非常奇怪的问题导致您脱发.
  2. 即使您选择使用 RC API,GetXPathCount 方法不是静态方法,这就是您会得到原始错误的原因.
  3. 无论如何,您的 XPath 是不正确的...我将假设这是某物的 ID,但我建议您正确学习 XPath 查询.
  1. Decide whether you want to use the Driver API or RC API. Do not mix between the two, it's going to get messy and cause you to lose your hair through very weird issues appearing out of nowhere.
  2. Even if you elect the use the RC API, the GetXPathCount method is not a static method, which is why you'd be getting your original error.
  3. Your XPath is incorrect anyway...I'm going to assume that is the ID of something, but I would suggest learning XPath queries properly.

建议:

推荐:由于您使用的是 C#,您可以使用 LINQ to Objects 的强大功能,并完全模仿 GetXPathCount 的功能.通过这个:

Recommended: since you are using C#, you can use the awesome power of LINQ to Objects, and mimic exactly what GetXPathCount does. Through this:

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

虽然如果这实际上只是一个 ID,你可以简单地说:

Although if this is literally just an ID, you could make it simply:

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

完全不推荐:选择使用 RC API 并使用 DefaultSelenium 类来正确实例化 Selenium类:

Not recommended at all: Elect to use the RC API and use the DefaultSelenium class to properly instantiate the Selenium class:

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 支持.

Also not recommended: Elect to use the WebDriverBackedSelenium API which will give you the old RC API whilst allowing you use the WebDriver backing.

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.

You've included using's for both NUnit and MSTest (NUnit.Framework and Microsoft.VisualStudio.TestTools.UnitTesting), and yet you seem to be using MSTest.

如果您坚持使用 MSTest,请删除您的 NUnit 引用,它只会增加混乱、增加编译时间并构建不必要的引用.

Remove your NUnit references if you are sticking with MSTest, it will only add to confusion, increase compile time and build unnecessary references.

这篇关于有谁知道为什么 GetXpathCount() 在 C# 中不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆