String s=driver.findElement(By.xpath("//input[@placeholder='Gender']")).getAttribute("placeholder"); 
System.out.println(s);

To get an attribute for a filed, you can use the .getAttribute() method.

Answer from kiran on Stack Overflow
🌐
BrowserStack
browserstack.com › home › guide › mastering selenium css selectors in 2026
CSS Selector in Selenium: Locate Elements with Examples | BrowserStack
December 10, 2025 - Learn to use CSS Selector in Selenium scripts for your automated tests with five types of CSS Selectors and code snippet examples.
🌐
Sauce Labs
saucelabs.com › home › blog › selenium tips: css selectors
Selenium Tips: CSS Selectors
April 2, 2023 - For classes, things are pretty similar in XPATH: “[@class='example']” while in CSS it’s just “.” ... This is useful for navigating lists of elements, such as forms or ul items. The next sibling will tell selenium to find the next adjacent element on the page that’s inside the same parent. Let’s show an example using a form to select the field after username. ... <input type = "text" class = "form-control" id = "username" name = "username" placeholder = "username" required autofocus></br>
Discussions

selenium webdriver - How to get value from a placeholder using xpath - Stack Overflow
To use this with Angular Material ... @data-placeholder and removed the .getAttribute(... 2020-12-14T17:35:23.077Z+00:00 ... I assume you are dealing with (front end)script generated web elements, then you must need to embrace lean way of thinking. Don't try to pull out a web element by it's property alone. If you are not getting them try to build a xpath from its parent or siblings. ... The best way is to find the element with CSS selector in this case ... More on stackoverflow.com
🌐 stackoverflow.com
python - Selenium - How to find an element by part of the placeholder value? - Stack Overflow
Is there any way to find an element by a part of the placeholder value? And ideally, case insensitive. Search by the following fu... More on stackoverflow.com
🌐 stackoverflow.com
How to locate the input tag as per the HTML provided through Selenium? - Stack Overflow
Please what locator do i use for the below. I have tried Xpath and CSS Selector but no luck. More on stackoverflow.com
🌐 stackoverflow.com
September 6, 2018
html - I need help finding a web element in selenium Java - Stack Overflow
I thought maybe searching by placeholder since that is static would be a good idea but I do not know how to go about that. ... Both queries should work. What is the method returning? ... Is this element inside an iframe. ... You are making mistake with css selector. More on stackoverflow.com
🌐 stackoverflow.com
🌐
TOOLSQA
toolsqa.com › selenium-webdriver › css-selectors-in-selenium
How to use and create CSS Selectors in Selenium with examples?
Subsequently, we can use them together to create a CSS Selector for locating the web element, as shown below: textarea.form-control[placeholder='Current Address'] ... Then we provided the value of the class attribute.
🌐
Stack Overflow
stackoverflow.com › questions › 52202904 › how-to-locate-the-input-tag-as-per-the-html-provided-through-selenium
How to locate the input tag as per the HTML provided through Selenium? - Stack Overflow
September 6, 2018 - 1) Using Java as script language driver.findElement(By.cssSelector("input[placeholder='Password']")) // or driver.findElement(By.xpath("//input[@placeholder='Password']")) 2) Using python as script language dirver.find_element_by_css_selector("input[placeholder='Password']") // or driver.find_element_by_xpath("//input[@placeholder='Password']") ... Please see below the error: Exception in thread "main" org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"css selector","selector":"input[placeholder='Password']"} (Session info: chrome=68.0.3440.106)
🌐
Javatpoint
javatpoint.com › selenium-css-selector-id-class-and-attribute
Selenium CSS Selector - ID/Class and Attribute - javatpoint
Selenium CSS Selector - ID/Class and Attribute with Introduction, features, selenium basic terminology, what is selenium, selenium limitations, selenium vs qtp, tool suite, selenium ide, ide-installation, ide-features, ide-first test case, ide-commands, ide-creating test cases manually, ide-login test etc.
Find elsewhere
🌐
Slajobs
slajobs.com › home › css selectors in selenium
Learn To Use CSS Selectors In Selenium | Examples - Selenium Course At SLA
December 15, 2023 - <input type = “password” class = “form-control” id = “password” name = “password” placeholder = “password” required> ... <button class = “btn btn-lg btn-primary btn-block radius” type = “submit” name = “login”>Login</button> ... Now let’s construct an XPath and CSS selector to determine which input field should come after “username.” If the form is rearranged, this will choose a different element or the “alias” input. ... Selenium’s attribute selector allows you to choose elements based on any attribute value if the ordering of the child elements is not important to you.
🌐
Frugal Testing
frugaltesting.com › blog › selenium-tips-css-selector
Selenium Tips- CSS Selector
<form class = "form-login" role = "form" action = "/main.php" method = "post"> <h3 class = "form-heading"></h3> <input type = "text" class = "form-control" id = "useremailid" name= "useremailid" placeholder = "useremailid" required autofocus> <input type = "password" class = "form-control" id = "userpassword" name ="userpassword" placeholder = "userpassword" required> <p> <button class = "btn btn-lg btn-primary" type = "submit" name = "login">Login</button> </form> Now let’s write an XPath and CSS Selector to choose element after emailid.
🌐
TestersDock
testersdock.com › home › css selectors tutorial
A detailed overview of how CSS Selectors are used in Selenium WebDriver
April 1, 2022 - In this example, we want to select the second item of the list but we don’t have any attribute that can be considered. So to select the second list item without any list id/class/name, we will locate the first list item with its id and then go to the next. So, the CSS selector in this case would be li#fruit+li.
🌐
Requestly
requestly.com › home › uncategorized › css selector in selenium: complete tutorial with examples
CSS Selector in Selenium: Complete Tutorial with Examples
September 17, 2025 - Learn how to use CSS selectors in Selenium to locate elements, handle dynamic content, and write robust automation tests.
🌐
GeeksforGeeks
geeksforgeeks.org › css › css-placeholder-selector
CSS ::placeholder Selector - GeeksforGeeks
July 11, 2025 - The ::first-line pseudo-element is the only subset of CSS properties that is applicable & can be applied in a rule using ::placeholder in its selector.
🌐
TestGrid
testgrid.io › selenium › how to use css selectors in selenium webdriver
CSS Selectors in Selenium: Types, Syntax & Examples
June 16, 2025 - Learn how to use CSS Selectors in Selenium to locate dynamic web elements using ID, class, attributes, and partial string matching.
🌐
Testmuai
testmuai.com › testmu ai › learning hub › css selectors in selenium
CSS Selectors in Selenium: A Complete Guide to Locating Web Elements
February 3, 2026 - Learn how to use CSS Selectors in Selenium to locate web elements efficiently with syntax, examples, and best practices for reliable test automation.
Top answer
1 of 3
1

