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 Overflowselenium webdriver - How to get value from a placeholder using xpath - Stack Overflow
python - Selenium - How to find an element by part of the placeholder value? - Stack Overflow
How to locate the input tag as per the HTML provided through Selenium? - Stack Overflow
html - I need help finding a web element in selenium Java - Stack Overflow
Videos
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.
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.
say, the HTML goes like this,
<div id="somestatic id">
<div id="xyz">
<input name="dynamic one"/>
</div>
</div>
Then you can build a xpath as ,
//*[@id='staticID']/div/input
for the HTML,
<div id="staticID"></div>
<input name="dynamic one"/>
the xpath is ,
//*[@id='staticID']/following-sibling::input
similarly there are n number of option available. Give them a try
You can always use contains instead of equals, as following:
browser.find_element(By.XPATH, "//input[contains(@placeholder,'some te')]")
To make it case-insensitive you can use translate function, as following:
browser.find_element(By.XPATH, "//input/@placeholder[contains(translate(., 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'),'some te')]")
Case-insensitive contains Xpath is taken from this question
@Prophet's answer is good, but I wanted to add CSS case too since it may be helpful.
You can use ^= to search for attributes starting with certain keyword and $= for attribute starting with certain keywrd and *= for attribute containing a keyword.
browser.find_element(By.CSS_SELECTOR, 'input[placeholder*="some te"]')
You can make it case-insensitive by adding i before closing the bracket
browser.find_element(By.CSS_SELECTOR, 'input[placeholder*="some te" i]')
You can try to find element by its placeholder
driver.findElement(By.xpath("//input[@placeholder='Ask me anything!']"))
You are making mistake with css selector.
I think you forgot to put . in front of class name while using css selector.
Correct css locator would be .ember-view.ember-text-field.search
So your final code look like :
String cssLocator = ".ember-view.ember-text-field.search";
WebElement ele= driver.findElement(By.cssSelector(cssLocator));
or if you want to make it more specific then you can use:
String cssLocator = ".ember-view[placeholder='Ask me anything!']"; also
You can get the placeholder text using the getAttribute method of webdriver.
The HTML:
<input id="<ur id>" class="<ur class name>" type="password" lang="en" maxlength="30" placeholder="Enter Password" data-label="passwordPlaceholder" tabindex="5">
The Java code:
String password=driver.findElement(By.cssSelector("ur css path")).getAttribute("placeholder");
I've done similar tests. For our fields with placeholder text, the text appears in a placeholder attribute on the element. In Ruby, we use code like this:
element = @driver.find_element(*<locator string>*)
expected_placeholder_text = element.attribute('placeholder')
That gives us a string that we compare just like any other string. You could also shorten the element.attribute call to be
element['placeholder']
as well, but we prefer the former (for non-relevant and completely arbitrary reasons).
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'.
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