Giter Site home page Giter Site logo

Custom Task about chromedp HOT 6 CLOSED

chromedp avatar chromedp commented on May 14, 2024
Custom Task

from chromedp.

Comments (6)

kenshaw avatar kenshaw commented on May 14, 2024

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.

clanstyles avatar clanstyles commented on May 14, 2024

I'm trying to add the new Action to the tasks to be executed.

tasks := chromedp.Tasks{
    Newaction(),
}

from chromedp.

kenshaw avatar kenshaw commented on May 14, 2024

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.

clanstyles avatar clanstyles commented on May 14, 2024

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.

clanstyles avatar clanstyles commented on May 14, 2024

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.

kenshaw avatar kenshaw commented on May 14, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.