0
votes

I'm launching a selenium test. For some user you don't have the same number of page. That's why I need to implement a try catch in my code but sadly every time he try and the element is not there he stop the program. Giving NoSuchElementException while I put this try catch. I try to put before each click if the element displayd before but the same error show up.

If someone can help me that's will be really nice!

try {
        driver.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS);
        driver.findElement(By.xpath("//input[@type='radio' and @value='true']")).click();
        sleep();

        driver.findElement(By.xpath("//input[@type='submit' and @value='>> valider <<']")).click();
        sleep();
 } catch (org.openqa.selenium.NoSuchElementException e) {
            e.printStackTrace();
 }


Log error :

    Starting ChromeDriver 2.31.488763 (092de99f48a300323ecf8c2a4e2e7cab51de5ba8) on port 6496
Only local connections are allowed.
sept. 20, 2017 11:30:38 AM org.openqa.selenium.remote.ProtocolHandshake createSession
INFOS: Detected dialect: OSS
org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//input[@type='radio' and @value='true']"}
  (Session info: chrome=60.0.3112.113)
  (Driver info: chromedriver=2.31.488763 (092de99f48a300323ecf8c2a4e2e7cab51de5ba8),platform=Windows NT 6.1.7601 SP1 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 22 milliseconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '3.4.0', revision: 'unknown', time: 'unknown'

Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities [{applicationCacheEnabled=false, rotatable=false, mobileEmulationEnabled=false, networkConnectionEnabled=false, chrome={chromedriverVersion=2.31.488763 (092de99f48a300323ecf8c2a4e2e7cab51de5ba8), userDataDir=C:\Temp\scoped_dir9468_31433}, takesHeapSnapshot=true, pageLoadStrategy=normal, databaseEnabled=false, handlesAlerts=true, hasTouchScreen=false, version=60.0.3112.113, platform=XP, browserConnectionEnabled=false, nativeEvents=true, acceptSslCerts=true, locationContextEnabled=true, webStorageEnabled=true, browserName=chrome, takesScreenshot=true, javascriptEnabled=true, cssSelectorsEnabled=true, setWindowRect=true, unexpectedAlertBehaviour=}]
Session ID: 3592b946923cfbb74e8e512fb3a212ef
*** Element info: {Using=xpath, value=//input[@type='radio' and @value='true']}
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:215)
    at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:167)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:671)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:410)
    at org.openqa.selenium.remote.RemoteWebDriver.findElementByXPath(RemoteWebDriver.java:509)
    at org.openqa.selenium.By$ByXPath.findElement(By.java:361)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:402)
    at serialys.Test.test(Test.java:83)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
2
Please share your log and error details - iamsankalp89
You are printing e.printStackTrace(); in catch block which print the Stacktracerepalce this with your code like System.out.println("Element is not found"); or some code - iamsankalp89

2 Answers

0
votes

you need to first check if the element is there or not and then click that element, You can create following method :

public boolean isElementPresent(WebDriver driver, By elem) {
        try {
            driver.findElement(elem);
            return true;
        }catch(NoSuchElementException e) {
            return false;
        }
    }

This method returns 'true' if element is present.

Then you can call this method to check if element is present before clicking the element as following:

try {
        driver.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS);
        if(isElementPresent(driver, By.xpath("//input[@type='radio' and @value='true']")){
            driver.findElement(By.xpath("//input[@type='radio' and @value='true']")).click();
        }
        sleep();

        driver.findElement(By.xpath("//input[@type='submit' and @value='>> valider <<']")).click();
        sleep();
 } catch (org.openqa.selenium.NoSuchElementException e) {
            e.printStackTrace();
 }
0
votes

You are printing e.printStackTrace(); in catch block which print the Stacktracerepalce this with your code like System.out.println("Element is not found"); or some code

try {
        driver.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS);
        driver.findElement(By.xpath("//input[@type='radio' and @value='true']")).click();
        sleep();

        driver.findElement(By.xpath("//input[@type='submit' and @value='>> valider <<']")).click();
        sleep();
 } catch (org.openqa.selenium.NoSuchElementException e) {
           System.out.println("Element is not found");
 }