Comments (1)
following code works according to former issue:
package main
import (
"context"
"fmt"
"github.com/chromedp/cdproto/cdp"
"log"
"os"
"path/filepath"
"reflect"
"sort"
"strings"
"sync"
"time"
"github.com/chromedp/cdproto/browser"
"github.com/chromedp/chromedp"
)
func main() {
ctx, cancel := chromedp.NewContext(
context.Background(),
chromedp.WithLogf(log.Printf),
)
defer cancel()
ctx, cancel = context.WithTimeout(ctx, 60*time.Second)
defer cancel()
done := make(chan string, 1)
lock := sync.Mutex{}
m := make(map[string]int)
chromedp.ListenBrowser(ctx, func(v interface{}) {
if ev, ok := v.(*browser.EventDownloadProgress); ok {
fmt.Println("ev.State", ev.State)
completed := "(unknown)"
if ev.TotalBytes != 0 {
completed = fmt.Sprintf("%0.2f%%", ev.ReceivedBytes/ev.TotalBytes*100.0)
}
log.Printf("state: %s, completed: %s\n", ev.State.String(), completed)
if ev.State == browser.DownloadProgressStateCompleted {
done <- ev.GUID
close(done)
}
}
typeString := reflect.TypeOf(v).String()
lock.Lock()
m[typeString] += 1
lock.Unlock()
})
// get working directory
wd, err := os.Getwd()
if err != nil {
log.Fatal(err)
}
if err := chromedp.Run(ctx,
chromedp.Navigate(`http://www.sse.com.cn/assortment/stock/list/share/`),
chromedp.ActionFunc(func(ctx context.Context) error {
c := chromedp.FromContext(ctx)
return browser.
SetDownloadBehavior(browser.SetDownloadBehaviorBehaviorAllowAndName).
WithDownloadPath(wd).
WithEventsEnabled(true).Do(cdp.WithExecutor(ctx, c.Browser))
}),
chromedp.Click(`body > div.container.sse_content > div > div.col-lg-9.col-xxl-10 > div > div.sse_colContent.js_stocksDepository > h2 > a`, chromedp.BySearch, chromedp.NodeVisible),
); err != nil && !strings.Contains(err.Error(), "net::ERR_ABORTED") {
log.Fatal(err)
}
time.Sleep(time.Second * 3)
var keySlice []string
for k, _ := range m {
keySlice = append(keySlice, k)
}
sort.Strings(keySlice)
for _, k := range keySlice {
fmt.Println(k, m[k])
}
guid := <-done
log.Printf("wrote %s", filepath.Join(wd, guid))
}
from chromedp.
Related Issues (20)
- when use chromedp.Evaluate, how can I get the promise error info ?
- Navigate Hangup with custom url scheme HOT 1
- page.StopLoading() cannot stop navigate
- chrome failed to start with no detail error
- Screenshot from remote browser
- context canceled even with new context HOT 1
- Download events being omitted on the page level but chromedp listens for it on the Browser level HOT 1
- Image not showing up in header
- Can't use proxy and open multiple tabs ?
- Target.targetCrashed > errorCode 11 with chromedp.Navigate() in Docker container environnement HOT 7
- Is it possible to use the net/http client in chromedp ? HOT 1
- GetOuterHTML().WithPierce(true) not returning <iframe> contents
- How to execute JavaScript in a specified context? HOT 1
- How to set the state of ShadowDOM from closed to open?
- Question: condition for set FooterTemplate
- Is it possible to capture error messages related to CORS, CSP violations, mixed-content violations, etc.?
- Can I Capture Raw HTTP Data?
- How to start chrome in arm environment?Are there any other plans?
- Why can't I listen to my iframe's network requests
- How i get dpi of chrome
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from chromedp.