Finding an element by partial id with Selenium in C#.

SeleniumAutomation TestingTesting Tools

We can find an element by partial is with Selenium in C#. This can be done as we identify elements with the locator's CSS and xpath. The regular expression is used to find the partially matched element.

Let us investigate the id attribute of an element having value as gsc−i−id1.

In xpath, we utilize the contains() function for partial matching. So, here xpath expression shall be //*[contains(@id, 'id')]. This is because the subtext id is within the text gsc−i−id1. We can also take the help of the starts−with() function. So, the xpath expression becomes //*[starts−with(@id, 'gsc')] as the text gsc−i−id1 starts with the subtext gsc.

In CSS, we utilize the * symbol for partial matching. So, here CSS expression shall be input[id*='id']. This is because the subtext id is within the text gsc-i-id1. We can also take the help of the ^ symbol. So, the CSS expression becomes input[id^='gsc'] as the text gsc−i−id1 starts with the subtext gsc. Moreover, we can use the $ symbol. So, the xpath expression becomes input[id$='id1'] as the text gsc−i−id1 ends with the subtext id1.

For implementation we shall be using the NUnit framework.

Example

using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using System;
namespace NUnitTestProject1{
   public class Tests{
      String u =
      "https://www.tutorialspoint.com/about/about_careers.htm";
      IWebDriver d;
      [SetUp]
      public void Setup(){
         //creating object of FirefoxDriver
         d = new FirefoxDriver();
      }
      [Test]
      public void Test1(){
         d.Navigate()
         .GoToUrl(u);
         // identify element with * in CSS
         IWebElement l
         = d.FindElement(By.CssSelector("input[id*='id']"));
         l.SendKeys("C#");
         //obtain input value
         Console.WriteLine(l.GetAttribute("value"));
         l.Clear();
         // identify element with ^ in CSS
         IWebElement m
         = d.FindElement(By.CssSelector("input[id^='gsc']"));
         m.SendKeys("NUnit");
         //obtain input value
         Console.WriteLine(m.GetAttribute("value"));
         m.Clear();
         // identify element with $ in CSS
         IWebElement n
         = d.FindElement(By.CssSelector("input[id$='id1']"));
         n.SendKeys("Selenium");
         //obtain input value
         Console.WriteLine(n.GetAttribute("value"));
         n.Clear();
         // identify element with contains in xpath
         IWebElement o = d.
         FindElement(By.XPath("//input[contains(@id,'id')]"));
         o.SendKeys("Java");
         //obtain input value
         Console.WriteLine(o.GetAttribute("value"));
         o.Clear();
         // identify element with starts-with() in xpath
         IWebElement p = d.
         FindElement(By.XPath("//input[starts−with(@id,'gsc')]"));
         p.SendKeys("Python");
         //obtain input value
         Console.WriteLine(p.GetAttribute("value"));
         p.Clear();
      }
      [TearDown]
      public void close_Browser(){
         d.Quit();
      }
   }
}

Output

Click on Run All Tests

Click on Open additional output for this result link −

We should get the Test Outcome and Standard Output.

raja
Published on 30-Jan-2021 12:26:25
Advertisements