Exception "no such element" when using Selenium with C #

I have a web page in my Calculator web project where I have placed one text field with the id "txtNum1"

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
</head>
<body>
    <form id="frmCalc">
        <div style="padding-top:20px;">
            My Sample Text Box <br/><br/>
            <input id="txtNum1" type="text" />
        </div>
    </form>
</body>
</html>

      

Nothing fancy, so the page loads like below

Web Page Preview

Now I have created a properties file "BrowserTest1.feature" in my "Calculator.Specs" project. The code is as follows

Feature: BrowserTest1
In order to check url
As browser
I want to be see url of page

@mytag
Scenario: Go to URL
    Given I have entered 'http://localhost:58529/'
    When I go to that url
    Then there is a text box on the page with id 'txtNum1'

      

Then I code the BrowserTest.cs file to implement the unit tests

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Support.UI;
using TechTalk.SpecFlow;

namespace Calculator.Specs
{
    [Binding]
    class BrowserTest
    {

        public string urlPath;


        [Given(@"I have entered 'http://localhost:58529/'")]
        protected void SetURL()
        {
            string URLPath = "http://localhost:58529/";
            urlPath = URLPath;
        }

        [When(@"I go to that url")]
        protected void NavigateToURL()
        {
             using (var driver = new ChromeDriver())
             {
                 driver.Navigate().GoToUrl(urlPath);
             }
        }


        [Then(@"there is a text box on the page with id 'txtNum1'")]
        protected void TxtBoxExists()
        {
            bool txtExists = false;
            using (var driver = new ChromeDriver())
            {
                IWebElement txtBox1 = driver.FindElement(By.Id("txtNum1"));

                if (txtBox1 != null)
                {
                    txtExists = true;
                }
            }
            Assert.AreEqual(true, txtExists);
        }
    }
}

      

As far as I know I have all the necessary links for Visual Studio IDE to integrate SpecFlow / Selenium

References Section

When I run the unit test, the web page loads fine, however I get a failed test when I try to find an element with id "txtNum1" even though a text field with that id exists on the page

Exception Message

Exception details are as follows

Exception Details

Can anyone point me in the right direction as to what I am missing to let Selenium find the text field on the page with id "txtNum1"

+3


source to share


3 answers


you need to use the same driver instance between steps, otherwise the driver that is navigating to the page is different from the driver you are trying to get the element from.

you have several options for this. The simplest is to create a field for your driver instance and use the field in all steps. This will work as long as all your steps are in the same file.

Another option is to store the driver object in ScenarioContext.Current

(or FeatureContext.Current

). They are dictionaries, so you can do this:

ScenarioContext.Current["webDriver"] = driver;

      



This will work fine, but you'll have to throw every time you get the driver, since the dictionary just accepts objects, but you will be able to share your driver instance between steps no matter what file they are in.

The third (and best option IMHO) is to create a class WebDriverContext

and accept it in your step class constructor. Create your driver once there (in its constructor) and then each step can use the WebDriverContext instance to access the shared driver.

An example of setting this parameter can be seen in here

+1


source


Your driver just has to go a little slower

Add a delay before each one driver.FindElement

, for example:

Thread.Sleep(2000);
IWebElement txtBox1 = driver.FindElement(By.Id("txtNum1"));

      



Or better yet:

WebDriverWait wait = new WebDriverWait(driver, new TimeSpan(0,0,10));
wait.Until(ExpectedConditions.ElementIsVisible(By.Id("txtNum1")));
IWebElement txtBox1 = driver.FindElement(By.Id("txtNum1"));

      

+1


source


Found the answer after a little code analysis. This issue is related to multiple instances of ChromeDriver in the code. So the first instance was loading the page, but when I tried to find an element on the page, I was using a new instance of the page, not the one already used to load the page. So this second instance of the chrome driver without loading the page would always return an exception as no item was found as there was no page on this instance of the chrome driver

0


source







All Articles