Skip to content

Finding text in web page

Péter Bencze edited this page Jun 19, 2019 · 1 revision

Find text in web page

TextFinder is a helper class which can be used to find text in web elements.

Create an instance

A text matching pattern must be specified. Optionally, it is also possible to provide one or more locating mechanisms. These are used to locate web elements on the page, whose content is searched for matching text. If not specified, the By.tagName("body") locator is used by default.

Find text

Use:


Example

public class MyCrawler extends Crawler {

    private final TextFinder textFinder;

    public MyCrawler(final CrawlerConfiguration config) {
        super(config);

        // A helper class that is intended to make it easier to find text on web pages
        textFinder = new TextFinder(Pattern.compile("text pattern", Pattern.MULTILINE));
    }

    @Override
    protected void onResponseSuccess(final ResponseSuccessEvent event) {
        textFinder.findFirstInResponse(event.getCompleteCrawlResponse())
                .ifPresent(matchResult -> {
                    // Do something with the matched text...
                });

        // ...
    }
}