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

Got some strange thing about methord Text() #1144

Closed
Longdexin opened this issue Nov 14, 2024 · 1 comment
Closed

Got some strange thing about methord Text() #1144

Longdexin opened this issue Nov 14, 2024 · 1 comment
Labels
question Questions related to rod

Comments

@Longdexin
Copy link

Longdexin commented Nov 14, 2024

Rod Version: v0.116.2

The code to demonstrate your question

Test code

package main

import (
   "log"
   "time"

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

func main() {
   u := launcher.New().
   	Headless(false).
   	NoSandbox(true).
   	Set("disable-web-security").
   	Set("disable-site-isolation-trials").
   	MustLaunch()

   page := rod.New().ControlURL(u).MustConnect().NoDefaultDevice().MustPage("https://www.cincinnatichildrens.org/search/research")
   defer func() {
   	_ = page.Close()
   }()
   timeout := 10 * time.Second
   el, err := page.Timeout(timeout).ElementX(`//div[@class='intro']/h2[contains(text(),'Browse by Category')]`)
   if err != nil {
   	log.Println(err)
   	return
   }
   // print <h2>Browse by Category (28)</h2>
   if html, err := el.Timeout(timeout).HTML(); err == nil {
   	log.Println(html)
   }
   <-time.After(5 * time.Second) // when I comment this line, the text is blank
   if text, err := el.Timeout(timeout).Text(); err == nil {
   	log.Printf(`text: %s`, text)
   } else {
   	log.Println(err)
   }
} 

go.mod

module test

go 1.23.3

require github.com/go-rod/rod v0.116.2

require (
	github.com/ysmood/fetchup v0.2.4 // indirect
	github.com/ysmood/goob v0.4.0 // indirect
	github.com/ysmood/got v0.40.0 // indirect
	github.com/ysmood/gson v0.7.3 // indirect
	github.com/ysmood/leakless v0.9.0 // indirect
)

What you got

When I commented the line '<-time.After(5 * time.Second) ', the text printed below was blank, because the text had not shown up on the page yet, but the html printed above already had the text.

What you expect to see

Since I have selected the element by ElementX() successfully, the element should have the correct text as expected no matter it has shown on the page or not.

What have you tried to solve the question

I know it is reasonable, but it seems to be so wild at first sight and has caused some problems to my code.
Hope someone can explain this and help me out, thanks a lot.

@Longdexin Longdexin added the question Questions related to rod label Nov 14, 2024
@ysmood
Copy link
Member

ysmood commented Nov 14, 2024

Element.Text will be affect by the render engine, it's upstream issue that rod can't solve, this code works fine to me:

package main

import (
	"fmt"

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

func main() {
	page := rod.New().MustConnect().NoDefaultDevice().MustPage("https://www.cincinnatichildrens.org/search/research")
	el := page.MustElementX(`//div[@class='intro']/h2[contains(text(),'Browse by Category')]`)
	fmt.Println(el.MustWaitVisible().MustText())
}

@ysmood ysmood closed this as completed Nov 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Questions related to rod
Projects
None yet
Development

No branches or pull requests

2 participants