1

Am very new to automation and am trying to automate a registration page and get the output in a text file using cucumber framework. but it seems my Then part does not have right piece of code and also getting the following error : org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element Below are details: Feature: Customer Registration Scenario Outline: Validation of the registration form Given I am able to access customer registration form When I try to fill in "" "" and "" And I click on Submit Then I should be able to navigate to Registered Successfully page with customer details

        Examples: 
        | customerName  | age | address | phoneNumber  | email     |
        | testuserOne   | 21  | London  | 789456123    |xyz@xyz.com|
        | testuserTwo   | 23  | Paris   | 789456128    |xvyxvy.com |



    Step file:



package StepDefintion;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;
import java.io.*;


public class stepDefintion{

    WebDriver driver;

    @Given("^I am able to access customer registration form$")
    public void i_am_able_to_access_customer_registration_form() throws Throwable 
    {
        System.setProperty("webdriver.chrome.driver", "D:\\learning\\chromedriver_win32\\chromedriver.exe");
        driver= new ChromeDriver(); 
        driver.get("http://apps.e-box.co.in/CustomerRegistration/Index");

    }

    @When("^I try to fill in \"([^\"]*)\" (\\d+) \"([^\"]*)\" (\\d+) and \"([^\"]*)\"$")
    public void i_try_to_fill_in_and(String arg1, int arg2, String arg3, int arg4, String arg5) throws Throwable 
    {
      driver.findElement(By.name("cname")).sendKeys(arg1);
      driver.findElement(By.name("age")).sendKeys(String.valueOf(arg2));
      driver.findElement(By.name("address")).sendKeys(arg3);
      driver.findElement(By.name("phonenumber")).sendKeys(String.valueOf(arg4));
      driver.findElement(By.name("email")).sendKeys(arg5);

    }

    @When("^I click on Submit$")
    public void i_click_on_Submit() throws Throwable 
    {
     driver.findElement(By.id("submit")).click();
    }



    @Then("^I should be able to navigate to Registered Succesfully page with customer details$")
    public void i_should_be_able_to_navigate_to_Registered_Succesfully_page_with_customer_details() throws Throwable, IOException 
    {
        String S1 = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//h2[normalize-space()='Registered Succesfully']//following::td[2]"))).getAttribute("innerHTML");       

        File file = new File ("D:\\learning\\CucumberParameterization\\src\\output.txt");
        FileWriter FW = new FileWriter (file);
        PrintWriter PW = new PrintWriter (FW);

        PW.print(S1);

        PW.close();

    }


}




        Error Stack Trace :

            org.openqa.selenium.WebDriverException: chrome not reachable
      (Session info: chrome=64.0.3282.167)
      (Driver info: chromedriver=2.35.528161 (5b82f2d2aae0ca24b877009200ced9065a772e73),platform=Windows NT 6.1.7601 SP1 x86_64) (WARNING: The server did not provide any stacktrace information)
    Command duration or timeout: 4.01 seconds
    Build info: version: '2.47.1', revision: '411b314', time: '2015-07-30 02:56:46'
    System info: host: '****', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_161'
    Driver info: org.openqa.selenium.chrome.ChromeDriver
    Capabilities [{applicationCacheEnabled=false, rotatable=false, mobileEmulationEnabled=false, networkConnectionEnabled=false, chrome={chromedriverVersion=2.35.528161 (5b82f2d2aae0ca24b877009200ced9065a772e73), userDataDir=***\AppData\Local\Temp\scoped_dir8160_2912}, takesHeapSnapshot=true, pageLoadStrategy=normal, databaseEnabled=false, handlesAlerts=true, hasTouchScreen=false, version=64.0.3282.167, platform=XP, browserConnectionEnabled=false, nativeEvents=true, acceptSslCerts=false, acceptInsecureCerts=false, locationContextEnabled=true, webStorageEnabled=true, browserName=chrome, takesScreenshot=true, javascriptEnabled=true, cssSelectorsEnabled=true, setWindowRect=true, unexpectedAlertBehaviour=}]
    Session ID: cf56a3d609cb4b5dd6b20413ec2fbdab
    *** Element info: {Using=xpath, value=//h2[normalize-space()='Registered Succesfully']//following::td[2]}
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
        at java.lang.reflect.Constructor.newInstance(Unknown Source)
        at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:206)
        at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:158)
        at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:595)
        at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:348)
        at org.openqa.selenium.remote.RemoteWebDriver.findElementByXPath(RemoteWebDriver.java:445)
        at org.openqa.selenium.By$ByXPath.findElement(By.java:358)
        at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:340)
        at org.openqa.selenium.support.ui.ExpectedConditions.findElement(ExpectedConditions.java:861)
        at org.openqa.selenium.support.ui.ExpectedConditions.access$0(ExpectedConditions.java:859)
        at org.openqa.selenium.support.ui.ExpectedConditions$7.apply(ExpectedConditions.java:205)
        at org.openqa.selenium.support.ui.ExpectedConditions$7.apply(ExpectedConditions.java:1)
        at org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:209)
        at StepDefintion.stepDefintion.i_should_be_able_to_navigate_to_Registered_Succesfully_page_with_customer_details(stepDefintion.java:50)
        at ?.Then I should be able to navigate to Registered Succesfully page with customer details(D:/learning/CucumberParameterization/src/featureFiles/Customer.feature:8)
