Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

249
Views
Selenium-Get list index number based on text match

Automation scenario is to get all the links in weblist & then get this arraylist content outside loop and then get the index number dynamically based on the url hit.

String values="";
List<WebElement> url_link = driver.findElements(By.cssSelector(".anomaly>a"));
for ( WebElement we: url_link) { 

    String temp = values;
    values = temp + we.getText();
}
System.out.println("Text "+values);
int ind=values.indexOf("www.test.com");

System.out.println("Index "+ind);

The above code returns me a weird index number of 74.

The url_link output contents are:

wwww.hatch.com
wwww.tist.com
wwww.helix.com
wwww.patching.com
wwww.seyh.com
wwww.test.com
wwww.toast.com
wwww.telling.com
wwww.uity.com

so based upon the expected result i am expecting the index number to be 5.

Any help will be appreciated.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

The variable values is a variable of type String, it's not a List. So when you do values = temp + we.getText();, you are basically appending all the links in a single String. So the output will actually be this:

wwww.hatch.comwwww.tist.comwwww.helix.comwwww.patching.comwwww.seyh.comwwww.test.comwwww.toast.comwwww.telling.comwwww.uity.com

The index of www.test.com is 74 in this string. You should be adding these links in an ArrayList and then find the index of the link you want to search.

Something like this should work:

List<String> values = new ArrayList<String>();
List<WebElement> url_link = driver.findElements(By.cssSelector(".anomaly>a"));
for ( WebElement we: url_link) { 

    values.add(we.getText());
}

int ind = values.indexOf("www.test.com");

System.out.println("Index "+ind);
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!