Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get value from a placeholder using xpath

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?

like image 216
user2572510 Avatar asked Dec 20 '13 05:12

user2572510


People also ask

How do you find the value of XPath?

We can find an element using the xpath locator with Selenium webdriver. To identify the element with xpath, the expression should be //tagname[@attribute='value']. To identify the element with xpath, the expression should be //tagname[@class='value']. There can be two types of xpath – relative and absolute.

How do you getText from a text box in Selenium?

We can get the entered text from a textbox in Selenium webdriver. To obtain the value attribute of an element in the html document, we have to use the getAttribute() method. Then the value is passed as a parameter to the method. Let us consider a textbox where we entered some text and then want to get the entered text.

In which interface getText () and getAttribute () is available?

getText() is a method which gets us the visible (i.e. not hidden by CSS) innerText of this element, including sub-elements, without any leading or trailing white space. The getAttribute() method is declared in the WebElement interface, and it returns the value of the web element's attribute as a string.


2 Answers

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.

like image 150
kiran Avatar answered Oct 09 '22 17:10

kiran


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

like image 37
Karthikeyan Avatar answered Oct 09 '22 17:10

Karthikeyan