Wednesday, 17 August 2016

Selenium2: WebDriver: getText: Get visible text

WebElement interface provides getText method, which returns the visible (i.e. not hidden by CSS) innerText of this element, including sub elements.


index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
 pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>

<body>
 <div id="div1">
  <h1 id="header1">Simple application to demonstrate Selenium
   webdriver</h1>
 </div>

 <div id="div2">
  <p id="para1">WebDriver interface provides number of methods to
   locate elements in a web page. You can locate an element by using
   class name, id, name, linktext etc.,</p>
 </div>
</body>
</html>

Assume index.jsp is available at “http://localhost:8080/application”.

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class App {
 public static void main(String[] args) {
  WebDriver driver = new FirefoxDriver();
  driver.get("http://localhost:8080/application");

  WebElement div1 = driver.findElement(By.id("div1"));
  WebElement div2 = driver.findElement(By.id("div2"));

  System.out.println(div1.getText());
  System.out.println("****************");
  System.out.println(div2.getText());

  driver.close();
 }
}


Output
Simple application to demonstrate Selenium webdriver
****************
WebDriver interface provides number of methods to locate elements in a web page. You can locate an element by using class name, id, name, linktext etc.,






Previous                                                 Next                                                 Home

No comments:

Post a Comment