Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7178573
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T16:56:52+00:00 2026-05-28T16:56:52+00:00

Below I’m debugging and throwing an exception on purpose to find out the value

  • 0

Below I’m debugging and throwing an exception on purpose to find out the value of a JavaScript call from WebDriver. How can I cast the jQuery call so I can print a string (based on the number of tr tags in my table with id of “viewtable”) in my exception message? I imagine this has absolutely nothing to do with the C# code. I bet the driver can’t execute the jQuery call properly, but I don’t know the correct syntax.

Exception thrown by NUnit:

Selenium.ProductPricing.TheUntitledTest:
System.InvalidCastException : Unable to cast object of type 'System.Int64' to type 'System.String'.

Environment:

  • Class Library project is called Selenium.sln/Selenium.csproj
  • project referencing NUnit dll & Selenium C# client drivers including WebDriver dll files
  • project has one class class called ProductPricing.cs
  • running class library dll in NUnit 2.6

Test Case C# code:
(search for “BAD!” below)

using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Support.UI;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.IE;
using OpenQA.Selenium.Chrome;
using Selenium;
using System.Text;
using System;

namespace Selenium
{
    [TestFixture]
    public class ProductPricing
    {
        private IWebDriver driver;
        private StringBuilder verificationErrors;
        private string baseURL;

        [SetUp]
        public void Setup()
        {
            driver = new FirefoxDriver();
            baseURL = "http://buyemp.qa.xxx.com/";

            ISelenium selenium = new WebDriverBackedSelenium(driver, baseURL);
            selenium.Start();

            verificationErrors = new StringBuilder();
        }

        [TearDown]
        public void TeardownTest()
        {
            try
            {
                driver.Quit();
            }
            catch (Exception)
            {
                // Ignore errors if unable to close the browser
            }
            Assert.AreEqual("", verificationErrors.ToString());
        }

        [Test]
        public void TheUntitledTest()
        {
            //String var_skip_product = "false";
            String var_admin_user = "coders@xxx.com";
            String var_admin_pass = "notsure";
            driver.Navigate().GoToUrl(baseURL + "/admin");
            driver.FindElement(By.Id("email")).Clear();
            driver.FindElement(By.Id("email")).SendKeys(var_admin_user);
            driver.FindElement(By.Id("password")).Clear(); 
            driver.FindElement(By.Id("password")).SendKeys(var_admin_pass);
            driver.FindElement(By.CssSelector("input[type=\"submit\"]")).Click();            
            driver.WaitForElement(By.LinkText("Products"));
            driver.FindElement(By.LinkText("Products")).Click();
            String var_product_row = "24";  // force script to start on row 24/25

            //// ERROR: Caught exception [unknown command [getTableTrCount]]
            // Command: getTableTrCount | Target: viewtable | Value: var_table_row_count (user extensions don't work in WebDriver)
            IJavaScriptExecutor js = driver as IJavaScriptExecutor;

            // this one throws an exception with value 22 - GOOD!
            //int x = Convert.ToInt32((string)js.ExecuteScript("return '22'"));

            // this one throws an exception with the cast exception - BAD!
            int x = Convert.ToInt32((string)js.ExecuteScript("return $('#viewtable tr').length"));

            // explicitly throwing Selenium exception so we can debug this code in NUnit
            throw new SeleniumException(x.ToString());

            // Command: storeText | Target: //a[@title='last page']/text() | Value: var_page_total_text
            // Conversion: String var_page_total_text = driver.FindElement(By.XPath("//a[@title='last page']/text()")).Text;
            String var_page_total_text = driver.FindElement(By.XPath("//a[@title='last page']")).Text;

            //// ERROR: Caught exception [ERROR: Unsupported command [getEval]]
            // Command: eval | Target: javascript{storedVars['var_page_total_text'].substring(1,storedVars['var_page_total_text'].length-1)}
            //int var_page_total = Convert.ToInt32(var_page_total_text.Substring(1,var_page_total_text.Length-1));           

        }    

        private bool IsElementPresent(By by)
        {
            try
            {
                driver.FindElement(by);
                return true;
            }
            catch (NoSuchElementException)
            {
                return false;
            }
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T16:56:53+00:00Added an answer on May 28, 2026 at 4:56 pm

    Just from the exception I’m assuming that ExecuteScript is returning an int64 for $("query").length and a string for $("query").html().

    So you might want to try this:

    string x = js.ExecuteScript("return $('#viewtable tr').length").ToString();
    

    or if you prefer a number:

    long x = (long)js.ExecuteScript("return $('#viewtable tr').length");
    

    Not sure about the second one but the first one should work.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does anyone know how can I replace this 2 symbol below from the string
Below is my $.ajax call, how do I put a selects (multiple) selected values
Below are lines from the c++ programming language template<class T > T sqrt(T );
Below is the plugin code that i have tried to reuse from an old
Below I have a Json function and some php code which it retrieves. JavaScript
Below I have a jquery code which includes a textbox where the user can
Below is the code I use in Chrome extension which gets a url from
Below is my code behind-file in my project, and as you can see i'm
Below is excerpt from Google Developer Console typeof Object // type of Object is
//below In my application i have a button select image from sdcard and on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.