How to pass array of WebElement type to a method

Answered

Hi there,

I am trying to create 2 methods viz One method to locate web element on the page using driver.findElement. Method accepts set of multiple xpaths from excel file like xpath1,xpath2,xpath3…..so on. On next I have separated all xpaths based on ‘,’ comma sign using split() and saved them into an array ‘strarray’ and did driver.findElement(By.xpath(“strarray[i]”) using loop.

Now issue is that, I need to save these values into an array of WebElement type. After doing this, I need to return this array to some other method so that I can do further action using those returned value.

Is there any for implementing same. Or if I am wrong then please suggest a way.

Add Comment
  • 7 Answer(s)
    Best answer

    Please try to use arraylist instead of arrays , that’s my personal suggestion.

    Thanks,
    Vishal

    Doctor Answered on September 9, 2014.
    Add Comment

    1. Return the values from the your previous method.

    2. Create a method of type array of strings.

    3. Assign the returned values to an array of webelement type.

    4. Do whatever processing with these values now.

    Thanks,
    Vishal S

    Doctor Answered on September 9, 2014.
    Add Comment

    Hi  Vishaalcbe,

    Please have a look over below mentioned code.

    private WebElement strelement = null;
    private WebElement[] strelement1 = null;
    private String strtext,strtemp,strelmntxpath;
    private String[] strarr1,strarr2,strarr3;

    public WebElement[] find_multipleElement(String strelementidvalue) throws IOException, InterruptedException{
    System.out.println(“Entered inside the new keyword”);
    strarr1 = strelementidvalue.split(“=”);
    if(strarr1.length == 2) {
    strtext = strarr1[0];
    strelmntxpath = strarr1[1];
    strarr2=strelmntxpath.split(“,”)
    if(strtext.equals(“xpath”)) {
    try {
    for(int i=0; i<strarr2.length; i++){
    WebElement strelmntxpath1 = driver.findElement(By.xpath(strarr2[i]));
    strelement1[i] = strelmntxpath1;
    }
    }catch (NoSuchElementException e) {
    strelement1 = null;
    }
    }
    }
    else
    strelement1 = null;

    return strelement1;
    }

    This is that method which should return an array of WebElement type

    Suppose : Value passed to the function as strelementidvalue is

    xpath=/html/body/div[1]/div/div[2]/div[3]/ul/li[1]/a/span[2],/html/body/div[1]/div/div[2]/div[3]/ul/li[9]/a/span[2],/html/body/div[1]/div/div[4]/div[3]/ul/li[12]/a/span[2]

    Please help me regarding this

    Train Answered on September 9, 2014.
    Add Comment

    I shall give you a sample program. You would learn more on tweaking this code.

    import java.util.*;

    class Listuser {
    void a() {
    String b[]={“1″,”2″};
    c(b);
    }

    void c(String b[]) {
    int e=b.length;
    for(int f=0;f<e;f++) {
    System.out.print(b[f]+” “);//Single Space
    }
    }

    public static void main(String args[]) {
    Listuser ob=new Listuser();
    ob.a();
    }

    // private void c(String[] b) {
    // throw new UnsupportedOperationException(“Not yet implemented”);
    // }
    }

    Doctor Answered on September 9, 2014.
    Add Comment

    Hi Vishal,

    Thanx  for your help…Yes you are right, Arraylist would be better option than arrays.

    Train Answered on September 9, 2014.
    Add Comment

    Hi Vishal,

    Your suggestion works like charm. Finally it worked with ArrayList of WebElement type. I am sharing that code which can help a newbie in future.

    Here strelementidvalue is like xpath=/html/body/div[1]/div[5]/div[2]/div[5],/html/body/div[1]/div[5]/div[2]/div[5]/div/div[2],/html/body/div[1]/div[5]/div[2]

    It contains 3 xpaths separated by ‘commas’ being read from excel file. After that each xpath has been separated ad processed to do locate element in required webpage. Everytime I added returning value i.e Webelement is being appended into ArrayList of WebElement type and this list is being returned as value of below mentioned menthod.

    public ArrayList<WebElement> find_multipleElement(String strelementidvalue) throws IOException, InterruptedException{
    strelementList = new ArrayList<WebElement>();
    strarr1 = strelementidvalue.split(“=”);
    if(strarr1.length == 2) {
    strtext = strarr1[0];
    strelmntxpath = strarr1[1];
    if(strtext.equals(“xpath”)) {
    try{
    StringTokenizer st = new StringTokenizer(strelmntxpath,”,”);
    while(st.hasMoreTokens()){
    //strelement1 = st.nextToken();
    strelementList.add(driver.findElement(By.xpath(st.nextToken())));
    }
    } catch(NoSuchElementException e) {
    strelementList = null;
    }
    }
    else
    strelementList = null;
    }

    return strelementList;
    }

    Anyway Vishal, once again thanks for your help.

    Train Answered on September 11, 2014.
    Add Comment

    Bingo

    Doctor Answered on September 12, 2014.
    Add Comment
  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.