Selenium C# ElementNotFound Exception Handling











up vote
0
down vote

favorite












I am running a case in Selenium where I want to verify that all elements are displayed on a web page but only want custom asserts to display at the end of the test if one or more cannot be found. Currently, using driver.FindElement, I cannot get past the ElementNotFound exceptions when assigning elements to variables if they are not there at time of identification. Is there any way around this? Here is my current code



        IWebElement userIcon = driver.FindElement(By.CssSelector("li.dropdown:nth-child(3) > a:nth-child(1)"));
userIcon.Click();


IWebElement profileIcon = driver.FindElement(By.CssSelector("li.dropdown:nth-child(3) > ul:nth-child(2) > li:nth-child(1)"));
profileIcon.Click();


IWebElement headerLogo = driver.FindElement(By.CssSelector(".company-logo"));


IWebElement headerMsgDrop = driver.FindElement(By.CssSelector(".fa-envelope-o"));
headerMsgDrop.Click();
IWebElement headerMsgDropSubGeneral = driver.FindElement(By.CssSelector("li.dropdown:nth-child(2) > ul:nth-child(2)"));


Assert.Multiple(() =>
{
Assert.IsTrue(headerLogo.Displayed, "Header Logo NOT Present");
Assert.IsTrue(headerMsgDrop.Displayed, "Header Main Message Drop NOT Present");
Assert.IsTrue(headerMsgDropSubGeneral.Displayed, "Header - Message - General Option NOT Present");
});


IWebElement headerUserDrop = driver.FindElement(By.CssSelector("li.dropdown:nth-child(3) > a:nth-child(1)"));
headerUserDrop.Click();
IWebElement headerUserDropSubProfile = driver.FindElement(By.CssSelector(".user-name"));
IWebElement headerUserDropSubCredentials = driver.FindElement(By.CssSelector("li.dropdown:nth-child(3) > ul:nth-child(2) > li:nth-child(2) > a:nth-child(1)"));
IWebElement headerUserDropSubSettings = driver.FindElement(By.CssSelector("li.dropdown:nth-child(3) > ul:nth-child(2) > li:nth-child(3) > a:nth-child(1)"));
IWebElement headerUserDropSubChgPass = driver.FindElement(By.CssSelector("li.dropdown:nth-child(3) > ul:nth-child(2) > li:nth-child(4) > a:nth-child(1)"));
IWebElement headerUserDropSubRstGrid = driver.FindElement(By.CssSelector("#clearLocalStorage"));
IWebElement headerUserDropSubLogOff = driver.FindElement(By.CssSelector("li.dropdown:nth-child(3) > ul:nth-child(2) > li:nth-child(6) > a:nth-child(1)"));


Assert.Multiple(() =>
{
Assert.IsTrue(headerUserDrop.Displayed, "Header Main User Drop NOT Present");
Assert.IsTrue(headerUserDropSubProfile.Displayed, "Header - User - Profile Option NOT Present");
Assert.IsTrue(headerUserDropSubCredentials.Displayed, "Header - User - Credentials Option NOT Present");
Assert.IsTrue(headerUserDropSubSettings.Displayed, "Header - User - Settings Option NOT Present");
Assert.IsTrue(headerUserDropSubChgPass.Displayed, "Header - User - Change Password Option NOT Present");
Assert.IsTrue(headerUserDropSubRstGrid.Displayed, "Header - User - Reset Grid Option NOT Present");
Assert.IsTrue(headerUserDropSubLogOff.Displayed, "Header - User - Log off Option NOT Present");
});


IWebElement headerSupportDrop = driver.FindElement(By.CssSelector("li.dropdown:nth-child(4) > a:nth-child(1)"));
headerSupportDrop.Click();
IWebElement headerSupportDropSubBase = driver.FindElement(By.CssSelector("li.dropdown:nth-child(4) > ul:nth-child(2) > li:nth-child(1) > a:nth-child(1)"));
IWebElement headerSupportDropSubFaq = driver.FindElement(By.CssSelector("li.dropdown:nth-child(4) > ul:nth-child(2) > li:nth-child(2) > a:nth-child(1)"));
IWebElement headerSupportDropSubTicket = driver.FindElement(By.CssSelector("li.dropdown:nth-child(4) > ul:nth-child(2) > li:nth-child(3) > a:nth-child(1)"));


