1

I wrote a program where values are coming from sql database.Now i want to send those values to frontend using selenium. say it is an pincode i fetched from Database but now if locate the element where i want to store that fetched otp how will I do?.sendkeys in selenium is not accepting Piece of code I wrote :

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class JavaConnect2SQL {

    public static void main(String[] args) throws ClassNotFoundException {
        
        try {
        Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
        // TODO Auto-generated method stub
//String url="jdbc:sqlserver://LAPTOP-F0E1C4GT\\SQLEXPRESS;databaseName=BikeStores;";
//String user="sa";
//String password="NewPassword@123";

String url = "jdbc:sqlserver://localhost;user=sa;password=NewPassword@123;databaseName=BikeStores";
//connServer.connect(url);


    Connection connection=DriverManager.getConnection(url);
    System.out.println("Connected");
    Statement statement = connection.createStatement();

    String query = "select  TOP 1 pincode from pintable";
    ResultSet rs = statement.executeQuery(query);
    while(rs.next()){
       System.out.println(rs.getString(1));
        
        
        System.setProperty("webdriver.chrome.driver","D:\\chromedriver.exe");
        WebDriver driver=new ChromeDriver();
        driver.navigate().to("https://indialends.com/");
        driver.findElement(By.linkText("Personal Loan")).click();
        //driver.findElement(By.name("li_display_name")).toString(rs.getString(1));
        //Belowline->Is my confusion??
driver.findElement(By.name("pincode")).sendKeys("122018");//this 122018 i am writing by sendkeys but i want this to be filled from SQL itself

} }catch (SQLException e) {
    // TODO Auto-generated catch block
    System.out.println("Oops,there is error");
    e.printStackTrace();
}
        }
}

PLease help and suggest

0
2
driver.findElement(By.name("pincode")).sendKeys(rs.getString(1));

Is this what you looking for

3
  • 1
    @ Ravneet kaur, we should upvote an answer if it helped. – Vishal Aggarwal Jan 16 at 10:04
  • 1
    @RavneetKaur you don't have enough reputation you accound accept the answer by clicking the tick sign near to answer . Once you have enough reputation upvote and accept the answer :) if it helps Vishal Thank you .. :) – PDHide Jan 16 at 10:06
  • yes sure vishal – Ravneet Kaur Jan 17 at 7:05
0

I would also suggest to make it less messy:

  • Make few generic db connection/retrieval functions
  • if data retrieval is not dynamic , create separate script to populate a data source like an file: json, csv or excel to further use directly in simpler way in tests.
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.