👉 Crack SDET QA Interviews with a Powerful Self-Introduction 👉 https://topmate.io/qa_sdet_automation...
👉 Top 50 SDET Behavioral Interview Questions & Answers 👉 https://topmate.io/qa_sdet_automation...
Level up your SDET and QA skills! 🚀 SELENIUM : findElement using cssClass
SDET Automation Testing Interview Questions & Answers
We will be covering a wide range of topics including QA manual testing, automation testing, Selenium, Java, Jenkins, Cucumber, Maven, and various testing frameworks.
In Selenium with Java, you can locate a web element using its CSS class attribute using the By class and the cssSelector method.
Here's an example code snippet that demonstrates how to do this:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class FindElementByCssClass {
public static void main(String[] args) {
// Creating a new ChromeDriver instance
WebDriver driver = new ChromeDriver();
// Navigating to the website
driver.get("https://www.example.com");
// Locating the element using its CSS class attribute
WebElement element = driver.findElement(By.cssSelector(".class-name"));
// Performing an action on the element
element.click();
// Closing the browser window
driver.quit();
}
}
In this example, the By.cssSelector method is used to locate the element using its CSS class name, which is represented by the .class-name selector.
The driver.findElement method is used to find the first element that matches the selector.
Once the element is located, it can be interacted with using various methods provided by the WebElement class. Finally, the driver.quit() method is used to close the browser window.