如何登录网页并截取屏幕截图



如何使用chromedp登录网页并截取屏幕截图。

https://github.com/chromedp/chromedp

将文本发送到网站输入的示例

func main() {
var err error
// create context
ctxt, cancel := context.WithCancel(context.Background())
defer cancel()
// create chrome instance
c, err := chromedp.New(ctxt, chromedp.WithLog(log.Printf))
if err != nil {
log.Fatal(err)
}
// run task list
var val1, val2, val3, val4 string
err = c.Run(ctxt, sendkeys(&val1, &val2, &val3, &val4))
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)
}
log.Printf("#input1 value: %s", val1)
log.Printf("#textarea1 value: %s", val2)
log.Printf("#input2 value: %s", val3)
log.Printf("#select1 value: %s", val4)
}
func sendkeys(val1, val2, val3, val4 *string) chromedp.Tasks {
return chromedp.Tasks{
chromedp.Navigate("file:" + os.Getenv("GOPATH") + 
"/src/github.com/chromedp/chromedp/testdata/visible.html"),
chromedp.WaitVisible(`#input1`, chromedp.ByID),
chromedp.WaitVisible(`#textarea1`, chromedp.ByID),
chromedp.SendKeys(`#textarea1`,
kb.End+"bbnnaoeunntest1nnblah2nntttbbother
box!tntest4", chromedp.ByID),
chromedp.Value(`#input1`, val1, chromedp.ByID),
chromedp.Value(`#textarea1`, val2, chromedp.ByID),
chromedp.SetValue(`#input2`, "test3", chromedp.ByID),
chromedp.Value(`#input2`, val3, chromedp.ByID),
chromedp.SendKeys(`#select1`, kb.ArrowDown+kb.ArrowDown,
chromedp.ByID),
chromedp.Value(`#select1`, val4, chromedp.ByID),
chromedp.Sleep(30 * time.Second),
}
}

之后,您可以单击登录按钮,另一个示例

// Command click is a chromedp example demonstrating how to use a selector to
// click on an element.
package main
import (
"context"
"log"
"time"
"github.com/chromedp/chromedp"
)
func main() {
var err error
// create context
ctxt, cancel := context.WithCancel(context.Background())
defer cancel()
// create chrome instance
c, err := chromedp.New(ctxt, chromedp.WithLog(log.Printf))
if err != nil {
log.Fatal(err)
}
// run task list
err = c.Run(ctxt, click())
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)
}
}
func click() chromedp.Tasks {
return chromedp.Tasks{
chromedp.Navigate(`https://golang.org/pkg/time/`),
chromedp.WaitVisible(`#footer`),
chromedp.Click(`#pkg-overview`, chromedp.NodeVisible),
chromedp.Sleep(150 * time.Second),
}
}

用于截屏

// Command screenshot is a chromedp example demonstrating how to take a
// screenshot of a specific element.
package main
import (
"context"
"io/ioutil"
"log"
"time"
"github.com/chromedp/chromedp"
)
func main() {
var err error
// create context
ctxt, cancel := context.WithCancel(context.Background())
defer cancel()
// create chrome instance
c, err := chromedp.New(ctxt, chromedp.WithLog(log.Printf))
if err != nil {
log.Fatal(err)
}
// run task list
var buf []byte
err = c.Run(ctxt, screenshot(`https://brank.as/`, `#contact-form`, &buf))
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("contact-form.png", buf, 0644)
if err != nil {
log.Fatal(err)
}
}
func screenshot(urlstr, sel string, res *[]byte) chromedp.Tasks {
return chromedp.Tasks{
chromedp.Navigate(urlstr),
chromedp.Sleep(2 * time.Second),
chromedp.WaitVisible(sel, chromedp.ByID),
chromedp.WaitNotVisible(`div.v-middle > div.la-ball-clip-rotate`, chromedp.ByQuery),
chromedp.Screenshot(sel, res, chromedp.NodeVisible, chromedp.ByID),
}
}

如果您想要更详细的答案,则需要发布网站,希望对您有所帮助

相关内容

  • 没有找到相关文章