Created
September 13, 2017 08:21
-
-
Save yogesh-desai/cfb484cd0349fe18cb8e048f38616a6c to your computer and use it in GitHub Desktop.
Chromdp to use iframes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"context" | |
"io/ioutil" | |
"log" | |
"time" | |
"fmt" | |
cdp "github.com/knq/chromedp" | |
) | |
func main() { | |
var err error | |
// var js string | |
/* js = `function { | |
var iframe = document.getElementById("webyclip-widget-3"); | |
var innerDoc = iframe.contentDocument || iframe.contentWindow.document; | |
document.getElementById("demo").innerHTML = innerDoc.body.innerHTML; | |
}`;*/ | |
// create context | |
ctxt, cancel := context.WithCancel(context.Background()) | |
defer cancel() | |
// create chrome instance | |
c, err := cdp.New(ctxt, cdp.WithLog(log.Printf)) | |
if err != nil { | |
log.Fatal(err) | |
} | |
// run task list | |
url := "https://www.tokopedia.com/chocoapple/ready-stock-bnib-iphone-128gb-7-plus-jet-black-garansi-apple-1-tahun-10?src=topads"; | |
var buf []byte | |
m := make(map[string]string) | |
var html string | |
// err = c.Run(ctxt, screenshot(url, `#sliders_container`, &buf, &m)) | |
err = c.Run(ctxt, screenshot(url, `#webyclip-widget-3`, &buf, &html, &m)) | |
if err != nil { | |
log.Fatal(err) | |
} | |
// shutdown chrome | |
err = c.Shutdown(ctxt) | |
if err != nil { | |
log.Fatal(err) | |
} | |
// wait for chrome to finish | |
err = c.Wait() | |
if err != nil { | |
log.Fatal(err) | |
} | |
err = ioutil.WriteFile("/home/yogesh/GoCodes/WebCrawler/webyclip.png", buf, 0644) | |
if err != nil { | |
log.Fatal(err) | |
} | |
fmt.Println("\n\nThe outerHTML is: ", html) | |
fmt.Println("\n\nThe Attributes are: ", m) | |
// fmt.Println("\nThe Object window keys: ", &buf) | |
} | |
//func screenshot(urlstr, sel string, res *[]byte, m *map[string]string) cdp.Tasks { | |
func screenshot(urlstr, sel string, res *[]byte, html *string, m *map[string]string) cdp.Tasks { | |
return cdp.Tasks{ | |
cdp.Navigate(urlstr), | |
cdp.Sleep(5 * time.Second), | |
cdp.WaitVisible(sel, cdp.ByID), | |
//cdp.Screenshot(sel, res, cdp.NodeVisible, cdp.ByID), | |
// cdp.CaptureScreenshot(res), | |
// cdp.InnerHTML(sel, html, cdp.ByID), | |
// cdp.Evaluate(js, res, cdp.ByID), | |
// cdp.Evaluate(`Object.keys(window);`, res), | |
cdp.OuterHTML(sel, html, cdp.ByID), | |
cdp.Attributes(sel, m, cdp.ByID), | |
// cdp.Focus(sel,cdp.ByID), | |
cdp.CaptureScreenshot(res), | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment