FindsBy vs Lambda expression [Initiating Element]C#

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view

FindsBy vs Lambda expression [Initiating Element]C#

Phaneendra Bheesetti


As per the recent upgrade, I've been trying to upgrade my project with the latest changes of selenium 3.11.

I'm facing issue with Lambda expressions for validating false conditions.


While running/debugging the below test using Lambda expressions

The system is not throwing an error for the true conditions with the lambda expressions.

for the false conditions, System is directly throwing no such element exception. it is not even going into the 

IsElementDisplayed function to check the element is present or not.

If I use Old way of Initialising web elements like FindsBy then the system is going into isElementDisplayed function and returning false.

Can I get some resolution for this issue

Whenever I got a new build, I need to check the false conditions as there will be application errors on the page.

Test case:

Whenever I'm loading a web page I'll check whether all the fields are present(displayed) and no errors showed up as well.

Sample test Code:


// OLD Way

[FindsBy(How = How.XPath, Using = "//*[@id='header']/h1[contains(text(),'Server Error')]")]
IWebElement error_Site_AppServerError _OLD { get; set; }

// New Way

IWebElement error_Site_AppServerError_New => DriverInstance.Driver.FindElement(By.XPath("//*[@id='header']/h1[contains(text(),'Server Error')]"));

IWebElement IMG_Logo_Tenant => DriverInstance.Driver.FindElement(By.XPath("//*[@id='tenant-logo']/img"));

        public void test1()




          1 -  Assert.That(IMG_Logo_Tenant.Displayed, Is.True);
          2 - Assert.IsTrue(ElementHelper.IsElementDisplayed(IMG_Logo_Tenant));
          3 - Assert.IsFalse(ElementHelper.IsElementDisplayed(error_Site_AppServerError _OLD)); [Returned False and execution proceeded to line 4]
          4 - Assert.IsFalse(ElementHelper.IsElementDisplayed(error_Site_AppServerError_New)); [Directly throwed no such element without going into the iselementdisplayed function]


          catch(Exception e)





public static bool IsElementDisplayed(IWebElement element)

                bool ele = element.Displayed;

                return true;
            catch (Exception)
                return false;
                throw new Exception(string.Format("Element Not Displayed or Enabled Exception"));

Exception Info:

Result Message:

OpenQA.Selenium.NoSuchElementException : no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id='header']/h1[contains(text(),'Server Error')]"}

  (Session info: chrome=67.0.3396.99)

  (Driver info: chromedriver=2.40.565498 (ea082db3280dd6843ebfb08a625e3eb905c4f5ab),platform=Windows NT 10.0.17134 x86_64)

please help in finding the resolution of above issue.

Thank you,


You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit
For more options, visit