Best Rod code snippet using rod.WaitWritable
element.go
Source:element.go
...215 err = el.WaitEnabled()216 if err != nil {217 return err218 }219 err = el.WaitWritable()220 if err != nil {221 return err222 }223 err = el.page.InsertText(text)224 _, _ = el.Evaluate(evalHelper(js.InputEvent).ByUser())225 return err226}227// InputTime èç¦è¯¥å
ç´ åå
¶è¾å
¥æ¶é´ã228// å¨æ§è¡æä½ä¹åï¼å®å°æ»å¨å°å
ç´ ï¼çå¾
å
¶å¯è§ãå¯ç¨åå¯åã229// å®å°çå¾
å
ç´ å¯è§ãå¯ç¨åå¯åã230func (el *Element) InputTime(t time.Time) error {231 err := el.Focus()232 if err != nil {233 return err234 }235 err = el.WaitEnabled()236 if err != nil {237 return err238 }239 err = el.WaitWritable()240 if err != nil {241 return err242 }243 defer el.tryTrace(TraceTypeInput, "input "+t.String())()244 _, err = el.Evaluate(evalHelper(js.InputTime, t.UnixNano()/1e6).ByUser())245 return err246}247// Blur 类似äºæ¹æ³ Blur248func (el *Element) Blur() error {249 _, err := el.Evaluate(Eval("() => this.blur()").ByUser())250 return err251}252// Select éæ©ä¸éæ©å¨å¹é
çåé项å
ç´ ã253// å¨æä½ä¹åï¼å®å°æ»å¨å°å
ç´ ï¼çå¾
å®å¯è§ã254// å¦æ没æä¸éæ©å¨å¹é
çé项ï¼å®å°è¿åErrElementNotFoundã255func (el *Element) Select(selectors []string, selected bool, t SelectorType) error {256 err := el.Focus()257 if err != nil {258 return err259 }260 defer el.tryTrace(TraceTypeInput, fmt.Sprintf(`select "%s"`, strings.Join(selectors, "; ")))()261 el.page.browser.trySlowmotion()262 res, err := el.Evaluate(evalHelper(js.Select, selectors, selected, t).ByUser())263 if err != nil {264 return err265 }266 if !res.Value.Bool() {267 return &ErrElementNotFound{}268 }269 return nil270}271// Matches æ£æ¥csséæ©å¨æ¯å¦å¯ä»¥éæ©å
ç´ 272func (el *Element) Matches(selector string) (bool, error) {273 res, err := el.Eval(`s => this.matches(s)`, selector)274 if err != nil {275 return false, err276 }277 return res.Value.Bool(), nil278}279// Attribute DOM对象çå±æ§280// Attribute vs Property: https://stackoverflow.com/questions/6003819/what-is-the-difference-between-properties-and-attributes-in-html281func (el *Element) Attribute(name string) (*string, error) {282 attr, err := el.Eval("(n) => this.getAttribute(n)", name)283 if err != nil {284 return nil, err285 }286 if attr.Value.Nil() {287 return nil, nil288 }289 s := attr.Value.Str()290 return &s, nil291}292// Property DOM对象çå±æ§293// Property vs Attribute: https://stackoverflow.com/questions/6003819/what-is-the-difference-between-properties-and-attributes-in-html294func (el *Element) Property(name string) (gson.JSON, error) {295 prop, err := el.Eval("(n) => this[n]", name)296 if err != nil {297 return gson.New(nil), err298 }299 return prop.Value, nil300}301// SetFiles 设置å½åæ件è¾å
¥å
ç´ çæ件302func (el *Element) SetFiles(paths []string) error {303 absPaths := []string{}304 for _, p := range paths {305 absPath, err := filepath.Abs(p)306 utils.E(err)307 absPaths = append(absPaths, absPath)308 }309 defer el.tryTrace(TraceTypeInput, fmt.Sprintf("set files: %v", absPaths))()310 el.page.browser.trySlowmotion()311 err := proto.DOMSetFileInputFiles{312 Files: absPaths,313 ObjectID: el.id(),314 }.Call(el)315 return err316}317// Describe æè¿°å½åå
ç´ ã深度æ¯åºæ£ç´¢å级çæ大深度ï¼é»è®¤ä¸º1ï¼å¯¹æ´ä¸ªåæ 使ç¨-1ï¼ææä¾å¤§äº0çæ´æ°ã318// pierceå³å®å¨è¿ååæ æ¶æ¯å¦è¦éåiframesåå½±åæ ¹ã319// è¿åçproto.DOMNodeãNodeIDå°å§ç»ä¸ºç©ºï¼å 为NodeIDä¸ç¨³å®ï¼å½proto.DOMDocumentUpdated被触åæ¶ï¼320// 页é¢ä¸çææNodeIDé½å°è¢«éæ°åé
å°å¦ä¸ä¸ªå¼ï¼ãæ们ä¸å»ºè®®ä½¿ç¨NodeIDï¼èæ¯ä½¿ç¨BackendNodeIDæ¥æ è¯å
ç´ ã321func (el *Element) Describe(depth int, pierce bool) (*proto.DOMNode, error) {322 val, err := proto.DOMDescribeNode{ObjectID: el.id(), Depth: gson.Int(depth), Pierce: pierce}.Call(el)323 if err != nil {324 return nil, err325 }326 return val.Node, nil327}328// ShadowRoot ShadowRootè¿åæ¤å
ç´ çå½±åæ ¹329func (el *Element) ShadowRoot() (*Element, error) {330 node, err := el.Describe(1, false)331 if err != nil {332 return nil, err333 }334 // è½ç¶ç°å¨å®æ¯ä¸ä¸ªæ°ç»ï¼ä½w3cå°å
¶è§èæ´æ¹ä¸ºå个æ°ç»ã335 id := node.ShadowRoots[0].BackendNodeID336 shadowNode, err := proto.DOMResolveNode{BackendNodeID: id}.Call(el)337 if err != nil {338 return nil, err339 }340 return el.page.ElementFromObject(shadowNode.Object)341}342// Frame å建ä¸ä¸ªè¡¨ç¤ºiframeç页é¢å®ä¾343func (el *Element) Frame() (*Page, error) {344 node, err := el.Describe(1, false)345 if err != nil {346 return nil, err347 }348 clone := *el.page349 clone.FrameID = node.FrameID350 clone.jsCtxID = new(proto.RuntimeRemoteObjectID)351 clone.element = el352 clone.sleeper = el.sleeper353 return &clone, nil354}355// ContainesElement æ£æ¥ç®æ æ¯å¦æ¯æå¨å
ç´ å
ã356func (el *Element) ContainsElement(target *Element) (bool, error) {357 res, err := el.Evaluate(evalHelper(js.ContainsElement, target.Object))358 if err != nil {359 return false, err360 }361 return res.Value.Bool(), nil362}363// Text å
ç´ æ¾ç¤ºçææ¬364func (el *Element) Text() (string, error) {365 str, err := el.Evaluate(evalHelper(js.Text))366 if err != nil {367 return "", err368 }369 return str.Value.String(), nil370}371// HTML å
ç´ çHTML372func (el *Element) HTML() (string, error) {373 res, err := proto.DOMGetOuterHTML{ObjectID: el.Object.ObjectID}.Call(el)374 if err != nil {375 return "", err376 }377 return res.OuterHTML, nil378}379// Visible å¦æå
ç´ å¨é¡µé¢ä¸å¯è§ï¼åè¿åtrue380func (el *Element) Visible() (bool, error) {381 res, err := el.Evaluate(evalHelper(js.Visible))382 if err != nil {383 return false, err384 }385 return res.Value.Bool(), nil386}387// WaitLoad 类似äºï¼imgï¼å
ç´ ççå¾
å è½½388func (el *Element) WaitLoad() error {389 defer el.tryTrace(TraceTypeWait, "load")()390 _, err := el.Evaluate(evalHelper(js.WaitLoad).ByPromise())391 return err392}393// WaitStable çå¾
ç´å°å¨dæç»æ¶é´å
没æå½¢ç¶æä½ç½®ååã394// å°å¿ï¼dä¸æ¯æ大çå¾
è¶
æ¶ï¼å®æ¯æä¸ç¨³å®çæ¶é´ã395// å¦æè¦è®¾ç½®è¶
æ¶ï¼å¯ä»¥ä½¿ç¨âElement.timeoutâå½æ°ã396func (el *Element) WaitStable(d time.Duration) error {397 err := el.WaitVisible()398 if err != nil {399 return err400 }401 defer el.tryTrace(TraceTypeWait, "stable")()402 shape, err := el.Shape()403 if err != nil {404 return err405 }406 t := time.NewTicker(d)407 defer t.Stop()408 for {409 select {410 case <-t.C:411 case <-el.ctx.Done():412 return el.ctx.Err()413 }414 current, err := el.Shape()415 if err != nil {416 return err417 }418 if reflect.DeepEqual(shape, current) {419 break420 }421 shape = current422 }423 return nil424}425// WaitStableRAF çå¾
ç´å°è¿ç»ä¸¤ä¸ªå¨ç»å¸§çå½¢ç¶æä½ç½®æ²¡æååã426// å¦æè¦çå¾
ç±JSèä¸æ¯CSS触åçå¨ç»ï¼æ好使ç¨Element.WaitStableã427// å
³äº animation frame: https://developer.mozilla.org/en-US/docs/Web/API/window/requestAnimationFrame428func (el *Element) WaitStableRAF() error {429 err := el.WaitVisible()430 if err != nil {431 return err432 }433 defer el.tryTrace(TraceTypeWait, "stable RAF")()434 var shape *proto.DOMGetContentQuadsResult435 for {436 err = el.page.WaitRepaint()437 if err != nil {438 return err439 }440 current, err := el.Shape()441 if err != nil {442 return err443 }444 if reflect.DeepEqual(shape, current) {445 break446 }447 shape = current448 }449 return nil450}451// WaitInteractable çå¾
å
ç´ å¯äº¤äºã452// å®å°å¨æ¯æ¬¡å°è¯æ¶å°è¯æ»å¨å°å
ç´ ã453func (el *Element) WaitInteractable() (pt *proto.Point, err error) {454 defer el.tryTrace(TraceTypeWait, "interactable")()455 err = utils.Retry(el.ctx, el.sleeper(), func() (bool, error) {456 // 对äºå»¶è¿å 载页é¢ï¼å
ç´ å¯ä»¥å¨è§å£ä¹å¤ã457 // å¦ææ们ä¸æ»å¨å°å®ï¼å®å°æ°¸è¿ä¸å¯ç¨ã458 err := el.ScrollIntoView()459 if err != nil {460 return true, err461 }462 pt, err = el.Interactable()463 if errors.Is(err, &ErrCovered{}) {464 return false, nil465 }466 return true, err467 })468 return469}470// Wait çå¾
jsè¿åtrue471func (el *Element) Wait(opts *EvalOptions) error {472 return el.page.Context(el.ctx).Sleeper(el.sleeper).Wait(opts.This(el.Object))473}474// WaitVisible ç´å°å
ç´ å¯è§475func (el *Element) WaitVisible() error {476 defer el.tryTrace(TraceTypeWait, "visible")()477 return el.Wait(evalHelper(js.Visible))478}479// WaitEnabled ç´å°è¯¥å
ç´ æªè¢«ç¦ç¨ã480// Doc for readonly: https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/readonly481func (el *Element) WaitEnabled() error {482 defer el.tryTrace(TraceTypeWait, "enabled")()483 return el.Wait(Eval(`() => !this.disabled`))484}485// WaitWritable ç´å°è¯¥å
ç´ ä¸æ¯åªè¯»çã486// Doc for disabled: https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/disabled487func (el *Element) WaitWritable() error {488 defer el.tryTrace(TraceTypeWait, "writable")()489 return el.Wait(Eval(`() => !this.readonly`))490}491// WaitInvisible ç´å°å
件ä¸å¯è§492func (el *Element) WaitInvisible() error {493 defer el.tryTrace(TraceTypeWait, "invisible")()494 return el.Wait(evalHelper(js.Invisible))495}496// CanvastoiImage è·åç»å¸çå¾åæ°æ®ã497// é»è®¤æ ¼å¼ä¸ºimage/pngã498// é»è®¤è´¨é为0.92ã499// doc: https://developer.mozilla.org/en-US/docs/Web/API/HTMLCanvasElement/toDataURL500func (el *Element) CanvasToImage(format string, quality float64) ([]byte, error) {501 res, err := el.Eval(`(format, quality) => this.toDataURL(format, quality)`, format, quality)...
WaitWritable
Using AI Code Generation
1import (2func main() {3 l := launcher.New().Headless(false)4 defer l.Cleanup()5 url := l.MustLaunch()6 fmt.Println("URL: ", url)7 browser := rod.New().ControlURL(url).MustConnect()8 page.MustWaitLoad().MustWaitWritable()9 time.Sleep(10 * time.Second)10}
WaitWritable
Using AI Code Generation
1import (2func main() {3 browser := rod.New().ControlURL(launcher.New().MustLaunch()).MustConnect()4 page := browser.MustPage("")5 searchBox := page.MustWaitVisible("#tsf > div:nth-child(2) > div > div.RNNXgb > div > div.a4bIc > input", 10)6 searchBox.MustWaitWritable(10)7 searchBox.MustInput("Hello World")8 inputValue, err := page.Eval(`document.querySelector("#tsf > div:nth-child(2) > div > div.RNNXgb > div > div.a4bIc > input").value`, nil)9 if err != nil {10 panic(err)11 }12 fmt.Println("Input value is: ", inputValue)13 page.MustElement("#tsf > div:nth-child(2) > div > div.FPdoLc.VlcLAe > center > input.gNO89b").MustClick()14 page.MustWaitLoad()15 title, err := page.Eval(`document.title`, nil)16 if err != nil {17 panic(err)18 }19 fmt.Println("Page title is: ", title)20 url, err := page.Eval(`document.URL`, nil)21 if err != nil {22 panic(err)23 }24 fmt.Println("Page URL is: ", url)25 html, err := page.Eval(`document.body.innerHTML`, nil)26 if err != nil {27 panic(err)28 }29 fmt.Println("Page HTML is: ", html)30 cookies, err := page.Eval(`document.cookie`, nil)31 if err != nil {32 panic(err)33 }34 fmt.Println("Page cookies are: ", cookies)
WaitWritable
Using AI Code Generation
1import (2func main() {3 browser := rod.New().ControlURL(launcher.New().MustLaunch()).MustConnect()4 page.MustWaitLoad()5 page.MustWaitWritable()6 time.Sleep(5 * time.Second)7 browser.MustClose()8 fmt.Println("Done")9}
WaitWritable
Using AI Code Generation
1import (2func main() {3 browser := rod.New().Connect()4 page.WaitLoad()5 page.Eval(`document.querySelector("input").value = "rod"`)6 page.WaitRequestIdle().Wait(2 * time.Second)7 page.Element("input[name=btnK]").WaitWritable().Click()8}
WaitWritable
Using AI Code Generation
1import (2func main() {3 browser := rod.New().Connect()4 page.WaitReady()5 page.WaitWritable()6 fmt.Println(page.Title())7}8import (9func main() {10 browser := rod.New().Connect()11 page.WaitReady()12 page.WaitStable()13 fmt.Println(page.Title())14}15import (16func main() {17 browser := rod.New().Connect()18 page.WaitLoad()19 fmt.Println(page.Title())20}21import (22func main() {23 browser := rod.New().Connect()
WaitWritable
Using AI Code Generation
1import (2func main() {3 browser := rod.New().Connect()4 page.WaitLoad()5 page.Element(".gNO89b").WaitClickable()6 page.Element(".gNO89b").Click()7 page.WaitLoad()8 page.Element("#result-stats").WaitVisible()9 fmt.Println(page.Element("#result-stats").Text())10 browser.Close()11}12About 1,20,00,00,000 results (0.78 seconds)
WaitWritable
Using AI Code Generation
1import (2func main() {3 l := launcher.New().Headless(false)4 defer l.Cleanup()5 browser := rod.New().ControlURL(l).MustConnect()6 defer browser.Close()7 page := browser.MustPage("")8 defer page.Close()9 if err != nil {10 fmt.Println(err)11 }12 err = page.WaitWritable().Error()13 if err != nil {14 fmt.Println(err)15 }16 time.Sleep(10 * time.Second)17}18import (19func main() {20 l := launcher.New().Headless(false)21 defer l.Cleanup()22 browser := rod.New().ControlURL(l).MustConnect()23 defer browser.Close()24 page := browser.MustPage("")25 defer page.Close()26 if err != nil {27 fmt.Println(err)28 }29 err = page.WaitStable().Error()30 if err != nil {31 fmt.Println(err)32 }33 time.Sleep(10 * time.Second)34}35import (36func main() {37 l := launcher.New().Headless(false)38 defer l.Cleanup()39 browser := rod.New().ControlURL(l).Must
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!