Assert.Multiple(() =>
{
Assert.IsTrue(headerSupportDrop.Displayed, "Header Main Support Drop NOT Present");
Assert.IsTrue(headerSupportDropSubBase.Displayed, "Header - Support - Knowledge Base Option NOT Present");
Assert.IsTrue(headerSupportDropSubFaq.Displayed, "Header - Support - FAQ Option NOT Present");
Assert.IsTrue(headerSupportDropSubTicket.Displayed, "Header - Support - Submit Request Option NOT Present");
});


IWebElement emailTextInputField = driver.FindElement(By.CssSelector("div.col-md-10:nth-child(2)"));
IWebElement saveBut = driver.FindElement(By.CssSelector(".btn"));
IWebElement body = driver.FindElement(By.TagName("body"));


Assert.Multiple(() =>
{
Assert.IsTrue(body.Text.Contains("Profile"),"Profile Text NOT Present");
Assert.IsTrue(body.Text.Contains("Email"),"Email Text NOT Present");
Assert.IsTrue(body.Text.Contains("© 2018 - Stage Front Tickets"),"Copyright Text NOT Present");
Assert.IsTrue(emailTextInputField.Displayed, "Email Text Input Field NOT Present");
Assert.IsTrue(saveBut.Displayed, "Save Button NOT Present");
});


driver.Close();

}









share|improve this question






















  • Here are two articles on exception handling and (re)throwing I link a lot: blogs.msdn.microsoft.com/ericlippert/2008/09/10/… | codeproject.com/Articles/9538/…
    – Christopher
    Nov 20 at 21:51















up vote
0
down vote

favorite












I am running a case in Selenium where I want to verify that all elements are displayed on a web page but only want custom asserts to display at the end of the test if one or more cannot be found. Currently, using driver.FindElement, I cannot get past the ElementNotFound exceptions when assigning elements to variables if they are not there at time of identification. Is there any way around this? Here is my current code



        IWebElement userIcon = driver.FindElement(By.CssSelector("li.dropdown:nth-child(3) > a:nth-child(1)"));
userIcon.Click();


IWebElement profileIcon = driver.FindElement(By.CssSelector("li.dropdown:nth-child(3) > ul:nth-child(2) > li:nth-child(1)"));
profileIcon.Click();


IWebElement headerLogo = driver.FindElement(By.CssSelector(".company-logo"));


IWebElement headerMsgDrop = driver.FindElement(By.CssSelector(".fa-envelope-o"));
headerMsgDrop.Click();
IWebElement headerMsgDropSubGeneral = driver.FindElement(By.CssSelector("li.dropdown:nth-child(2) > ul:nth-child(2)"));


Assert.Multiple(() =>
{
Assert.IsTrue(headerLogo.Displayed, "Header Logo NOT Present");
Assert.IsTrue(headerMsgDrop.Displayed, "Header Main Message Drop NOT Present");
Assert.IsTrue(headerMsgDropSubGeneral.Displayed, "Header - Message - General Option NOT Present");
});


IWebElement headerUserDrop = driver.FindElement(By.CssSelector("li.dropdown:nth-child(3) > a:nth-child(1)"));
headerUserDrop.Click();
IWebElement headerUserDropSubProfile = driver.FindElement(By.CssSelector(".user-name"));
IWebElement headerUserDropSubCredentials = driver.FindElement(By.CssSelector("li.dropdown:nth-child(3) > ul:nth-child(2) > li:nth-child(2) > a:nth-child(1)"));
IWebElement headerUserDropSubSettings = driver.FindElement(By.CssSelector("li.dropdown:nth-child(3) > ul:nth-child(2) > li:nth-child(3) > a:nth-child(1)"));
IWebElement headerUserDropSubChgPass = driver.FindElement(By.CssSelector("li.dropdown:nth-child(3) > ul:nth-child(2) > li:nth-child(4) > a:nth-child(1)"));
IWebElement headerUserDropSubRstGrid = driver.FindElement(By.CssSelector("#clearLocalStorage"));
IWebElement headerUserDropSubLogOff = driver.FindElement(By.CssSelector("li.dropdown:nth-child(3) > ul:nth-child(2) > li:nth-child(6) > a:nth-child(1)"));


