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




  1. Go to http://gyansangrah.com/ContactUs.aspx
  2. Access the email id textbox using CSS selector.
  3. Write text in the textbox.
  4. 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


Picture showing how to Locate web elements using CSS selector in selenium
Click to Enlarge


Posted By  -  Karan Gupta
 
Posted On  -  Tuesday, April 4, 2017

Query/Feedback


Your Email Id
 
Subject
 
Query/FeedbackCharacters remaining 250