<...">

How to get title attribute of input element? - webdriver

How to get Title attribute in input element

<input type="image" title="Previous Page"> <input type="image" title="First Page"> <input type="image" title="Next Page"> <input type="image" title="Last Page"> 
+5
source share
4 answers

What have you tried? Typically, the following should work:

 WebElement element = driver.findElement(By.tagName("input")); String title = element.getAttribute("title"); 
+12
source

The answer provided by Jim Evans is correct, but for a more specific one I would suggest something like below. Remember that pasta copy may not work, and you need to change something to be able to work with your full HTML.

 List<WebElement> elements = driver.findElements(By.tagName("input")); for (WebElement element : elements) { if (element.getAttribute("type").equals("image")) { System.out.println(element.getAttribute("title")); } } 

The above code will loop on all web pages that are of type = "image" and print the "title" attribute on each of them.

However, you should vote for Jim as the right one, though.

+3
source

First you need to define the input element from which you want to get the value of the title attribute.

Then the following should work:

 element.getAttribute("title"); 
+1
source

It is very simple and works for.

 String title = driver.getTitle(); 
-1
source

All Articles