Assert.Multiple(() =>
{
Assert.IsTrue(headerUserDrop.Displayed, "Header Main User Drop NOT Present");
Assert.IsTrue(headerUserDropSubProfile.Displayed, "Header - User - Profile Option NOT Present");
Assert.IsTrue(headerUserDropSubCredentials.Displayed, "Header - User - Credentials Option NOT Present");
Assert.IsTrue(headerUserDropSubSettings.Displayed, "Header - User - Settings Option NOT Present");
Assert.IsTrue(headerUserDropSubChgPass.Displayed, "Header - User - Change Password Option NOT Present");
Assert.IsTrue(headerUserDropSubRstGrid.Displayed, "Header - User - Reset Grid Option NOT Present");
Assert.IsTrue(headerUserDropSubLogOff.Displayed, "Header - User - Log off Option NOT Present");
});


IWebElement headerSupportDrop = driver.FindElement(By.CssSelector("li.dropdown:nth-child(4) > a:nth-child(1)"));
headerSupportDrop.Click();
IWebElement headerSupportDropSubBase = driver.FindElement(By.CssSelector("li.dropdown:nth-child(4) > ul:nth-child(2) > li:nth-child(1) > a:nth-child(1)"));
IWebElement headerSupportDropSubFaq = driver.FindElement(By.CssSelector("li.dropdown:nth-child(4) > ul:nth-child(2) > li:nth-child(2) > a:nth-child(1)"));
IWebElement headerSupportDropSubTicket = driver.FindElement(By.CssSelector("li.dropdown:nth-child(4) > ul:nth-child(2) > li:nth-child(3) > a:nth-child(1)"));


Assert.Multiple(() =>
{
Assert.IsTrue(headerSupportDrop.Displayed, "Header Main Support Drop NOT Present");
Assert.IsTrue(headerSupportDropSubBase.Displayed, "Header - Support - Knowledge Base Option NOT Present");
Assert.IsTrue(headerSupportDropSubFaq.Displayed, "Header - Support - FAQ Option NOT Present");
Assert.IsTrue(headerSupportDropSubTicket.Displayed, "Header - Support - Submit Request Option NOT Present");
});


IWebElement emailTextInputField = driver.FindElement(By.CssSelector("div.col-md-10:nth-child(2)"));
IWebElement saveBut = driver.FindElement(By.CssSelector(".btn"));
IWebElement body = driver.FindElement(By.TagName("body"));


Assert.Multiple(() =>
{
Assert.IsTrue(body.Text.Contains("Profile"),"Profile Text NOT Present");
Assert.IsTrue(body.Text.Contains("Email"),"Email Text NOT Present");
Assert.IsTrue(body.Text.Contains("© 2018 - Stage Front Tickets"),"Copyright Text NOT Present");
Assert.IsTrue(emailTextInputField.Displayed, "Email Text Input Field NOT Present");
Assert.IsTrue(saveBut.Displayed, "Save Button NOT Present");
});


driver.Close();

}









share|improve this question






















  • Here are two articles on exception handling and (re)throwing I link a lot: blogs.msdn.microsoft.com/ericlippert/2008/09/10/… | codeproject.com/Articles/9538/…
    – Christopher
    Nov 20 at 21:51













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I am running a case in Selenium where I want to verify that all elements are displayed on a web page but only want custom asserts to display at the end of the test if one or more cannot be found. Currently, using driver.FindElement, I cannot get past the ElementNotFound exceptions when assigning elements to variables if they are not there at time of identification. Is there any way around this? Here is my current code



        IWebElement userIcon = driver.FindElement(By.CssSelector("li.dropdown:nth-child(3) > a:nth-child(1)"));
