简体   繁体   中英

Does anyone know why GetXpathCount() doesn't work in C#?

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

i got the following error message:

The type or namespace name 'GetXPathCount' does not exist in the namespace 'Selenium' (are you missing an assembly reference?)

Here's the entire code structure:

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

            ..........

        }

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

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

Driver = CreateDriverInstance(BaseUrl);

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

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

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 .

Three things:

  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.

Suggestions:

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;

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

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

or

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

or

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

Another observation:

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM