0

We are using Selenium with C#. We have a driver script where we instantiate the driver and then we call a testscript (automated functional testcase flow). Everything works well when the object is present on the page. We are facing issue of driver getting killed when we try and verify that certain object is not present, below is the code

Driver Script Code

//driver = new RemoteWebDriver(new Uri(uri), capabilites,TimeSpan.FromSeconds(120));

Test Script Code

public class GraphNew
{
    public Boolean testGraphNew(IWebDriver driver, Logger OneLogger)
    {
        try
        { //Navigate to a page
            try
            { 
                driver.FindElement(By.XPath("//a[contains(text(),'Add New Claim')]")).Click();
            }
            catch
            {
                OneLogger.Log("Element is not prsent")
            }
        }
    }
    catch(Exception e)
    {
        OneLogger.LogException(e);
        return false;
    }
}

The problem is when the object is not identified (as it is not present) in the inner try, rather than going to the inner catch block, the execution proceeds to outer catch and shows exception as -

{"The HTTP request to the remote WebDriver server for URL http://localhost:4449/wd/hub/session/35c483a6-6871-425a-a936-aeebb0742fd2/element timed out after 120 seconds."}

and driver gets killed.

Can anyone please suggest, if we are missing something or what should be the idle way to code so that once the object is not identified, driver does not get killed and execution continues for the remaining code.

M. Schena
  • 2,039
  • 1
  • 21
  • 29
  • While formatting your code, i saw that from your first `try`, the `catch` is outside of the method! – M. Schena Feb 02 '16 at 14:39
  • Is that all the code? You're not explicitly quitting the driver anywhere? – Andrew Regan Feb 02 '16 at 14:54
  • I have put only snippet of the big code. We do quit the driver in the main driver script post the execution of TestScript (in the above example testGraphNew), it's just that I have not mentioned it here. – user5872693 Feb 02 '16 at 15:11
  • Aha - I assume you're failing to catch the 'not present' error and simply falling through to the outermost exception handler, which is what's killing your driver. – Andrew Regan Feb 02 '16 at 15:17
  • See also: http://stackoverflow.com/questions/10899360/webdriver-and-c-sharp-nosuchelement-exception – Andrew Regan Feb 02 '16 at 15:30

1 Answers1

0

You can do something like below ...

The code is in java but it is very similar/near to C#

You can check first whether your element is present or not in your HTML DOM to prevent from error/failer of script. like below:- (I am replacing size to lenght in below code as lenght is using in C# to determine the size of array)

  if (driver.findElements("YOUR LOCATOR").Length() != 0) {  
            driver.findElement(YOUR LOCATOR).click();  
            System.out.println("element exists");     
        }  
  else{
           System.out.println("element is not exists");     
      }

You can also use isDisplayed() method of selenium, it will return boolean value. true if element exist on DOM.

Boolean result =  driver.findElement(By.xpath("//a[contains(text(),'Add New Claim')]")).isDisplayed();
if(result==true)
{
    System.out.println("element exists"); 
}else{
    System.out.println("element is not exists");
}

Refer below link if you want to know more about isDisplayed() method

http://www.softwaretestinghelp.com/webdriver-commands-selenium-tutorial-14/

Hope it will help you :)

Shubham Jain
  • 16,610
  • 15
  • 78
  • 125
  • Thanks Shubham for your reply. I tried using following code, however it still goes to the outer catch with the same error as i observed earlier, ` if (driver.FindElements(By.XPath("//a[contains(text(),'Add New Claim')]")).Count != 0) OneLogger.Log(OneLogger.PASS, "element exists"); else OneLogger.Log(OneLogger.Fail, "element does not exist");` – user5872693 Feb 03 '16 at 09:41
  • oh! .. I have added one more way in my answer by using isDisplayed method .. that should work .. please get back to me if still facing any issue – Shubham Jain Feb 03 '16 at 10:01