userIcon.Click();


IWebElement profileIcon = driver.FindElement(By.CssSelector("li.dropdown:nth-child(3) > ul:nth-child(2) > li:nth-child(1)"));
profileIcon.Click();


IWebElement headerLogo = driver.FindElement(By.CssSelector(".company-logo"));


IWebElement headerMsgDrop = driver.FindElement(By.CssSelector(".fa-envelope-o"));
headerMsgDrop.Click();
IWebElement headerMsgDropSubGeneral = driver.FindElement(By.CssSelector("li.dropdown:nth-child(2) > ul:nth-child(2)"));


Assert.Multiple(() =>
{
Assert.IsTrue(headerLogo.Displayed, "Header Logo NOT Present");
Assert.IsTrue(headerMsgDrop.Displayed, "Header Main Message Drop NOT Present");
Assert.IsTrue(headerMsgDropSubGeneral.Displayed, "Header - Message - General Option NOT Present");
});


IWebElement headerUserDrop = driver.FindElement(By.CssSelector("li.dropdown:nth-child(3) > a:nth-child(1)"));
headerUserDrop.Click();
IWebElement headerUserDropSubProfile = driver.FindElement(By.CssSelector(".user-name"));
IWebElement headerUserDropSubCredentials = driver.FindElement(By.CssSelector("li.dropdown:nth-child(3) > ul:nth-child(2) > li:nth-child(2) > a:nth-child(1)"));
IWebElement headerUserDropSubSettings = driver.FindElement(By.CssSelector("li.dropdown:nth-child(3) > ul:nth-child(2) > li:nth-child(3) > a:nth-child(1)"));
IWebElement headerUserDropSubChgPass = driver.FindElement(By.CssSelector("li.dropdown:nth-child(3) > ul:nth-child(2) > li:nth-child(4) > a:nth-child(1)"));
IWebElement headerUserDropSubRstGrid = driver.FindElement(By.CssSelector("#clearLocalStorage"));
IWebElement headerUserDropSubLogOff = driver.FindElement(By.CssSelector("li.dropdown:nth-child(3) > ul:nth-child(2) > li:nth-child(6) > a:nth-child(1)"));


Assert.Multiple(() =>
{
Assert.IsTrue(headerUserDrop.Displayed, "Header Main User Drop NOT Present");
Assert.IsTrue(headerUserDropSubProfile.Displayed, "Header - User - Profile Option NOT Present");
Assert.IsTrue(headerUserDropSubCredentials.Displayed, "Header - User - Credentials Option NOT Present");
Assert.IsTrue(headerUserDropSubSettings.Displayed, "Header - User - Settings Option NOT Present");
Assert.IsTrue(headerUserDropSubChgPass.Displayed, "Header - User - Change Password Option NOT Present");
Assert.IsTrue(headerUserDropSubRstGrid.Displayed, "Header - User - Reset Grid Option NOT Present");
Assert.IsTrue(headerUserDropSubLogOff.Displayed, "Header - User - Log off Option NOT Present");
});


IWebElement headerSupportDrop = driver.FindElement(By.CssSelector("li.dropdown:nth-child(4) > a:nth-child(1)"));
headerSupportDrop.Click();
IWebElement headerSupportDropSubBase = driver.FindElement(By.CssSelector("li.dropdown:nth-child(4) > ul:nth-child(2) > li:nth-child(1) > a:nth-child(1)"));
IWebElement headerSupportDropSubFaq = driver.FindElement(By.CssSelector("li.dropdown:nth-child(4) > ul:nth-child(2) > li:nth-child(2) > a:nth-child(1)"));
IWebElement headerSupportDropSubTicket = driver.FindElement(By.CssSelector("li.dropdown:nth-child(4) > ul:nth-child(2) > li:nth-child(3) > a:nth-child(1)"));


