How to get value from a placeholder using xpath
Asked Answered
H

4

15

All of the elements are dynamic. I can see only Placeholder which is unique from the following html:-

<input 
   id="ext-gen1617" 
   type="text" 
   size="20" 
   class="x-form-field x-form-text x-form-focus" 
   autocomplete="off" 
   aria-invalid="false" 
   placeholder="Gender" 
   data-errorqtip="" 
   role="textbox" 
   aria-describedby="combobox-1166-errorEl" 
   aria-required="true" 
   style="width: 78px;"
/>

I need to get the value displayed in

placeholder="Gender". 

I tried using

//input[@placeholder='Gender']

But my webdriver script failed to identify it.

Can anyone please help me out with possible solution to it?

Hurleigh answered 20/12, 2013 at 5:46 Comment(4)
what error you are getting ? Is driver in the right frame?Berlin
org.openqa.selenium.NoSuchElementException: Unable to find element with xpath == //*[@placeholder='Gender']Hurleigh
There are many possibilities for this error, If your case is not fit into here #17678087 , then let me know we can dig furtherBerlin
Hello Karthikeyan I could not find any solution in the link which you provided.Hurleigh
P
19
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.

Pertinacity answered 20/12, 2013 at 7:22 Comment(5)
I need to click inside the Gender field. How can i do that if i use getAttribute() which will return a string not webelement?Hurleigh
you mean u need to enter some value in gender filed ??Pertinacity
in the above snippet u asked for the value displayed in place holder that's why i printed place holder value in my codePertinacity
Every Webelement in the above snippet is dynamic. The only static web element is Placeholder whose value is "Gender". I thought of using this placeholder element and click inside the "Gender" text box. After clicking inside it i will get 2 options -- Male & Female. For selecting anyone of these 2 options i have the xpath. But clicking inside the Gender text box itself is not working. I used the following:- //input[@placeholder='Gender'] //input[contains(@class,'x-form-field')][@placeholder='Gender']Hurleigh
To use this with Angular Material design I replaced @placeholder with @data-placeholder and removed the .getAttribute(...Wessels
B
1

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

Berlin answered 20/12, 2013 at 11:56 Comment(0)
A
0

Try

driver.findElement(
   By.cssSelector("input[id*='ext-gen']")
).getAttribute("placeholder")

Let me know is the above statement is working or not.

Attestation answered 9/4, 2014 at 9:1 Comment(0)
G
0

The best way is to find the element with CSS selector in this case - input[placeholder='Gender'], which can easily find the element.

Grivation answered 26/7, 2022 at 18:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.