๐Ÿ” Locating and Interacting with Web Elements

๐Ÿ” Locating and Interacting with Web Elements in Selenium

✅ Why is Locating Elements Important?

To automate web testing, Selenium needs to find (locate) elements on a webpage—buttons, text boxes, links, etc.—to interact with them (click, type, read text).


๐Ÿ”‘ Common Ways to Locate Elements

Locator Type How It Works Example (Java)

id Finds element by unique id attribute driver.findElement(By.id("username"))

name Finds element by name attribute driver.findElement(By.name("email"))

className Finds element by class attribute driver.findElement(By.className("btn"))

tagName Finds element by tag name driver.findElement(By.tagName("input"))

linkText Finds anchor (<a>) by exact text driver.findElement(By.linkText("Home"))

partialLinkText Finds anchor by partial match driver.findElement(By.partialLinkText("Read"))

cssSelector Finds element using CSS selectors driver.findElement(By.cssSelector(".menu > li"))

xpath Finds element using XPath expressions driver.findElement(By.xpath("//div[@id='main']"))


๐Ÿ› ️ How to Interact with Elements

Once located, you can perform actions like:


Action Example (Java) Description

Click element.click(); Click a button or link

Type text element.sendKeys("hello"); Enter text into input fields

Clear text element.clear(); Clear existing text from inputs

Get text String text = element.getText(); Retrieve visible text

Check if displayed element.isDisplayed(); Check if element is visible

Check if enabled element.isEnabled(); Check if element can be interacted with

Submit form element.submit(); Submit a form element


๐Ÿ”Ž Example: Login Form Automation

java

Copy

Edit

WebDriver driver = new ChromeDriver();

driver.get("https://example.com/login");


// Locate username and password fields and login button

WebElement username = driver.findElement(By.id("username"));

WebElement password = driver.findElement(By.id("password"));

WebElement loginBtn = driver.findElement(By.cssSelector("button.login"));


// Interact with elements

username.sendKeys("myUser");

password.sendKeys("myPassword");

loginBtn.click();

๐Ÿ’ก Tips for Reliable Locators

Prefer id and name for uniqueness and speed.


Use CSS selectors for flexible, readable queries.


Use XPath for complex queries but keep them simple and robust.


Avoid brittle locators that depend on position or exact text that may change.


Use browser DevTools (Inspect Element) to experiment with selectors.


๐Ÿ“ฆ Bonus: Waiting for Elements

To avoid issues where Selenium tries to interact before elements load:


java

Copy

Edit

WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));

WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("username")));

element.sendKeys("myUser");

✅ Summary

Step Code Snippet

Locate element by id driver.findElement(By.id("id"))

Click element element.click();

Type text element.sendKeys("text");

Learn Selenium Python Training in Hyderabad

Read More

Setting Up Selenium in PyCharm or VS Code

Top Tools You Need to Start Selenium with Python

How to Automate Web Testing Using Selenium and Python

Python vs Other Languages for Selenium: Pros & Cons

Visit Our IHUB Talent Training Institute in Hyderabad

Get Directions 

Comments

Popular posts from this blog

Handling Frames and Iframes Using Playwright

Tosca for API Testing: A Step-by-Step Tutorial

Working with Tosca Parameters (Buffer, Dynamic Expressions)