Search code examples
javajsoup

Get element from multiple div class with colon in css html


There are 2 classes with the same name

<div class="website text:middle"><a href="/class/name" class=" "> A</a></div>
<div class="website text:middle"><a href="/class/grade" class=" "> 1</a></div>

How to get A and 1? I tried using getElementById with :eq(0) and it gives out null


Solution

  • Method getElementById queries for elements with a specified id, not class; I'm not sure what you were trying to query with :eq(0) either.

    Try:

    // String html = ...
    Document doc = Jsoup.parse(html);
    List<String> result = doc.getElementsByClass("text:middle").eachText();
    // result = ["A", "1"]
    

    EDIT

    You can query for elements that match multiple classes! See Jsoup select div having multiple classes.

    However, a colon (:) is a special character in css and needs to be escaped when it appears as part of a class name in a selector query. I don't think that jsoup currently supports this and simply treats everything after a colon as a pseudo-class.