想要从数据库中检索电子邮件字段并在Selenium Xpath中显示

时间:2016-09-28 10:25:49

标签: java mysql xpath jdbc selenium-webdriver

想要从数据库中检索电子邮件字段并在xpath中显示 这是我的代码,请检查一下我提供的Selenium Xpath类和我的db类

public static void main(String[] args) {
    Connection conn = null;
    Statement stmt = null;
    try{
      //STEP 2: Register JDBC driver
      Class.forName("com.mysql.jdbc.Driver");
      //STEP 3: Open a connection
      System.out.println("Connecting to a selected database...");
      conn = DriverManager.getConnection(DB_URL, USER, PASS);
      System.out.println("Connected database successfully..."); //STEP 4: Execute a query
      System.out.println("Creating statement...");
      stmt = conn.createStatement();

      String sql = "SELECT client_id, first_name, last_name, email FROM person";
      ResultSet rs = stmt.executeQuery(sql);
      //STEP 5: Extract data from result set
      while(rs.next()){
         int id  = rs.getInt("client_id");
         String email = rs.getString("email");
         String first = rs.getString("first_name");
         String last = rs.getString("last_name");

         //Display values
         System.out.print("ID: " + id);
         System.out.print(", Age: " + email);
         System.out.print(", First: " + first);
         System.out.println(", Last: " + last);
      }
    public class Login extends DB {

    public void Loginfill(){
    System.setProperty("webdriver.chrome.driver","E:\\workspace\\chromeDriver\\chromeDriver.exe");

        ChromeDriver driver = new ChromeDriver();
        driver.get("http://qa-0.ls.vu");
        driver.findElement(By.id("nav-loginButton")).click();
        driver.findElement(By.id("login_username")).sendKeys("");
        driver.findElement(By.id("login_password")).sendKeys("learnship123$");
        driver.findElement(By.xpath("/html/body/nav/div[2]/div[2]/div/form[1]/button")).click();    
    }
}

请帮我说出我必须做的事情

1 个答案:

答案 0 :(得分:0)

你的问题不明确,但我认为这样的事情应该有效:

public static void main(String[] args){

String email = getEmail();
Login.LoginFill(email);
}

static String getEmail() throws SQLException {
    Connection conn = null;
    Statement stmt = null;

    System.out.println("Connecting to a selected database...");
    conn = DriverManager.getConnection(DB_URL, USER, PASS);
    System.out.println("Connected database successfully..."); //STEP 4: Execute a query
    System.out.println("Creating statement...");
    stmt = conn.createStatement();

    String sql = "SELECT email FROM person";
    ResultSet rs = stmt.executeQuery(sql);
    //STEP 5: Extract data from result set

    String email = rs.getString("email");

    //Display values
    System.out.print(", email: " + email);

    return email;
}
class Login {

    public static void Loginfill(String email){
        System.setProperty("webdriver.chrome.driver","E:\\workspace\\chromeDriver\\chromeDriver.exe");

        ChromeDriver driver = new ChromeDriver();
        driver.get("http://qa-0.ls.vu");
        driver.findElement(By.id("nav-loginButton")).click();
        driver.findElement(By.id("login_username")).sendKeys(email);
        driver.findElement(By.id("login_password")).sendKeys("learnship123$");
        driver.findElement(By.xpath("/html/body/nav/div[2]/div[2]/div/form[1]/button")).click();
    }
}