去吧,罗德,按下按钮。

huangapple go评论92阅读模式
英文:

Go Rod If Button

问题

尝试确定页面上是否存在特定按钮,如果按钮不存在,该代码似乎会冻结而不是继续执行。有没有什么方法可以确定页面上是否存在按钮,而不会停止代码的运行? 去吧,罗德,按下按钮。

package main

import (
	"log"

	"github.com/go-rod/rod"
)

func main() {

	urlSlice := []string{
		"https://www.amd.com/en/direct-buy/5450881700/us",
		"https://www.amd.com/en/direct-buy/5458373400/us",
	}

	for {

		for _, url := range urlSlice {

			page := rod.New().MustConnect().MustPage(url)

			btn := page.MustElement("#product-details-info > div.container > div > div > div.product-page-description.col-flex-lg-5.col-flex-sm-12 > button")

			if btn.MustText() == " ADD TO CART" {

				log.Printf(
					"%s \t %s",
					page.MustInfo().Title,
					btn.MustText(),
				)

			}

		}
	}
}

这是要翻译的代码部分。

英文:

Trying to determine if a particular button exists on a page, if the button doesn't exist this code seems to freeze instead of continuing. Any ideas what options there might be to determine if the button exists or not on a page without the code stopping? 去吧,罗德,按下按钮。

package main

import (
	"log"

	"github.com/go-rod/rod"
)

func main() {

	urlSlice := []string{
		"https://www.amd.com/en/direct-buy/5450881700/us",
		"https://www.amd.com/en/direct-buy/5458373400/us",
	}

	for {

		for _, url := range urlSlice {

			page := rod.New().MustConnect().MustPage(url)

			btn := page.MustElement("#product-details-info > div.container > div > div > div.product-page-description.col-flex-lg-5.col-flex-sm-12 > button")

			if btn.MustText() == " ADD TO CART" {

				log.Printf(
					"%s \t %s",
					page.MustInfo().Title,
					btn.MustText(),
				)

			}

		}
	}
}

答案1

得分: 2

btn := page.Timeout(10 * time.Second).MustElement("#product-details-info > div.container > div > div > div.product-page-description.col-flex-lg-5.col-flex-sm-12 > button")

英文:
btn := page.Timeout(10 * time.Second).MustElement("#product-details-info > div.container > div > div > div.product-page-description.col-flex-lg-5.col-flex-sm-12 > button")

答案2

得分: 1

我知道这是一个旧帖子,但如果有人迷路到这里,这是我解决这类问题的方法:

我使用.MustElements(...)来获取所有的按钮,然后在一个循环中检查按钮的.MustText()值是否与我要查找的内容匹配。

buttons := page.MustElements("button")
for _, b := range buttons {
    t, err := b.Text()
    if err != nil {
        // 错误处理
    }
    if t == " ADD TO CART" {
        // 做你需要做的事情
        log.Printf(
            "%s \t %s",
            page.MustInfo().Title,
            btn.MustText(),
        )
        break
    }
}

当然,如果有一种明确的功能可以测试给定元素是否存在,那就更好了。

英文:

I know this is an old post, but if anyone wanders here, here's how I solved this kind of problem:

I used .MustElements(...) to get all the buttons and then in a loop I checked the buttons' .MustText() value if any of them is matching to what I'm looking for.

buttons := page.MustElements("button")
for _, b := range buttons {
	t, err := b.Text()
	if err != nil {
      // error handling
	}
	if t == " ADD TO CART" {
		// Do what you need to do 
        log.Printf(
                "%s \t %s",
                page.MustInfo().Title,
                btn.MustText(),
        )
		break
	}
}

Of course it would be good if there would be some kind of explicit functionality that tests if a given element exists or not.

huangapple
  • 本文由 发表于 2021年8月2日 08:57:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/68615422.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定