Monday, 21 August 2023

Explain Relative locators in Selenium 4 with Examples?





 SDET Interview Question and Answers

TestNG Interview questions and answers

Jenkins Interview Questions and Answers

Appium Interview Questions and Answers

Selenium Interview Questions and answers

Java Coding Interview Questions and Answers

GIT Interview Questions and Answers

Let's review our understanding of Selenium first.

Webdriver:

WebDriver drives a browser natively, as a user would, either locally or on a remote machine using the Selenium server, marks a leap forward in terms of browser automation.

Selenium WebDriver refers to both the language bindings and the implementations of the individual browser control code. This is commonly referred to as just WebDriver and its not a class but an INTERFACE.

Selenium WebDriver is a W3C Recommendation

  • WebDriver is designed as a simple and more concise programming interface.

  • WebDriver is a compact object-oriented API.

  • It drives the browser effectively.

  • The latest release is Selenium 4.


What is relative Locators ?

Relative locators in Selenium 4 provide a set of new strategies to locate elements based on their proximity to other elements. These strategies include methods like above(), below(), toLeftOf(), toRightOf(), and more. They offer enhanced flexibility and accuracy in locating elements, making test automation more robust and efficient.

Here's a brief explanation with example code for each of the relative locators in Selenium 4:


  1. above():

    The above() method allows you to locate an element that is positioned above another specified element.

WebElement element = driver.findElement(By.id("targetElement")); WebElement aboveElement = driver.findElement(RelativeLocator
.above(By.id("targetElement")));

  1. below():

    The below() method helps you locate an element that is positioned below a specified element.

WebElement element = driver.findElement(By.id("targetElement")); WebElement belowElement = driver.findElement(RelativeLocator
.below(By.id("targetElement")));


****

How to handle StaleElement Exception IN selenium? Ans: Check the post HERE for example

***
  1. toLeftOf():

    The toLeftOf() method enables you to locate an element that is positioned to the left of another specified element.

WebElement element = driver.findElement(By.id("targetElement")); WebElement leftElement = driver.findElement(RelativeLocator
.toLeftOf(By.id("targetElement")));



*******
How to IMPLEMENT POM in Selenium Automation Framework with Code? Ans: Check the post HERE for example of implementation with example *******
  1. toRightOf():

    The toRightOf() method lets you locate an element that is positioned to the right of another specified element.

WebElement element = driver.findElement(By.id("targetElement")); WebElement rightElement = driver.findElement(RelativeLocator
.toRightOf(By.id("targetElement")));


  1. near():

    The near() method allows you to locate an element near another specified element using a proximity radius.

WebElement element = driver.findElement(By.id("targetElement")); WebElement nearElement = driver.findElement(RelativeLocator
.near(By.id("targetElement")).with(By.tagName("button")));



*******

76. What are the new features in Selenium 4? VERY IMP 

Click Here For Answer

*********

  1. withTagName():

     The withTagName() method narrows down the search within the relative locator to elements with a specific tag name.

WebElement element = driver.findElement(By.id("targetElement")); WebElement tagElement = driver.findElement(RelativeLocator
.above(By.id("targetElement")).withTagName("input"));

  1. withText():

    The withText() method allows you to locate an element with a specific text value within the relative locator's search scope.

WebElement element = driver.findElement(By.id("targetElement")); WebElement textElement = driver.findElement(RelativeLocator
.toLeftOf(By.id("targetElement")).withText("Submit"));


  1. below() with Offset:

    You can use the below() method along with an offset value to locate an element below another element with a specific distance.

WebElement element = driver.findElement(By.id("targetElement")); WebElement belowWithOffset = driver.findElement(RelativeLocator
.below(By.id("targetElement")).near(By.tagName("input"))
.withOffset(10, 20));


These relative locators enhance the precision of element identification in Selenium 4 by considering their proximity to other elements. They are particularly useful in cases where traditional locators might not be accurate enough.

⭐️⭐️⭐️⭐️⭐️

📌YouTube channel:
https://lnkd.in/gHJ5BDJZ

