Articles → SELENIUM → Locate Web Elements Using CSS Selector In Selenium
Locate Web Elements Using CSS Selector In Selenium
Prerequisite
Syntax
driver.findElement(By.cssSelector(""))
Example
- Go to http://gyansangrah.com/ContactUs.aspx
- Access the email id textbox using CSS selector.
- Write text in the textbox.
package com.FirstSeleniumProject;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
public class MyClass {
public static void main(String[] args) {
WebDriver driver;
String service = "IEDriverServer path";
System.setProperty("webdriver.ie.driver", service);
driver = new InternetExplorerDriver();
driver.get("http://gyansangrah.com/ContactUs.aspx");
// Locating by link text
driver.findElement(By.cssSelector("[id=ctl00_cphGyanSangrah_txtEmailID]")).sendKeys("a@a.in");
}
}
Output
Click to Enlarge