The code you've used to locate the element concerned, are both wrong, as explained below:

1- driver.findElement(By.cssSelector(".T4"));
Since you have put "." before "T4", it will search for the element having class T4, which is not present. "T4", in fact is the innerHTML/text of the element you want to locate.

2-driver.findElement(By.cssSelector("//input[@id='gbqfq']"));
As @BoltClock commented, this is infact an xpath that you are trying to use with a cssSelector method.

You can use the below code to locate the element using xpath, instead:

driver.findElement(By.xpath("//div[.='T4']"));

This will locate the div element with exact innerHTML/text as 'T4'.

Furthermore, You can also use the 'id', provided it is unique and doesn't change dynamically as below:

1- driver.findElement(By.xpath("//div[@id='99']"));
This will locate the div element with id '99'.

2- driver.findElement(By.id("99"));
This will locate the element with id '99'.

3-driver.findElement(By.cssSelector("div[id='99']"));
This will also locate the div element with id '99'.

2 of 3
0

You are using XPath selector with By.cssSelector() which is wrong and in css . means class and you are trying to pass the text instead. You cannot perform text based search with css selector at this momenet. XPath is the best option in such case.

nth-child() gives you the ability to find the child element with index if you want to use the cssSelector. See this

//css walk thorugh with index
.dojoDndItem:nth-child(1)

However, I would prefer xpath text based search in this case

//div[.='1']
//div[.='T4']

..etc