Assert.Multiple(() =>
{
Assert.IsTrue(headerSupportDrop.Displayed, "Header Main Support Drop NOT Present");
Assert.IsTrue(headerSupportDropSubBase.Displayed, "Header - Support - Knowledge Base Option NOT Present");
Assert.IsTrue(headerSupportDropSubFaq.Displayed, "Header - Support - FAQ Option NOT Present");
Assert.IsTrue(headerSupportDropSubTicket.Displayed, "Header - Support - Submit Request Option NOT Present");
});


IWebElement emailTextInputField = driver.FindElement(By.CssSelector("div.col-md-10:nth-child(2)"));
IWebElement saveBut = driver.FindElement(By.CssSelector(".btn"));
IWebElement body = driver.FindElement(By.TagName("body"));


Assert.Multiple(() =>
{
Assert.IsTrue(body.Text.Contains("Profile"),"Profile Text NOT Present");
Assert.IsTrue(body.Text.Contains("Email"),"Email Text NOT Present");
Assert.IsTrue(body.Text.Contains("© 2018 - Stage Front Tickets"),"Copyright Text NOT Present");
Assert.IsTrue(emailTextInputField.Displayed, "Email Text Input Field NOT Present");
Assert.IsTrue(saveBut.Displayed, "Save Button NOT Present");
});


driver.Close();

}









share|improve this question













I am running a case in Selenium where I want to verify that all elements are displayed on a web page but only want custom asserts to display at the end of the test if one or more cannot be found. Currently, using driver.FindElement, I cannot get past the ElementNotFound exceptions when assigning elements to variables if they are not there at time of identification. Is there any way around this? Here is my current code



        IWebElement userIcon = driver.FindElement(By.CssSelector("li.dropdown:nth-child(3) > a:nth-child(1)"));
userIcon.Click();


IWebElement profileIcon = driver.FindElement(By.CssSelector("li.dropdown:nth-child(3) > ul:nth-child(2) > li:nth-child(1)"));
profileIcon.Click();


IWebElement headerLogo = driver.FindElement(By.CssSelector(".company-logo"));


IWebElement headerMsgDrop = driver.FindElement(By.CssSelector(".fa-envelope-o"));
headerMsgDrop.Click();
IWebElement headerMsgDropSubGeneral = driver.FindElement(By.CssSelector("li.dropdown:nth-child(2) > ul:nth-child(2)"));


Assert.Multiple(() =>
{
Assert.IsTrue(headerLogo.Displayed, "Header Logo NOT Present");
Assert.IsTrue(headerMsgDrop.Displayed, "Header Main Message Drop NOT Present");
Assert.IsTrue(headerMsgDropSubGeneral.Displayed, "Header - Message - General Option NOT Present");
});


IWebElement headerUserDrop = driver.FindElement(By.CssSelector("li.dropdown:nth-child(3) > a:nth-child(1)"));
headerUserDrop.Click();
IWebElement headerUserDropSubProfile = driver.FindElement(By.CssSelector(".user-name"));
IWebElement headerUserDropSubCredentials = driver.FindElement(By.CssSelector("li.dropdown:nth-child(3) > ul:nth-child(2) > li:nth-child(2) > a:nth-child(1)"));
IWebElement headerUserDropSubSettings = driver.FindElement(By.CssSelector("li.dropdown:nth-child(3) > ul:nth-child(2) > li:nth-child(3) > a:nth-child(1)"));
IWebElement headerUserDropSubChgPass = driver.FindElement(By.CssSelector("li.dropdown:nth-child(3) > ul:nth-child(2) > li:nth-child(4) > a:nth-child(1)"));
IWebElement headerUserDropSubRstGrid = driver.FindElement(By.CssSelector("#clearLocalStorage"));
IWebElement headerUserDropSubLogOff = driver.FindElement(By.CssSelector("li.dropdown:nth-child(3) > ul:nth-child(2) > li:nth-child(6) > a:nth-child(1)"));


