Java Selenium:如何筛选含指定子元素的info类元素并存入ArrayList
Got it, let's work through this contact info extraction problem to get that existence check logic solid. Here's a straightforward, practical approach to make sure only complete contact entries make it into your ArrayList:
Core Logic Breakdown
We'll iterate over every info element, check that all three required child elements (n, adr, primary phone) are present, and only add the entry to our list if all three exist.
Step 1: Set Up Your Parsing & List
Assuming you're using Jsoup (the go-to library for web scraping in Java), start by grabbing all info elements and initializing your ArrayList:
// Parse your HTML content (could be from a URL or string) Document doc = Jsoup.parse(yourHtmlSource); Elements allInfoElements = doc.select(".info"); // Initialize your list to hold valid contacts ArrayList<Contact> validContacts = new ArrayList<>();
Step 2: Add Existence Validation for Required Fields
For each info element, use selectFirst() to fetch each child element—this returns null if the element doesn't exist, which makes our null check simple and safe:
for (Element infoElement : allInfoElements) { // Fetch each required child element Element nameElement = infoElement.selectFirst(".n"); Element addressElement = infoElement.selectFirst(".adr"); // Note: If the phone class is "primary phone" (space-separated), it's two classes—use .primary.phone to match both Element phoneElement = infoElement.selectFirst(".primary.phone"); // Validate all three elements exist before proceeding if (nameElement != null && addressElement != null && phoneElement != null) { // Extract clean text (adjust to use html() if you need formatting) String name = nameElement.text().trim(); String address = addressElement.text().trim(); String phone = phoneElement.text().trim(); // Add the complete contact to your list validContacts.add(new Contact(name, address, phone)); } }
Step 3: Create a Contact POJO
You'll need a simple class to store the contact data neatly:
public class Contact { private String name; private String address; private String phone; public Contact(String name, String address, String phone) { this.name = name; this.address = address; this.phone = phone; } // Add getters/setters if you need to access the data later public String getName() { return name; } public String getAddress() { return address; } public String getPhone() { return phone; } }
Key Tips
- Class Selector Note: If the phone element has classes
primaryandphone(space-separated),.primary.phoneis the correct selector (no space) to match elements with both classes. - Null Safety: Using
selectFirst()avoids exceptions that would happen if you usedselect().get(0)on a missing element. - Flexibility: Swap
text()forhtml()if you need to preserve nested formatting or links in the address/name fields.
This setup ensures you only collect contacts with all three required pieces of information, skipping any incomplete entries automatically.
内容的提问来源于stack exchange,提问作者ztrition

