"no such element" exception when using Selenium with C# -
In my web project "Calculator Web" I have a web page where I have a single text box with the ID "txtNum1" Is kept
& lt ;! DOCTYPE html & gt; & Lt; Html xmlns = "http://www.w3.org/1999/xhtml" & gt; & Lt; Top & gt; & Lt; Title & gt; & Lt; / Title & gt; & Lt; / Head & gt; & Lt; Body & gt; & Lt; Form id = "frmCalc" & gt; & Lt; Div style = "padding-top: 20px;" & Gt; My sample text box & lt; Br / & gt; & Lt; Br / & gt; & Lt; Input id = "txtNum1" type = "text" / & gt; & Lt; / Div & gt; & Lt; / Form & gt; & Lt; / Body & gt; & Lt; / Html & gt;
Nothing is fancy, so the page load is as below
Now I have created a feature file" BrowserTest1.feature "for my project" Calculator. Specs ". To check the code
Feature: BrowserTest 1 URL as a browser, I want to see the URL of the page @MyTag Scenarios: By visiting the URL I have entered 'http: // localhost: 58529 / 'When I go to that URL, then there is a text box on the page with' TTTNIM1 'I / C> Then I have a browser browser browser file for the implementation of the unit test < / P>
System usage Doing tax; Using System.Collections.Generic; Using System.Linq; Using System.Text; System usage threading. Task; Use of Microsoft Visual Studio Test Tools Unitatistics; Using OpenQA.Selenium; Using OpenQA.Selenium.Chrome; Using OpenQA.Selenium.Support.UI; Using TechTalk.SpecFlow; Namespace Calculator. Speaks {{binding} class browserstst {public string urlPath; [Looking at (@ "I've signed in 'http: // localhost: 58529 /' ')) Secure Secelius () {string URLPath =" http: // localhost: 58529 "; UrlPath = URLPath;} [when ( @ "I go to that URL")] void navigateToURL ({var driver = new ChromeDriver ()} {driver.Navigate (). GoToUrl (urlpath);}} [Then ("id" txtNum1 "" Is a text box on the page)) Secure void TxtBoxExists () {bool txtExists = false; (Var driver = New ChromeDrover) (IWebElement txtBox1 = driver.FindElement (By.Id ("txtNum1")); If (txtBox1! = Null) {txtExists = true;}} responsibility. Real (true, txtExists);}}}
as far as I know that I have the necessary reference for all the spaceflow / selenium integration of Visual Studio IDE
When I run the unit test, the web page load gets cured, but I get a failed test when I try to find the element with the id "txtNum1", even if Only the text box with that ID exists on the page
Exception descriptions appear as follows
Does anyone I can indicate in the right direction how I am not able to find the text box on the page with selenium id "txtNum1",
You need to use the same example as the driver between the steps, otherwise the driver who navigates on the page is different from the driver whom you want to tr Get Ying.
You have some options for this, the easiest is to create a field for your driver's example and use the fields in all steps. It will work as long as all your steps are in the same file.
The second option ScenarioContext.Current
(or FeatureContext. Current
). These are dictionaries so that you can do this:
landscape contact. Current ["WebDriver"] = Driver;
This will work fine, but you have to remove the driver every time because the object takes the bus in the dictionary, but you will be able to share your driver image in some way . The files they are in.
The third (and the IMHO best) option is to create a WebDriverContext
class and accept it in its step class constructor. Once your driver is there (in his constructor) once and then each step can use the WebDrive content instance to access the shared driver.
An example of setting this setting
Comments
Post a Comment