0

As you can see I Have two list and I want output like one from one list and second from second list following is my code please help me in this.

Code in Selenium Webdriver in Java:

@Test
public void firstcase() {

    List<WebElement> elements = dr.findElements(By.xpath("//span[@class='job-card__subtitle hidden-xs']//span"));
    System.out.println("Company name:" + elements.size());

    List<WebElement> Jobelements = dr.findElements(By.xpath("(//div[contains(@class,'job-card__title title-overflow')])"));
    System.out.println("Profile:-" + Jobelements.size());

    for (WebElement temp : elements) {
        System.out.println("Company name:" +temp.getText());

    }

    for (WebElement tempjob : Jobelements) {
        System.out.println("Profile:-" +tempjob.getText());

    }
}

my output is:=
Company name:-6
Profile:-6
Company name:-Canon
Company name:-Reliance Automation
Company name:-Alibaba
Company name:-Kodak
Company name:-Vodafone
Company name:-PeopleGrove
Profile:-Market Researcher
Profile:-Program Manager
Profile:-Developer
Profile:-Market Researcher
Profile:-Sales Representative
Profile:-Customer Support Specialist

I want Output like Company name:Canon new line Profile:-Market Researcher

2 Answers 2

1
@Test
    public void firstcase() {

        List<WebElement> elements = dr.findElements(By.xpath("//span[@class='job-card__subtitle hidden-xs']//span"));
        System.out.println("Company name:" + elements.size());

        List<WebElement> Jobelements = dr.findElements(By.xpath("(//div[contains(@class,'job-card__title title-overflow')])"));
        System.out.println("Profile:-" + Jobelements.size());


        for (i=0;i<elements.size();++i) {
            System.out.println("Profile:-" +Jobelements.get(I).getText());
            System.out.println("Company name:-" +elements.get(I).getText());

        }

    }

Just iterate through size

6
  • Thank you for your help but it's showing error when i am adding for loop and error is the type of the type of the expression must be array type but it resolved to List <WebElement> Feb 7, 2021 at 9:21
  • Which line shows that error
    – PDHide
    Feb 7, 2021 at 11:03
  • @Jitendra updated the answer instead of [i] use get(I)
    – PDHide
    Feb 7, 2021 at 11:06
  • You can use [I] only for simple array for list interface you should use get(I) to get the balue
    – PDHide
    Feb 7, 2021 at 11:08
  • Just because of your help i got my answer which i want. Thank You for help... Feb 7, 2021 at 18:01
0
ArrayList<String> cars = new ArrayList<String>();
cars.add("Volvo");
cars.add("BMW");
cars.add("Ford");
cars.add("Mazda");

ArrayList<Integer> numbers = new ArrayList<Integer>();
numbers.add(12);
numbers.add(13);
numbers.add(14);
numbers.add(15);


// Get the iterator
Iterator<String> it = cars.iterator();

Iterator<Integer> it1 = numbers.iterator();

while(it.hasNext()){

System.out.println(it.next());

if(it1.hasNext())

System.out.println(it1.next());

}

Results are:

Volvo
12
BMW
13
Ford
14
Mazda
15
1
  • why to add that much extraline of code?
    – PDHide
    Feb 4, 2021 at 16:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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