Best Ocaramba code snippet using Ocaramba.Extensions.SearchContextExtensions.GetElement
SearchContextExtensions.cs
Source:SearchContextExtensions.cs  
...46        /// <returns>47        /// Found element.48        /// </returns>49        /// <example>How to use it: <code>50        /// this.Driver.GetElement(this.loginButton);51        /// </code></example>52        public static IWebElement GetElement(this ISearchContext element, ElementLocator locator, [Optional] string customMessage)53        {54            return element.GetElement(locator, BaseConfiguration.LongTimeout, e => e.Displayed && e.Enabled, customMessage);55        }56        /// <summary>57        /// Finds and waits for an element that is visible and displayed at specified time.58        /// </summary>59        /// <param name="element">The element.</param>60        /// <param name="locator">The locator.</param>61        /// <param name="timeout">Specified time to wait.</param>62        /// <param name="customMessage">Custom message to be displayed when there is no possible to get element.</param>63        /// <returns>64        /// Found element.65        /// </returns>66        /// <example>How to use it: <code>67        /// this.Driver.GetElement(this.loginButton, timeout);68        /// </code></example>69        public static IWebElement GetElement(this ISearchContext element, ElementLocator locator, double timeout, string customMessage)70        {71            return element.GetElement(locator, timeout, e => e.Displayed && e.Enabled, customMessage);72        }73        /// <summary>74        /// Finds and waits for an element that is visible and displayed at specified time.75        /// </summary>76        /// <param name="element">The element.</param>77        /// <param name="locator">The locator.</param>78        /// <param name="timeout">Specified time to wait.</param>79        /// <returns>80        /// Found element.81        /// </returns>82        /// <example>How to use it: <code>83        /// this.Driver.GetElement(this.loginButton, timeout);84        /// </code></example>85        public static IWebElement GetElement(this ISearchContext element, ElementLocator locator, double timeout)86        {87            return element.GetElement(locator, timeout, e => e.Displayed && e.Enabled);88        }89        /// <summary>90        /// Finds and waits for an element that meets specified conditions for long timeout.91        /// </summary>92        /// <param name="element">The element.</param>93        /// <param name="locator">The locator.</param>94        /// <param name="condition">Wait until condition met.</param>95        /// <param name="customMessage">Custom message to be displayed when there is no possible to get element.</param>96        /// <returns>97        /// Found element.98        /// </returns>99        /// <example>How to use it: <code>100        /// this.Driver.GetElement(this.loginButton, e => e.Displayed);101        /// </code></example>102        public static IWebElement GetElement(this ISearchContext element, ElementLocator locator, Func<IWebElement, bool> condition, [Optional] string customMessage)103        {104            return element.GetElement(locator, BaseConfiguration.LongTimeout, condition, customMessage);105        }106        /// <summary>107        /// Finds and waits for an element that meets specified conditions at specified time.108        /// </summary>109        /// <param name="element">The element.</param>110        /// <param name="locator">The locator.</param>111        /// <param name="timeout">The timeout.</param>112        /// <param name="condition">The condition to be met.</param>113        /// <param name="customMessage">Custom message to be displayed when there is no possible to get element.</param>114        /// <returns>115        /// Return found element.116        /// </returns>117        /// <example>How to use it: <code>118        /// this.Driver.GetElement(this.loginButton, timeout, e => e.Displayed);119        /// </code></example>120        public static IWebElement GetElement(this ISearchContext element, ElementLocator locator, double timeout, Func<IWebElement, bool> condition, [Optional] string customMessage)121        {122            var driver = element.ToDriver();123            if (DriversCustomSettings.IsDriverSynchronizationWithAngular(driver))124            {125                driver.WaitForAngular();126            }127            var by = locator.ToBy();128            var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(timeout)) { Message = customMessage };129            wait.IgnoreExceptionTypes(typeof(StaleElementReferenceException));130            wait.Until(131                    drv =>132                    {133                        var ele = element.FindElement(@by);134                        return condition(ele);135                    });136            return element.FindElement(@by);137        }138        /// <summary>139        /// Finds and waits for an element that meets specified conditions at specified time, recheck condition at specific time interval.140        /// </summary>141        /// <param name="element">The element.</param>142        /// <param name="locator">The locator.</param>143        /// <param name="timeout">The timeout.</param>144        /// <param name="timeInterval">The value indicating how often to check for the condition to be true..</param>145        /// <param name="condition">The condition to be met.</param>146        /// <param name="customMessage">Custom message to be displayed when there is no possible to get element.</param>147        /// <returns>148        /// Return found element.149        /// </returns>150        /// <example>How to use it: <code>151        /// this.Driver.GetElement(this.loginButton, timeout, timeInterval, e => e.Displayed & e.Enabled);152        /// </code></example>153        public static IWebElement GetElement(this ISearchContext element, ElementLocator locator, double timeout, double timeInterval, Func<IWebElement, bool> condition, [Optional] string customMessage)154        {155            var by = locator.ToBy();156            var wait = new WebDriverWait(new SystemClock(), element.ToDriver(), TimeSpan.FromSeconds(timeout), TimeSpan.FromSeconds(timeInterval)) { Message = customMessage };157            wait.IgnoreExceptionTypes(typeof(StaleElementReferenceException));158            wait.Until(159                    drv =>160                    {161                        var ele = element.FindElement(@by);162                        return condition(ele);163                    });164            return element.FindElement(@by);165        }166        /// <summary>167        /// Finds and waits for an element that is visible and displayed for long timeout.168        /// </summary>169        /// <typeparam name="T">IWebComponent like ICheckbox, ISelect, etc.</typeparam>170        /// <param name="searchContext">The search context.</param>171        /// <param name="locator">The locator.</param>172        /// <param name="customMessage">Custom message to be displayed when there is no possible to get element.</param>173        /// <returns>174        /// Located and displayed element.175        /// </returns>176        /// <example>How to specify element type to get additional actions for it: <code>177        /// var checkbox = this.Driver.GetElement<Checkbox>(this.stackOverFlowCheckbox);178        /// checkbox.TickCheckbox();179        /// </code></example>180        public static T GetElement<T>(this ISearchContext searchContext, ElementLocator locator, [Optional] string customMessage)181            where T : class, IWebElement182        {183            IWebElement webElemement = searchContext.GetElement(locator, customMessage);184            return webElemement.As<T>();185        }186        /// <summary>187        /// Finds and waits for an element that is visible and displayed at specified time.188        /// </summary>189        /// <typeparam name="T">IWebComponent like ICheckbox, ISelect, etc.</typeparam>190        /// <param name="searchContext">The search context.</param>191        /// <param name="locator">The locator.</param>192        /// <param name="timeout">Specified time to wait.</param>193        /// <returns>194        /// Located and displayed element.195        /// </returns>196        /// <example>How to specify element type to get additional actions for it: <code>197        /// var checkbox = this.Driver.GetElement<Checkbox>(this.stackOverFlowCheckbox, timeout);198        /// checkbox.TickCheckbox();199        /// </code></example>200        public static T GetElement<T>(this ISearchContext searchContext, ElementLocator locator, double timeout)201            where T : class, IWebElement202        {203            IWebElement webElemement = searchContext.GetElement(locator, timeout);204            return webElemement.As<T>();205        }206        /// <summary>207        /// Finds and waits for an element that meets specified conditions for long timeout.208        /// </summary>209        /// <typeparam name="T">IWebComponent like ICheckbox, ISelect, etc.</typeparam>210        /// <param name="searchContext">The search context.</param>211        /// <param name="locator">The locator.</param>212        /// <param name="condition">The condition to be met.</param>213        /// <param name="customMessage">Custom message to be displayed when there is no possible to get element.</param>214        /// <returns>215        /// Located and displayed element.216        /// </returns>217        /// <example>How to find hidden element, specify element type to get additional actions for it and specify condition : <code>218        /// var checkbox = this.Driver.GetElement<Checkbox>(this.stackOverFlowCheckbox, e => e.Displayed == false);219        /// checkbox.TickCheckbox();220        /// </code></example>221        public static T GetElement<T>(this ISearchContext searchContext, ElementLocator locator, Func<IWebElement, bool> condition, [Optional] string customMessage)222            where T : class, IWebElement223        {224            IWebElement webElemement = searchContext.GetElement(locator, condition, customMessage);225            return webElemement.As<T>();226        }227        /// <summary>228        /// Finds and waits for an element that meets specified conditions at specified time.229        /// </summary>230        /// <typeparam name="T">IWebComponent like ICheckbox, ISelect, etc.</typeparam>231        /// <param name="searchContext">The search context.</param>232        /// <param name="locator">The locator.</param>233        /// <param name="timeout">Specified time to wait.</param>234        /// <param name="condition">The condition to be met.</param>235        /// <param name="customMessage">Custom message to be displayed when there is no possible to get element.</param>236        /// <returns>237        /// Located and displayed element.238        /// </returns>239        /// <example>How to specify element type to get additional actions for it and specify time and condition to find this element: <code>240        /// var checkbox = this.Driver.GetElement<Checkbox>(this.stackOverFlowCheckbox, timeout, e => e.Displayed);241        /// checkbox.TickCheckbox();242        /// </code></example>243        public static T GetElement<T>(this ISearchContext searchContext, ElementLocator locator, double timeout, Func<IWebElement, bool> condition, [Optional] string customMessage)244            where T : class, IWebElement245        {246            IWebElement webElemement = searchContext.GetElement(locator, timeout, condition, customMessage);247            return webElemement.As<T>();248        }249        /// <summary>250        /// Finds elements that are enabled and displayed.251        /// </summary>252        /// <param name="element">The element.</param>253        /// <param name="locator">The locator.</param>254        /// <returns>255        /// Return all found and displayed and enabled elements..256        /// </returns>257        /// <example>How to find elements : <code>258        /// var checkboxes = this.Driver.GetElements(this.stackOverFlowCheckbox);259        /// </code></example>260        public static IList<IWebElement> GetElements(this ISearchContext element, ElementLocator locator)261        {262            return element.GetElements(locator, e => e.Displayed && e.Enabled).ToList();263        }264        /// <summary>265        /// Finds and waits for given timeout for at least minimum number of elements that meet specified conditions.266        /// </summary>267        /// <param name="element">The element.</param>268        /// <param name="locator">The locator.</param>269        /// <param name="timeout">Specified time to wait.</param>270        /// <param name="condition">Condition to be fulfilled by elements.</param>271        /// <param name="minNumberOfElements">The minimum number of elements to get.</param>272        /// <returns>273        /// Return all found and displayed and enabled elements.274        /// </returns>275        /// <example>How to find elements : <code>276        /// var checkboxes = this.Driver.GetElements(this.stackOverFlowCheckbox, timeout, e => e.Displayed && e.Enabled, 1);277        /// </code></example>278        public static IList<IWebElement> GetElements(this ISearchContext element, ElementLocator locator, double timeout, Func<IWebElement, bool> condition, int minNumberOfElements)279        {280            IList<IWebElement> elements = null;281            WaitHelper.Wait(282                () => (elements = GetElements(element, locator, condition).ToList()).Count >= minNumberOfElements,283                TimeSpan.FromSeconds(timeout),284                "Timeout while getting elements");285            return elements;286        }287        /// <summary>288        /// Finds and waits for LongTimeout timeout for at least minimum number of elements that are enabled and displayed.289        /// </summary>290        /// <param name="element">The element.</param>291        /// <param name="locator">The locator.</param>292        /// <param name="minNumberOfElements">The minimum number of elements to get.</param>293        /// <returns>294        /// Return all found and displayed and enabled elements.295        /// </returns>296        /// <example>How to find elements : <code>297        /// var checkboxes = this.Driver.GetElements(this.stackOverFlowCheckbox, 1);298        /// </code></example>299        public static IList<IWebElement> GetElements(this ISearchContext element, ElementLocator locator, int minNumberOfElements)300        {301            IList<IWebElement> elements = null;302            WaitHelper.Wait(303                () => (elements = GetElements(element, locator, e => e.Displayed && e.Enabled).ToList()).Count >= minNumberOfElements,304                TimeSpan.FromSeconds(BaseConfiguration.LongTimeout),305                "Timeout while getting elements");306            return elements;307        }308        /// <summary>309        /// Finds elements that meet specified conditions.310        /// </summary>311        /// <param name="element">The element.</param>312        /// <param name="locator">The locator.</param>313        /// <param name="condition">Condition to be fulfilled by elements.</param>314        /// <returns>315        /// Return all found elements for specified conditions.316        /// </returns>317        /// <example>How to find disabled elements : <code>318        /// var checkboxes = this.Driver.GetElements(this.stackOverFlowCheckbox, e => e.Enabled == false);319        /// </code></example>320        public static IList<IWebElement> GetElements(this ISearchContext element, ElementLocator locator, Func<IWebElement, bool> condition)321        {322            return element.FindElements(locator.ToBy()).Where(condition).ToList();323        }324        /// <summary>325        /// Finds elements that are visible and displayed.326        /// </summary>327        /// <typeparam name="T">IWebComponent like ICheckbox, ISelect, etc.</typeparam>328        /// <param name="searchContext">The search context.</param>329        /// <param name="locator">The locator.</param>330        /// <returns>331        /// Located elements.332        /// </returns>333        /// <example>How to find elements and specify element type to get additional actions for them : <code>334        /// var checkboxes = this.Driver.GetElements<Checkbox>(this.stackOverFlowCheckbox);335        /// checkboxes[0].TickCheckbox();336        /// </code></example>337        public static IList<T> GetElements<T>(this ISearchContext searchContext, ElementLocator locator)338            where T : class, IWebElement339        {340            var webElements = searchContext.GetElements(locator);341            return342                new ReadOnlyCollection<T>(343                    webElements.Select(e => e.As<T>()).ToList());344        }345        /// <summary>346        /// Finds elements that meet specified conditions.347        /// </summary>348        /// <typeparam name="T">IWebComponent like Checkbox, Select, etc.</typeparam>349        /// <param name="searchContext">The search context.</param>350        /// <param name="locator">The locator.</param>351        /// <param name="condition">The condition to be met.</param>352        /// <returns>353        /// Located elements.354        /// </returns>355        /// <example>How to find displayed elements and specify element type to get additional actions for them : <code>356        /// var checkboxes = this.Driver.GetElements<Checkbox>(this.stackOverFlowCheckbox, e => e.Displayed);357        /// checkboxes[0].TickCheckbox();358        /// </code></example>359        public static IList<T> GetElements<T>(this ISearchContext searchContext, ElementLocator locator, Func<IWebElement, bool> condition)360            where T : class, IWebElement361        {362            var webElements = searchContext.GetElements(locator, condition);363            return364                new ReadOnlyCollection<T>(365                    webElements.Select(e => e.As<T>()).ToList());366        }367        /// <summary>368        /// To the driver.369        /// </summary>370        /// <param name="webElement">The web element.</param>371        /// <returns>372        /// Driver from element.373        /// </returns>374        /// <exception cref="System.ArgumentException">Element must wrap a web driver.</exception>375        public static IWebDriver ToDriver(this ISearchContext webElement)376        {...GetElement
Using AI Code Generation
1using System;2using System.Collections.Generic;3using System.Linq;4using System.Text;5using System.Threading.Tasks;6using Ocaramba;7using Ocaramba.Extensions;8using Ocaramba.Types;9using NUnit.Framework;10using OpenQA.Selenium;11using OpenQA.Selenium.Remote;12using OpenQA.Selenium.Chrome;13using OpenQA.Selenium.Support.UI;14using System.Threading;15{16    {17        private readonly DriverContext driverContext = new DriverContext();18        public void Setup()19        {20            var driver = new ChromeDriver();21            driverContext.Driver = driver;22        }23        public void TestGetElement()24        {25            driverContext.Driver.Navigate().GoToUrl(url);26            var searchField = driverContext.Driver.GetElement(By.Name("q"));27            searchField.SendKeys("Selenium");28            var searchButton = driverContext.Driver.GetElement(By.Name("btnK"));29            searchButton.Click();30        }31        public void TearDown()32        {33            driverContext.Driver.Quit();34        }35    }36}37using System;38using System.Collections.Generic;39using System.Linq;40using System.Text;41using System.Threading.Tasks;42using Ocaramba;43using Ocaramba.Extensions;44using Ocaramba.Types;45using NUnit.Framework;46using OpenQA.Selenium;47using OpenQA.Selenium.Remote;48using OpenQA.Selenium.Chrome;49using OpenQA.Selenium.Support.UI;50using System.Threading;51{52    {53        private readonly DriverContext driverContext = new DriverContext();54        public void Setup()55        {56            var driver = new ChromeDriver();57            driverContext.Driver = driver;58        }59        public void TestGetElements()60        {61            driverContext.Driver.Navigate().GoToUrl(url);62            var searchField = driverContext.Driver.GetElement(By.Name("q"));63            searchField.SendKeys("Selenium");64            var searchButton = driverContext.Driver.GetElement(By.Name("btnK"));65            searchButton.Click();66            var results = driverContext.Driver.GetElements(By.CssSelector("div.g"));67            Console.WriteLine("NumberGetElement
Using AI Code Generation
1using System;2using System.Collections.Generic;3using System.Linq;4using System.Text;5using System.Threading.Tasks;6using Ocaramba;7using Ocaramba.Extensions;8using Ocaramba.Types;9using NUnit.Framework;10using OpenQA.Selenium;11using System.Threading;12using OpenQA.Selenium.Interactions;13using OpenQA.Selenium.Support.UI;14{15    {16        public void Test()17        {18            var element = Driver.GetElement(By.Id("lst-ib"));19            element.SendKeys("Hello World");20            var element2 = Driver.GetElement(By.Name("btnK"));21            element2.Click();22            Thread.Sleep(5000);23        }24    }25}GetElement
Using AI Code Generation
1using System;2using System.Collections.Generic;3using System.Linq;4using System.Text;5using System.Threading.Tasks;6using Ocaramba;7using Ocaramba.Extensions;8using Ocaramba.Types;9using NUnit.Framework;10using OpenQA.Selenium;11using OpenQA.Selenium.Chrome;12using OpenQA.Selenium.Firefox;13{14    {15        private readonly DriverContext driverContext = new DriverContext();16        private IWebDriver driver;17        public void TestMethod()18        {19            driver = new FirefoxDriver();20            driver.Navigate().GoToUrl(url);21            var element = driver.GetElement(By.Id("customers"));22            Assert.AreEqual(element.TagName, "table");23            driver.Quit();24        }25    }26}27using System;28using System.Collections.Generic;29using System.Linq;30using System.Text;31using System.Threading.Tasks;32using Ocaramba;33using Ocaramba.Extensions;34using Ocaramba.Types;35using NUnit.Framework;36using OpenQA.Selenium;37using OpenQA.Selenium.Chrome;38using OpenQA.Selenium.Firefox;39{40    {41        private readonly DriverContext driverContext = new DriverContext();42        private IWebDriver driver;43        public void TestMethod()44        {45            driver = new FirefoxDriver();46            driver.Navigate().GoToUrl(url);47            var element = driver.GetElement(By.Name("customers"));48            Assert.AreEqual(element.TagName, "table");49            driver.Quit();50        }51    }52}53using System;54using System.Collections.Generic;55using System.Linq;56using System.Text;57using System.Threading.Tasks;58using Ocaramba;59using Ocaramba.Extensions;60using Ocaramba.Types;61using NUnit.Framework;62using OpenQA.Selenium;63using OpenQA.Selenium.Chrome;64using OpenQA.Selenium.Firefox;65{GetElement
Using AI Code Generation
1using OpenQA.Selenium;2using Ocaramba.Extensions;3using Ocaramba.Types;4using NUnit.Framework;5{6    {7        private static readonly ElementLocator ElementLocator = new ElementLocator(Locator.CssSelector, "div#div1");8        public void GetElementTestsTest()9        {10            IWebDriver driver = new DummyDriver();11            var element = driver.GetElement(ElementLocator);12            Assert.AreEqual("div#div1", element.TagName);13        }14    }15}16using OpenQA.Selenium;17using Ocaramba.Extensions;18using Ocaramba.Types;19using NUnit.Framework;20{21    {22        private static readonly ElementLocator ElementLocator = new ElementLocator(Locator.CssSelector, "div#div1");23        public void GetElementTestsTest()24        {25            IWebDriver driver = new DummyDriver();26            var element = driver.GetElement(ElementLocator);27            Assert.AreEqual("div#div1", element.TagName);28        }29    }30}31using OpenQA.Selenium;32using Ocaramba.Extensions;33using Ocaramba.Types;34using NUnit.Framework;35{36    {37        private static readonly ElementLocator ElementLocator = new ElementLocator(Locator.CssSelector, "div#div1");38        public void GetElementTestsTest()39        {40            IWebDriver driver = new DummyDriver();41            var element = driver.GetElement(ElementLocator);42            Assert.AreEqual("div#div1", element.TagName);43        }44    }45}46using OpenQA.Selenium;47using Ocaramba.Extensions;48using Ocaramba.Types;49using NUnit.Framework;50{51    {52        private static readonly ElementLocator ElementLocator = new ElementLocator(Locator.CssSelector, "div#div1");53        public void GetElementTestsTest()GetElement
Using AI Code Generation
1using Ocaramba;2using Ocaramba.Extensions;3using Ocaramba.Types;4using NUnit.Framework;5using OpenQA.Selenium;6{7    {8        public void TestGetElementByName()9        {10            var searchContext = Driver.GetDriver() as ISearchContext;11            var element = searchContext.GetElement(By.Name("q"), 5);12            Assert.IsNotNull(element);13        }14    }15}16using Ocaramba;17using Ocaramba.Extensions;18using Ocaramba.Types;19using NUnit.Framework;20using OpenQA.Selenium;21{22    {23        public void TestGetElementByTagName()24        {25            var searchContext = Driver.GetDriver() as ISearchContext;26            var element = searchContext.GetElement(By.TagName("input"), 5);27            Assert.IsNotNull(element);28        }29    }30}31using Ocaramba;32using Ocaramba.Extensions;33using Ocaramba.Types;34using NUnit.Framework;35using OpenQA.Selenium;36{37    {38        public void TestGetElementByXPath()39        {40            var searchContext = Driver.GetDriver() as ISearchContext;41            Assert.IsNotNull(element);42        }43    }44}45using Ocaramba;46using Ocaramba.Extensions;47using Ocaramba.Types;48using NUnit.Framework;49using OpenQA.Selenium;50{51    {52        public void TestGetElementByCssSelector()53        {GetElement
Using AI Code Generation
1{2    public void TestGetElement()3    {4        Assert.IsNotNull(element);5    }6}7{8    public void TestGetElements()9    {10        Assert.IsNotNull(elements);11        Assert.AreEqual(2, elements.Count);12    }13}14{15    public void TestGetElement()16    {17        Assert.IsNotNull(element);18    }19}20{21    public void TestGetElement()22    {23        Assert.IsNotNull(element);24    }25}26{27    public void TestGetElement()28    {29        Assert.IsNotNull(element);30    }31}32{33    public void TestGetElement()34    {35        Assert.IsNotNull(element);36    }37}38{39    public void TestGetElement()40    {GetElement
Using AI Code Generation
1{2    using System;3    using NUnit.Framework;4    using OpenQA.Selenium;5    using Ocaramba.Extensions;6    using Ocaramba.Types;7    {8        public void GetElementByClassName()9        {10            this.DriverContext.Driver.SwitchTo().Frame("iframeResult");11            var element = this.DriverContext.Driver.GetElement(By.ClassName("test"), "test");12            Assert.True(element.Displayed);13        }14    }15}16{17    using System;18    using NUnit.Framework;19    using OpenQA.Selenium;20    using Ocaramba.Extensions;21    using Ocaramba.Types;22    {23        public void GetElementByCssSelector()24        {25            this.DriverContext.Driver.SwitchTo().Frame("iframeResult");26            var element = this.DriverContext.Driver.GetElement(By.CssSelector("input[type='text']"), "input[type='text']");27            Assert.True(element.Displayed);28        }29    }30}31{32    using System;33    using NUnit.Framework;34    using OpenQA.Selenium;35    using Ocaramba.Extensions;36    using Ocaramba.Types;37    {Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
