Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support ByQuery and ByQueryAll on specified nodes #463

Closed
xs23933 opened this issue Aug 27, 2019 · 5 comments · Fixed by #622
Closed

support ByQuery and ByQueryAll on specified nodes #463

xs23933 opened this issue Aug 27, 2019 · 5 comments · Fixed by #622

Comments

@xs23933
Copy link

xs23933 commented Aug 27, 2019

like puppeteer

	let li = await this.page.$$(`.s-result-item`)
	for (var y = 0; y < li.length; y++) {
		let itemAsin = await this.page.evaluate(element => element.getAttribute('data-asin'), li[y])
		if(itemDATA == posData) {// hit data
			hit = true;
			await li[y].$eval('a.s-links', e => e.click())
			return
		}
	}

maybe like

let eles = document.querySelector('.s-result-item')

eles[0].querySelector('a.s-links')

this is my code

chromedp.Run(c.ctx,
			chromedp.WaitVisible(`.a-pagination`, chromedp.ByQuery),
			chromedp.Nodes(`.s-result-item`, &nodes, chromedp.ByQueryAll),
		)
		for _, item := range nodes {
			cur := item.AttributeValue("data-id")
			if cur == searchID {
				fmt.Printf("hit %s = %s\n", cur, searchID)
				chromedp.Run(c.ctx, chromedp.Nodes(item.PartialXPathByID()+` //h2/a[@class="a-text-normal"]`, chromedp.BySearch)) // invalid 
	
				c.Sleep(2 * time.Second)
				return nil
			}
		}
@mvdan mvdan added the question label Aug 28, 2019
@JILeXanDR
Copy link

Hey! I'm also looking for an example of searching nested nodes.

@Ajod
Copy link

Ajod commented Nov 5, 2019

I also am interested in this if anyone has an idea :)

@mvdan
Copy link
Contributor

mvdan commented Nov 27, 2019

What about something like Nodes(".firstquery .secondquery", &nodes, ByQueryAll)? See .class1 .class2 in https://www.w3schools.com/cssref/css_selectors.asp, for example.

@JILeXanDR
Copy link

JILeXanDR commented Nov 27, 2019

@mvdan I guess the main idea is to use the already stored Node in the variable.

@mvdan
Copy link
Contributor

mvdan commented Nov 27, 2019

I see; I assume that you mean querying for nodes within a specific *cdp.Node, i.e. a subtree of the DOM. Right now that's not possible with Query, as the starting node is hard-coded to be the root node of the top-level frame.

The Chrome devtools protocol definitely supports this, so it's a limitation of our API. Perhaps there's a way we could work it in; we'd need to think about it. In the meantime, you could use https://godoc.org/github.com/chromedp/cdproto/dom#QuerySelectorAll directly, which is what ByQueryAll uses underneath. You can feed it whatever cdp.Node.NodeID you want as a starting point.

@mvdan mvdan changed the title How to find children element or node support ByQuery and ByQueryAll on specified nodes Nov 27, 2019
mvdan added a commit that referenced this issue May 15, 2020
Called FromNode, it means that we can run a query rooted at a specific
node, instead of the default which is the root node of the document.

Fixes #463.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants