0

Is there anyway I could do this thing i'm trying to do

So I want to run my java program on a website If I sees a certain text anywhere in the page It clicks on a link that is in the html code

Here is what I mean

Say the text i'm looking for is Banana and if it finds Banana on the page it goes to a link in the html code Is there anyway I can do that?

2
  • You need a HTML parser for java. Search them and use them, but first read the documentations. Commented Jan 2, 2015 at 12:39
  • there are many ways to do this. Commented Jan 2, 2015 at 12:46

2 Answers 2

1

I would recommend using jsoup because of it's css selectors

the code could then look somewhat like this:

Document doc = Jsoup.connect("http://en.wikipedia.org/").get();
Elements elements = doc.select("a");
for(Element e : elements) {
    if(e.text().contains("banana")) {
        String linkURL = e.attr("abs:href");
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

This demo perhaps should help you.

String demo = "<select id='list'><option value='0'>First value</option><option value='1'>Second value</option><option value='2'>Third value</option></select>";


        Document document = Jsoup.parse(demo);
        Elements options = document.select("select > option");

        for(Element element : options)
        {
           System.out.println(element.attr("value"));
        }

You would need to use Jsoup to parse html for more on it visit this link.

Note : I have used a String to parse you can directly connect to a url and parse the response html.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.