headzoo / surf

Stateful programmatic web browsing in Go.
MIT License
1.49k stars 160 forks source link

Click a link that doesn't have a class name ? #74

Closed Eon1 closed 7 years ago

Eon1 commented 7 years ago

Is it possible to click a link that doesn't have a class name like this ?

<a href="/some/kind/of/link">The Link</a>

I know you can do

bow.Click("a.new") Like in the docs which will find any anchor with the class "new" but how do we click a link by just if it has a anchor tag and some text like "The Link" ?

Click wants a selector string. Is there any way to get that from bow.Find ? I can get all the links with

bow.Find("a").Each(func(_ int, s *goquery.Selection) {
        if s.Text() == "The Link" {
            //how do i click on the link here ? 
        }
}

But then bow.Click wants a selection string and I have a goquery.Selection. How do I click the link once I've found it ?

shavit commented 7 years ago

At the moment it does not click links, just extract the URL string from the href attribute and open it using private methods.

You can extract the URLs the same way

bow.Find("a").Each(func(I int, sel *goquery.Selection){
  var src string
  src, ok := sel.Attr("href")

  // open the link
  // look at the click method:
  //   bow.httpGet ...
})

https://github.com/headzoo/surf/blob/master/browser/browser.go#L346

Eon1 commented 7 years ago

This helped me out thanks .