Assert.Multiple(() =>
{
Assert.IsTrue(headerUserDrop.Displayed, "Header Main User Drop NOT Present");
Assert.IsTrue(headerUserDropSubProfile.Displayed, "Header - User - Profile Option NOT Present");
Assert.IsTrue(headerUserDropSubCredentials.Displayed, "Header - User - Credentials Option NOT Present");
Assert.IsTrue(headerUserDropSubSettings.Displayed, "Header - User - Settings Option NOT Present");
Assert.IsTrue(headerUserDropSubChgPass.Displayed, "Header - User - Change Password Option NOT Present");
Assert.IsTrue(headerUserDropSubRstGrid.Displayed, "Header - User - Reset Grid Option NOT Present");
Assert.IsTrue(headerUserDropSubLogOff.Displayed, "Header - User - Log off Option NOT Present");
});


IWebElement headerSupportDrop = driver.FindElement(By.CssSelector("li.dropdown:nth-child(4) > a:nth-child(1)"));
headerSupportDrop.Click();
IWebElement headerSupportDropSubBase = driver.FindElement(By.CssSelector("li.dropdown:nth-child(4) > ul:nth-child(2) > li:nth-child(1) > a:nth-child(1)"));
IWebElement headerSupportDropSubFaq = driver.FindElement(By.CssSelector("li.dropdown:nth-child(4) > ul:nth-child(2) > li:nth-child(2) > a:nth-child(1)"));
IWebElement headerSupportDropSubTicket = driver.FindElement(By.CssSelector("li.dropdown:nth-child(4) > ul:nth-child(2) > li:nth-child(3) > a:nth-child(1)"));


Assert.Multiple(() =>
{
Assert.IsTrue(headerSupportDrop.Displayed, "Header Main Support Drop NOT Present");
Assert.IsTrue(headerSupportDropSubBase.Displayed, "Header - Support - Knowledge Base Option NOT Present");
Assert.IsTrue(headerSupportDropSubFaq.Displayed, "Header - Support - FAQ Option NOT Present");
Assert.IsTrue(headerSupportDropSubTicket.Displayed, "Header - Support - Submit Request Option NOT Present");
});


IWebElement emailTextInputField = driver.FindElement(By.CssSelector("div.col-md-10:nth-child(2)"));
IWebElement saveBut = driver.FindElement(By.CssSelector(".btn"));
IWebElement body = driver.FindElement(By.TagName("body"));


Assert.Multiple(() =>
{
Assert.IsTrue(body.Text.Contains("Profile"),"Profile Text NOT Present");
Assert.IsTrue(body.Text.Contains("Email"),"Email Text NOT Present");
Assert.IsTrue(body.Text.Contains("© 2018 - Stage Front Tickets"),"Copyright Text NOT Present");
Assert.IsTrue(emailTextInputField.Displayed, "Email Text Input Field NOT Present");
Assert.IsTrue(saveBut.Displayed, "Save Button NOT Present");
});


driver.Close();

}






c# selenium exception-handling






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 20 at 21:49









abuscaglio

111




111












  • Here are two articles on exception handling and (re)throwing I link a lot: blogs.msdn.microsoft.com/ericlippert/2008/09/10/… | codeproject.com/Articles/9538/…
    – Christopher
    Nov 20 at 21:51


















  • Here are two articles on exception handling and (re)throwing I link a lot: blogs.msdn.microsoft.com/ericlippert/2008/09/10/… | codeproject.com/Articles/9538/…
    – Christopher
    Nov 20 at 21:51
















Here are two articles on exception handling and (re)throwing I link a lot: blogs.msdn.microsoft.com/ericlippert/2008/09/10/… | codeproject.com/Articles/9538/…
– Christopher
Nov 20 at 21:51




Here are two articles on exception handling and (re)throwing I link a lot: blogs.msdn.microsoft.com/ericlippert/2008/09/10/… | codeproject.com/Articles/9538/…
– Christopher
Nov 20 at 21:51












