Selenium WebDriver - 如何返回文本的第一个单词

时间:2014-07-01 19:38:14

标签: java selenium-webdriver

成功页面上会返回姓氏和名字。以下两种方法都将Last Name返回给数据文件。我怎样才能获得返回的名字?我的第一个方法'getMemberFirstName'出了点问题 该字段没有id,name或class。

 public String getMemberFirstName(){
     String mbrFirstName= "";
     int indx;
     page.waitForTextToLoad("has been successfully added");
     mbrFirstName = page.getFieldTextBy(By.cssSelector("form[name=AddCustomerForm] font"));
     indx = mbrFirstName.indexOf(" "); //Isolate First Name from other text in field
     mbrFirstName = mbrFirstName.substring(indx).trim();
     System.out.println("First Name: " + mbrFirstName);
     return mbrFirstName;
 }

 public String getMemberLastName(){
     String mbrLastName= "";
     int indx;
     page.waitForTextToLoad("has been successfully added");
     mbrLastName = page.getFieldTextBy(By.cssSelector("form[name=AddCustomerForm] font"));
     indx = mbrLastName.indexOf(" ")+1; //Isolate Last Name from other text in field
     mbrLastName = mbrLastName.substring(indx).trim();
     System.out.println("Last Name: " + mbrLastName);
     return mbrLastName;
 }

1 个答案:

答案 0 :(得分:0)

您需要更改:

 mbrFirstName = mbrFirstName.substring(0, indx).trim();

http://docs.oracle.com/javase/7/docs/api/index.html?java/lang/String.html