Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

253
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda