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).