This post was most recently updated on August 5th, 2024
Here is example to find element by Locator Type as ID
- ID is attribute used with HTML element to uniquely identify it in HTML document.
- Faster and easiest way to locate an element in HTML document using selenium.
How to find web element by using id in Java language
1 |
WebElement wElement=driver.findElement(By.id("id_attribute_value")); |
Steps to find ID attribute of web element.
- Open url https://www.societyhive.com/ in chrome browser.
- Open Developers tool using F12 or Ctrl+Shift+I.
- Inspect element by using Ctrl+Shift+C or by clicking on arrow icon in devTools.
- Click on input box having placeholder text as “Society name” on web page.
Java code to find above element by id attribute is:
1 |
WebElement inputBox1=driver.findElement(By.id("txtSocietyName")); |
To enter some text in text box
1 |
inputBox1.SendKeys("Temp Value"); |
To clear entered values in Text box
1 |
inputBox1.clear(); |