B Gupta
  • 23
  • 4
  • Possible duplicate of [NoSuchElementExeption, selenium unable to locate element](https://stackoverflow.com/questions/48471321/nosuchelementexeption-selenium-unable-to-locate-element) – undetected Selenium Feb 15 '18 at 13:51

4 Answers4

0

The error says it all :

org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"/html/body/table/tbody/tr[1]/td[2]"}

Seems WebDriver instance is unable to locate the WebElement and you may require to induce some WebDriverWait for the element to be visible as follows :

String S1 = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//h2[normalize-space()='Registered Succesfully']//following::td[2]"))).getAttribute("innerHTML");

Here you can find a detailed discussion on NoSuchElementException


Update A :

As per your comment now you are seeing the following error :

org.openqa.selenium.WebDriverException: chrome not reachable (Session info: chrome=63.0.3239.132) (Driver info: chromedriver=2.33.506120 (e3e53437346286c0bc2d2dc9aa4915ba81d9023f),platform=Windows NT 6.1.7601 SP1 x86_64) (WARNING: The server did not provide any stacktrace information) Command duration or timeout: 4.01 seconds Build info: version: '2.47.1', revision: '411b314', time: '2015-07-30 02:56:46' *** Element info: {Using=xpath, value=//h2[normalize-space()='Registered Succesfully']//following::td[2]}

The main reason is the binaries you are using are not compatible as :

  • You are using chromedriver=2.33
  • Release Notes of ChromeDriver=2.33 clearly mentions the following :

Supports Chrome v60-62

  • Your Chrome Browser version is 63.x

So there is a clear mismatch between ChromeDriver and Chrome Browser

Solution

  • Upgrade your ChromeDriver to recent ChromeDriver=2.35 level.
  • As per the Release Notes of ChromeDriver=2.35 upgrade your Chrome Browser to Chrome v62-64 levels.
  • If your base Chrome Browser version is too old uninstall Chrome Browser through Revo Uninstaller and install a recent released GA version of Chrome Browser
  • Execute your Tests

Update B :

As per your comment as you are seeing the error as :

org.openqa.selenium.WebDriverException: chrome not reachable (Session info: chrome=64.0.3282.167) (Driver info: chromedriver=2.35.528161 (5b82f2d2aae0ca24b877009200ced9065a772e73),platform=Windows NT 6.1.7601 SP1 x86_64) (WARNING: The server did not provide any stacktrace information) Command duration or timeout: 4.01 seconds Capabilities [{applicationCacheEnabled=false, rotatable=false, mobileEmulationEnabled=false, networkConnectionEnabled=false, chrome={chromedriverVersion=2.35.528161
  • You are using Selenium Version 2.47.1 (released 2015-07-30 02:56:46) [as per the error stack trace within your question]

  • So the time gap between the release of Selenium Version 2.52.0 and ChromeDriver=2.35 is almost 3 Years and are incompatible. Hence ChromeDriver is unable to spawn the new Chrome Browser process.

Solution

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Sorry but this isnt helping as well, getting below errors: org.openqa.selenium.WebDriverException: chrome not reachable (Session info: chrome=63.0.3239.132) (Driver info: chromedriver=2.33.506120 (e3e53437346286c0bc2d2dc9aa4915ba81d9023f),platform=Windows NT 6.1.7601 SP1 x86_64) (WARNING: The server did not provide any stacktrace information) Command duration or timeout: 4.01 seconds Build info: version: '2.47.1', revision: '411b314', time: '2015-07-30 02:56:46' *** Element info: {Using=xpath, value=//h2[normalize-space()='Registered Succesfully']//following::td[2]} – B Gupta Feb 15 '18 at 08:18
  • Added an update to my answer. let me know the status. – undetected Selenium Feb 15 '18 at 10:41
  • Did what you mentioned. it didnt work:- org.openqa.selenium.WebDriverException: chrome not reachable (Session info: chrome=64.0.3282.167) (Driver info: chromedriver=2.35.528161 (5b82f2d2aae0ca24b877009200ced9065a772e73),platform=Windows NT 6.1.7601 SP1 x86_64) (WARNING: The server did not provide any stacktrace information) Command duration or timeout: 4.01 seconds Capabilities [{applicationCacheEnabled=false, rotatable=false, mobileEmulationEnabled=false, networkConnectionEnabled=false, chrome={chromedriverVersion=2.35.528161 – B Gupta Feb 15 '18 at 13:44
  • Can you update the question with your current code block and the complete stack trace for analysis? – undetected Selenium Feb 15 '18 at 13:47
  • made the changes, i'll check the selenium compatibility. – B Gupta Feb 16 '18 at 06:19
  • Have a question, how did you find "//h2[normalize-space()='Registered Succesfully']//following::td[2]" x path, i mean which element did you inspect. – B Gupta Feb 16 '18 at 06:27
  • Thanks your solution worked after selenium compatibility check, am bale to get the output pretty well after the wait and correct Xpath. – B Gupta Feb 19 '18 at 07:01
0

Looks like you are using absolute Xpath and webdriver is not able to locate element using it. You can try with this Xpath :

By.xpath("//h2[text()='Registered Succesfully']//following::tr[1]/td[1]"));
Indrapal Singh
  • 364
  • 1
  • 2
  • 13
0

You are missing form tag which contains table inside it.

You can use this XPATH :-

String S1= driver.findElement(By.xpath("/html/body/form/table/tbody/tr[1]/td[2]/input")).getText();

or //td[text()='Customer Name']/following-sibling::td/input

Ankur Singh
  • 1,239
  • 1
  • 8
  • 18
  • Thanks but this xpath is also throwing same error trace – B Gupta Feb 15 '18 at 08:16
  • org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"/html/body/form/table/tbody/tr[1]/td[2]/input"} – B Gupta Feb 15 '18 at 08:42
  • are you sure you can take input field value before submitting it ? – Ankur Singh Feb 15 '18 at 08:54
  • Sorry but am fairly new to automation and programming.Not sure what you are referring to, i believe am trying to take input value after submitting it from the "Registered successfully" page. I was trying to create the process of getting the output in a text file by simplest possible way without complicating it for a new learner like me. – B Gupta Feb 15 '18 at 09:23
-1

The others already said everything. I just want to add: when you have such an error, it is really helpful to check if the xpath of the element is correct. I found this answer that explains really good how: How to verify an XPath expression in Chrome Developers tool or Firefox's Firebug?

Firefox

  1. Either select "Web Console" from the Web Developer submenu in the Firefox Menu (or Tools menu if you display the menu bar or are on Mac OS X)
  2. or press the Ctrl+Shift+K (Command+Option+K on OS X) keyboard shortcut.

    In the command line at the bottom use the following:

    • $(): Returns the first element that matches. Equivalent to document.querySelector() or calls the $ function in the page, if it exists.
    • $$(): Returns an array of DOM nodes that match. This is like for document.querySelectorAll(), but returns an array instead of a NodeList.
    • $x(): Evaluates an XPath expression and returns an array of matching nodes.
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Mark
  • 418
  • 3
  • 12