2 Answers
2






active

oldest

votes

















up vote
0
down vote













You may be looking for this kind of solution:



    public IWebElement Get(By byLocator, double seconds = 10)
{
IWebElement element = null;

WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(seconds));
try
{
element = wait.Until(ExpectedConditions.ElementExists(byLocator));
}
catch (Exception) { }

return element;
}


Or you can wait for it to be clickable also.



ExpectedConditions.ElementToBeClickable(byLocator)





share|improve this answer




























    up vote
    0
    down vote













    It looks like you are not waiting or you are not giving UI to load properly before capturing the next element and perform certain action on it.



    You need to add some wait time before performing next step.
    Eg. waitTillElementPresent






    share|improve this answer





















      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53402101%2fselenium-c-sharp-elementnotfound-exception-handling%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      0
      down vote













      You may be looking for this kind of solution:



          public IWebElement Get(By byLocator, double seconds = 10)
      {
      IWebElement element = null;

      WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(seconds));
      try
      {
      element = wait.Until(ExpectedConditions.ElementExists(byLocator));
      }
      catch (Exception) { }

      return element;
      }


      Or you can wait for it to be clickable also.



      ExpectedConditions.ElementToBeClickable(byLocator)





      share|improve this answer

























        up vote
        0
        down vote













        You may be looking for this kind of solution:



            public IWebElement Get(By byLocator, double seconds = 10)
        {
        IWebElement element = null;

        WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(seconds));
        try
        {
        element = wait.Until(ExpectedConditions.ElementExists(byLocator));
        }
        catch (Exception) { }

        return element;
        }


        Or you can wait for it to be clickable also.



        ExpectedConditions.ElementToBeClickable(byLocator)





        share|improve this answer























          up vote
          0
          down vote










          up vote
          0
          down vote









          You may be looking for this kind of solution:



              public IWebElement Get(By byLocator, double seconds = 10)
          {
          IWebElement element = null;

          WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(seconds));
          try
          {
          element = wait.Until(ExpectedConditions.ElementExists(byLocator));
          }
          catch (Exception) { }

          return element;
          }


          Or you can wait for it to be clickable also.



          ExpectedConditions.ElementToBeClickable(byLocator)





          share|improve this answer












          You may be looking for this kind of solution:



              public IWebElement Get(By byLocator, double seconds = 10)
          {
          IWebElement element = null;

          WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(seconds));
          try
          {
          element = wait.Until(ExpectedConditions.ElementExists(byLocator));
          }
          catch (Exception) { }

          return element;
          }


          Or you can wait for it to be clickable also.



          ExpectedConditions.ElementToBeClickable(byLocator)






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 21 at 10:27









          MicMan

          114




          114
























              up vote
              0
              down vote













              It looks like you are not waiting or you are not giving UI to load properly before capturing the next element and perform certain action on it.



              You need to add some wait time before performing next step.
              Eg. waitTillElementPresent






              share|improve this answer

























                up vote
                0
                down vote













                It looks like you are not waiting or you are not giving UI to load properly before capturing the next element and perform certain action on it.



                You need to add some wait time before performing next step.
                Eg. waitTillElementPresent






                share|improve this answer























                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  It looks like you are not waiting or you are not giving UI to load properly before capturing the next element and perform certain action on it.



                  You need to add some wait time before performing next step.
                  Eg. waitTillElementPresent






                  share|improve this answer












                  It looks like you are not waiting or you are not giving UI to load properly before capturing the next element and perform certain action on it.



                  You need to add some wait time before performing next step.
                  Eg. waitTillElementPresent







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 22 at 17:27









                  Akash Jha

                  392




                  392






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.





                      Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                      Please pay close attention to the following guidance:


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53402101%2fselenium-c-sharp-elementnotfound-exception-handling%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      What visual should I use to simply compare current year value vs last year in Power BI desktop

                      Alexandru Averescu

                      Trompette piccolo