Is'haq
Is'haq

Reputation: 184

How to get text from a class placeholder textbox

I have been trying to read the text from a textbox which is a class place holder as you can see below

UPDATE:

 <div id="annotationHistoryDataTable_filter" class="dataTables_filter">

  <label>Search:<input type="search" class="" placeholder="" aria- 
  controls="annotationHistoryDataTable">

 </label></div>

I am using the below code to get the text but i am getting a null value. This usually gets all the text but is not working for this kind of text box.

 var theseElements = driver.FindElement(By.TagName("annotationHistoryDataTable")).Text;

Tried with By.LinkText too but still not working.

Here is the example of the textbox i want to read

enter image description here

Thank you.

Upvotes: 0

Views: 845

Answers (3)

PDHide
PDHide

Reputation: 19939

Your locator looks incorrect:

 var theseElements = driver.FindElement(By.TagName("annotationHistoryDataTable")).Text;

There is no tag with that name ,use below locator instead

 var theseElements = driver.FindElement(By.XPATH("//input[@aria-controls=\"annotationHistoryDataTable\" and @type = \"search\"]"));

Now try ;

 theseElements.text
 theseElements.getAttribute('value')

Update:

so as elements are inside iframe you need to switch to it first

driver.SwitchTo().Frame(driver.FindElement(By.XPATH("iframexpath")));
var theseElements = driver.FindElement(By.XPATH("//input[@aria-controls=\"annotationHistoryDataTable\" and @type = \"search\"]"));
theseElements.getAttribute('value') //print this

If you want to interact with elements outside the iframe after this , then you have to switch outside of iframe;

driver.SwitchTo().DefaultContent(); // just add this line after you are done with interacting with iframe elements

Upvotes: 1

HedgeHog
HedgeHog

Reputation: 25048

What happens

  1. You try to get that input by driver.FindElement(By.TagName("annotationHistoryDataTable")).Text;

  2. Ask yourself: Is there a TagName annotationHistoryDataTable?

So how to select this input?

  1. It is an input and it has an aria-controls, use this in your xpath:

    driver.FindElement(By.XPATH("//input[@aria-controls='annotationHistoryDataTable']"))
    
  2. The text is not stored between the <input> it is stored in the value attribute, so you should access it like:

    .GetAttribute("value")
    

Example

It is in python, but should show you what I mean.

from selenium import webdriver
browser = webdriver.Chrome('C:\Program Files\ChromeDriver\chromedriver.exe')

html_content = """
  <label>
   Search:<input type="search" class="" placeholder="" aria-controls="annotationHistoryDataTable"> 
 </label>
"""

browser.get("data:text/html;charset=utf-8,{html_content}".format(html_content=html_content))

browser.find_element_by_xpath("//input[@aria-controls='annotationHistoryDataTable']").get_attribute("value")
browser.close()

Upvotes: 1

Jonathan PETIT
Jonathan PETIT

Reputation: 196

You should try this :

 var theseElements = driver.FindElement(By.TagName("annotationHistoryDataTable")).get_attribute("innerText");

Upvotes: 1

Related Questions