Writing data to text file using selenium webdriver

package selenium_examples;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

public class Test{
 public static void main(String[] args) throws IOException {
 
   
   BufferedWriter bw = new BufferedWriter(new FileWriter("E:\\test.txt"));
   bw.write("Helloooo");
   bw.newLine();
                 bw.write("This is a new line");
                 bw.newLine();
                 bw.write("One more new line");
                 bw.close();
                 System.out.println("Done!");
 
   }
 }

Script 2:-

package selenium_examples;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Test {
 public static void main(String[] args) throws IOException {
 
  WebDriver driver = new FirefoxDriver();
         driver.get("https://www.google.com");
         String s=driver.getTitle();
    
  BufferedWriter bw = new BufferedWriter(new FileWriter("E:\\test1.txt"));
  bw.write(s);
         bw.close();
                System.out.println("Done!");
 
   }
 }

 

Selenium Webdriver Scripts - Executing multiple sql queries using JDBC

package selenium_examples;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class Database {
 public static void main(String[] args) throws ClassNotFoundException, SQLException {
  Class.forName("org.postgresql.Driver");
  Connection con = DriverManager.getConnection(dbUrl,username,password);
                //dbUrl="jdbc:postgresql://hostname:port/dbname"
  System.out.println("Connected to Database");
  Statement stmt =con.createStatement();
  Statement stmt1 =con.createStatement();
  ResultSet rs = stmt.executeQuery("select id from emp");
  ResultSet rs1 = stmt1.executeQuery("select deptno from dept");
 
  while (rs.next()) {
  
             int x = rs.getInt("id");
             System.out.println("id :"+ x);
 }
  while (rs1.next())
  {
  
            int z = rs1.getInt("deptno");
            System.out.println("deptno :"+ z);
 }
  con.close();
  System.out.println("Connection Closed");
 }

}


 

Selenium webdriver - How to get inner text of the cell of a html table

package selenium_examples;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Example {
 public static void main(String[] args)
     {

  WebDriver driver = new FirefoxDriver();
 
  driver.get("http://www.w3schools.com/html/html_tables.asp");
 
  String text=driver.findElement(By.xpath("//table/tbody/tr[3]/td[2]")).getText();
 
  System.out.println(text);
 
  }

Output:- Jackson
Above script gets 3rd row,2nd column cell text from below shown table.

working with webtables selenium webdriver
 

Reading data from notepad/text file using selenium webdriver

package selenium_examples;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class Test{
 public static void main(String[] args) throws IOException {
 
          
         FileReader r = new FileReader("E:\\test.txt");
         BufferedReader bfr = new BufferedReader(r);
         String x ="";
       
         while((x = bfr.readLine()) != null){
             System.out.println(x);
 
   }
         bfr.close();
 }
}
 

Related Posts Plugin for WordPress, Blogger...

Fun and Knowledge