Comments (6)
Could you provide more information / context on what you're trying to do? Considering that most of the node based actions are built similarly to that, you're probably just missing a small step in how you're executing the action.
from chromedp.
I'm trying to add the new Action to the tasks to be executed.
tasks := chromedp.Tasks{
Newaction(),
}
from chromedp.
Ok. You need to execute those tasks against a running Chrome instance. If you could provide a more complete code sample, I'd be better able to help you figure out whatever the problem(s) is(are).
from chromedp.
I actually got it working after toying with it a little more. I had an option set for ByID which was bugging it out! The above code should work and did.
This is what I was going to submit, which did end up working and found my bug.
package main
import (
"context"
"fmt"
"log"
"github.com/knq/chromedp"
cdp "github.com/knq/chromedp/cdp"
)
func main() {
c, err := chromedp.New(context.Background())
if err != nil {
log.Fatal(err)
}
if err := c.Run(context.Background(), Tasks()); err != nil {
log.Fatal(err)
}
}
func Tasks() chromedp.Tasks {
return chromedp.Tasks{
chromedp.Navigate(`https://www.google.com`),
chromedp.WaitVisible("#idb", chromedp.ByID),
NewAction(),
}
}
func NewAction(opts ...chromedp.QueryOption) chromedp.Action {
return chromedp.QueryAfter("head", func(ctxt context.Context, h cdp.Handler, nodes ...*cdp.Node) error {
log.Println("something after happeed")
if len(nodes) == 0 {
return fmt.Errorf("not found...")
}
return fmt.Errorf("found")
}, opts...)
}
from chromedp.
I'm not sure if I should ope another ticket. I want to make this action daisy chain and do other actions based on it being successful. If page has ... then click ... else ignore.
from chromedp.
Please take a look in sel.go
to see the various By*
implementations. To use them properly, you need to have a good handle on how their implementations map to the low-level Chrome Debugging Protocol, and in turn how the browser is executing those selector queries. ByID
is just a short hand for ByQuery("#"+id)
, which may / may not be what you actually want to use.
In terms of providing more complex "logic" actions, I've purposely decided not to, because I don't want to end up recreating something like a "DSL" as it seems redundant to me to do so, when you have the flexibility and power of Go at your disposal.
If you need to execute complex logic chains, then the way we are using chromedp
on our end looks something like this:
res, err := doChain1(c)
if err != nil { /* ... */ }
if res == expected {
err := doChain1SubA(c)
if err != nil { /* ... */ }
} else {
err := doChain1SubB(c)
if err != nill { /* ... */ }
}
I realize this may seem strange, but if you wanted to write your own "If/Then" action, you could easily implement it by satisfying the Action
interface:
// If executes a, and if a succeeded without error, executes b, otherwise
// executes c.
func If(a, b, c chromedp.Action) chromedp.Action {
return chromedp.ActionFunc(func(ctxt context.Context, h cdp.Handler) error {
err := a.Do(ctxt, h)
if err == nil {
return b.Do(ctxt, h)
}
return c.Do(ctxt, h)
})
}
I think defining your own simple action helpers like the above is fine, however I do not think actions like these belong in the high level chromedp
package. Possibly in the future we might create a separate chromedp/chain
that provides "something" like a bunch of helper func's to wrap complex logic into a single action.
However, I'd like to discourage use of the above, because you don't know what failed! Was it the logic chain A, B, or C that failed? If so, which "sub" chain failed? The Action
interface can't really change, because it needs to be composable.
Please note the obvious logic problems in the example If
above. Please don't use this! It's a horrible way to handle flow control, and if you want to write logic like that, you just end up with gulp
style logic chains. This is not the "Go way".
The "right" way is to wrap all your logic into a single "action" which in turn calls sub actions, and propagates a single failure to the top, and having the action itself modify / store results as the chain progresses.
from chromedp.
Related Issues (20)
- HTTP response status: 404, From chromedp/headless-shell - /devtools/browser/
- SendKeys sends double line break with \n
- Data race using wg.wait to ListenTarget
- Access page cannot be loaded HOT 2
- looking for more detail ahout drag and drop
- How to make multiple tasks run in different tabs in one browser?
- Google Chrome 120.0.6099.71 issue: chrome failed to start: [1211/110216.183901:ERROR:headless_shell.cc(216)] Headless commands are not compatible with remote debugging.
- webdriver BiDi? HOT 1
- No resource with given identifier found (-32000)
- The error occurred during use and cannot be replicated HOT 1
- Navigation failed to finish HOT 3
- chrome does not use cookies from existing profile directory
- Failed get the correct innerHtml using dom.GetOuterHTML() or chromedp.InnerHTML
- How to intercept and modify the js Request?
- Can't download the file to a expected dir. HOT 1
- plz! how to switch to iframe? how to select iframe?
- How to modify response in ajax
- panic: sync: negative WaitGroup counter
- [feature request] progress bar like in puppeteer-cluster
- chromedp.TextContent is used to retrieve the text content of a web page element. When the element is not present, the program cannot proceed and remains stuck at this point. HOT 2
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.