New Relative Locator methods (Below, RightOf, LeftOf) in Selenium 4 with C Sharp

New​​ Relative Locator​​ methods​​ (Below, RightOf, LeftOf)​​ in​​ Selenium 4​​ with C Sharp

Welcome to​​ 3rd​​ article in​​ Selenium 4​​ (new features) with C Sharp​​ series.

What you will Learn:

  • Relative Locator ‘Below’ method

  • Relative Locator ‘RightOf’ method

  • Relative Locator ‘LeftOf’ method

Relative Locator ‘Below’ method

The ‘Below’​​ method is exactly opposite of ‘Above’ method that we studied​​ in previous article.

Launch the website
https://www.way2automation.com/angularjs-protractor/registeration/#/login​​ ​​ 

The use case​​ we want to automate​​ is: we want to locate​​ the ‘username description’ text​​ field​​ that is​​ Below​​ the ‘Username *’​​ label​​ and​​ then write some text inside this​​ field

Inspect text field

Inspect​​ Username *, notice that it is represented by tagname ‘label

So we can write​​ the below 2 lines using the ‘Below’ method (self-explanatory)

Save and run the script.​​ 

Notice below that selenium has successfully located the ‘username description’​​ text​​ field below the ‘Username *’​​ label​​ and typed the text inside it

Relative Locator ‘RightOf’ method

Launch

https://www.selenium-tutorial.com/p/selenium-training​​ 

Notice above that ‘Enroll in Course for FREE’ is right to ‘Watch Promo’. So we can use​​ RightOf​​ method and click ‘Enroll in​​ Course for FREE’

Inspect ‘Watch Promo’, notice it has a unique id

Inspect​​ ‘Enroll in​​ Course for FREE’ button

So we can write the below script

Save and run​​ the script.

See below.​​ ‘Enroll in​​ Course for FREE’ gets clicked and​​ the registration page opens

Relative Locator ‘LeftOf’ method

LeftOf​​ method is opposite of ‘RightOf​​ that we saw above.

We can use this method to click ‘Watch Promo’ that is left of ‘Enroll in Course for FREE’

Let us use​​ the TagName ‘a’ to identify​​ ‘Watch Promo’​​ element

We can write below 2 lines to accomplish the purpose

Save and run script, notice below that promo video page comes up

So this is how we use the relative locator methods.

Code snippet (Below)

using​​ OpenQA.Selenium;

using​​ OpenQA.Selenium.Chrome;

using​​ System;

using​​ System.Collections.Generic;

using​​ System.Linq;

using​​ System.Text;

using​​ System.Threading;

using​​ System.Threading.Tasks;

 

namespace​​ RelLoc

{

 ​​ ​​ ​​​​ class​​ Program

 ​​ ​​ ​​​​ {

 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ static​​ void​​ Main(string[] args)

 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ {

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ IWebDriver driver =​​ new​​ ChromeDriver();

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ driver.Navigate().GoToUrl("https://www.way2automation.com/angularjs-protractor/registeration/#/login");

 

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ Thread.Sleep(2000);

 

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ IWebElement lbl = driver.FindElement(By.TagName("label"));

 

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ IWebElement elem = driver.FindElement(RelativeBy.WithLocator(By.Id("formly_1_input_username_0")).Below(lbl));

 

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ elem.SendKeys("hello");

 

 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ }

 ​​ ​​ ​​​​ }

}

 

Code snippet (RightOf)

using​​ OpenQA.Selenium;

using​​ OpenQA.Selenium.Chrome;

using​​ System;

using​​ System.Collections.Generic;

using​​ System.Linq;

using​​ System.Text;

using​​ System.Threading;

using​​ System.Threading.Tasks;

 

namespace​​ RelLoc

{

 ​​ ​​ ​​​​ class​​ Program

 ​​ ​​ ​​​​ {

 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ static​​ void​​ Main(string[] args)

 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ {

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ IWebDriver driver =​​ new​​ ChromeDriver();

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ driver.Navigate().GoToUrl("https://www.selenium-tutorial.com/p/selenium-training");

 

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ Thread.Sleep(2000);

 

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ IWebElement promoField = driver.FindElement(By.Id("watchpromo"));

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ driver.FindElement(RelativeBy.WithLocator(By.TagName("button")).RightOf(promoField)).Click();

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ }

 ​​ ​​ ​​​​ }

}

 

Code snippet (LeftOf)

using​​ OpenQA.Selenium;

using​​ OpenQA.Selenium.Chrome;

using​​ System;

using​​ System.Collections.Generic;

using​​ System.Linq;

using​​ System.Text;

using​​ System.Threading;

using​​ System.Threading.Tasks;

 

namespace​​ RelLoc

{

 ​​ ​​ ​​​​ class​​ Program

 ​​ ​​ ​​​​ {

 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ static​​ void​​ Main(string[] args)

 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ {

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ IWebDriver driver =​​ new​​ ChromeDriver();

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ driver.Navigate().GoToUrl("https://www.selenium-tutorial.com/p/selenium-training");

 

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ Thread.Sleep(2000);

 

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ IWebElement enrolField = driver.FindElement(By.Id("enroll-button-top"));

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ driver.FindElement(RelativeBy.WithLocator(By.TagName("a")).LeftOf(enrolField)).Click();

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ }

 ​​ ​​ ​​​​ }

}

 

Thank you for reading!

Share On

Share on facebook
Share on twitter
Share on linkedin
Share on whatsapp
Share on tumblr
Share on email

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top

Lifetime Membership Club

LIFETIME MEMBERSHIP BIG SALE - ALL LIVE COURES JUST - 10000 RS/149 USD
Attend All Live courses in just 10000 rs / $149 - offer ends 31st May 2024