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?

Joe
  • 337
  • 2
  • 16

2 Answers2

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");
    }
}
EyeSpy
  • 215
  • 1
  • 12
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.

Darshan Lila
  • 5,772
  • 2
  • 24
  • 34