📌Telegram group:
https://lnkd.in/gUUQeCha

📌Schedule 1:1 call:
https://lnkd.in/ddayTwnq

📌Medium blogs:
https://lnkd.in/gkUX8eKY


Revision:


49. Which Source code management tool have you used and tell me some of the basic commands that you have used?

Click here to answer


50. Have you worked on database side for fetching data to validate in selenium tests, if Yes then can you tell me some of the basic queries that you have used in day to day work?

Click here for answer with 11 queries


36.  How to launch batch file from Selenium WebDriver?

 Process batch = Runtime.getRuntime.exec("path of the batch file");


37. How to run selenium test from the command line?

java -classpath ".;selenium-server-standalone-2.33.0.jar" SampleClass


38. What is the name of the super interface of the Webdriver?

Ans: SearchContext.


39. Explain some of the strategies you have followed while using xpath?

Click here to answer


40. How do we execute the TestNG test suite from the command line?

Ans: java -cp “C:\AutomationReinvented\testng \lib\*;C:\AutomationReinvented\testng\bin” org.testng.TestNG testng.xml


*******************************************************************
For any doubts or career guidance from me, reach out here : https://topmate.io/sidharth_shukla

********************************************************************

Do remember that knowing Linux is one of the most important aspect to work as an SDET.

Basic Linux Commands for Automation QA


****************************************

TOP 15 BDD - CUCUMBER Interview Q&A


************************************************

✍️AUTHORLinkedIn Profile

************************************************

Learn (API-Microservice)Testing+ Selenium UI Automation-SDET with Self Paced Videos prepared by FAANG employees and LIVE Doubt Session 

SDET TRANING VIDEOS AVAILABLE with Live Doubt Session Check Training Page for Course Content or reach out @whatsapp +91-9619094122. 
This includes classnotes, 500+ interview questions, 3 projects, Java Coding question set for product companies along with career guidance from FAANG employees for Automation and SDET.

For more details whatsapp : https://lnkd.in/dnBWDM33

Course_001API Automation +
UI Automation +
Mobile Testing +
ChatGPT For Test Automation +
Jenkins-GIT-Docker
Course_002API Automation +
UI Automation +
Jenkins-GIT-Docker
Course_003API Automation +
ChatGPT for Test Automation +
Jenkins-GIT
Course_004ChatGPT for Test Automation
Course_005API Automation +
Jenkins-GIT

************************************************


SeleniumWebdriver Automation Testing Interview Questions:

https://automationreinvented.blogspot.com/search/label/SeleniumWebdriver

API Testing Interview Question Set:

https://automationreinvented.blogspot.com/2022/03/top-80-api-testing-interview-questions.html

DevOps Interview Q&A:

https://automationreinvented.blogspot.com/2021/11/top-11-devops-interview-questions-and.html 

Kubernetes Interview Question Set

https://automationreinvented.blogspot.com/search/label/Kubernetes

Docker Interview Question Set

https://automationreinvented.blogspot.com/Docker

Linux Interview question Set

https://automationreinvented.blogspot.com/search/label/Linux

Automation Testing/SDET Framework Design

https://automationreinvented.blogspot.com/search/label/FrameworkDesign

Java Related Interview Question Set

https://automationreinvented.blogspot.com/search/label/Java

GIT Interview Question Set:

https://automationreinvented.blogspot.com/2021/09/top-40-git-interview-questions-and.html

Coding Interview Question Set:

https://automationreinvented.blogspot.com/search/label/Coding%20Questions

Mobile Testing Interview Question Set:

https://automationreinvented.blogspot.com/search/label/Mobile%20Testing

Python Interview Question Set for QAE - SDET - SDE:

https://automationreinvented.blogspot.com/search/label/Python

 


All Time Popular Posts

Most Featured Post

Introduction to Web Testing with Playwright

  Introduction to Web Testing with Playwright 📌  Telegram Group:  https://t.me/+FTf_NPb--GQ2ODhl 📌  YouTube channel:  https://lnkd.in/gGUG...