I implemented a utility that assists with logging me into a website and merely want to check that authentication was successful. While it logs me in okay, I want to automate some downstream actions if it is successful. For that, I want to check if elements are present on the page to determine if authentication was successful.
This is what I am attempting to do:
func Exists(ctx context, elementId string) bool {
var value string
log.Warn().Msgf("checking for visibility of: %s via %s", elementId, s)
err := chromedp.Run(ctx, text(&value, elementId))
logging.Panic(err)
return &value != nil
}
func text(value *string, elementId string) chromedp.Tasks {
return chromedp.Tasks{
//chromedp.WaitVisible(elementId, chromedp.ByID),
chromedp.EvaluateAsDevTools("document.getElementById('" + elementId + "').value", value),
}
}
The exception I get when I attempt to use this function is: "encountered an undefined value"
I am passing in an elementId. I had originally included wait visible, but was thinking that if that element does not exist, perhaps it will raise an exception so that is why that was commented out, but it should just wait until it is visible and if not, throw a timeout exception or context cancelled.
Why is it throwing the undefined value and if elementId is defined, then what is undefined?
This is what works for me. I thought there might be a more elegant way to do it, I think the Java Web Driver implementation is a bit cleaner.
In any case, this basically waits up to the specified amount of time to report whether or not an element exists.
func Exists(pctx context, d time.Duration, sel interface{}, opts ...chromedp.QueryOption) bool {
log.Warn().Msgf("checking for visibility of: %s via %s", sel, s)
ctx, cancel := context.WithTimeout(pctx, d)
defer cancel()
err := chromedp.Run(ctx, chromedp.Tasks{chromedp.WaitVisible(sel, opts...)})
return err == nil
}