Selenium 任何等待某些javascript代码的waitForJs函数都会返回true

Selenium 任何等待某些javascript代码的waitForJs函数都会返回true,selenium,go,selenium-webdriver,Selenium,Go,Selenium Webdriver,这是一个问题 是否有任何函数仅在某些js代码返回true后才返回 var session *webdriver.Session ... session.waitForJs(`$('#redButton').css('color')=='red'`) // next code should be executed only after `#redButton` becomes red 问题是session.waitForJs方法不存在。我在Selenium的golang绑定中没有看到任何等待函数

这是一个问题

是否有任何函数仅在某些js代码返回true后才返回

var session *webdriver.Session
...
session.waitForJs(`$('#redButton').css('color')=='red'`)
// next code should be executed only after `#redButton` becomes red

问题是session.waitForJs方法不存在。

我在Selenium的golang绑定中没有看到任何等待函数,因此您很可能需要定义自己的等待函数。这是我第一次尝试golang,请耐心等待:

type elementCondition func(e WebElement) bool

// Function returns once timeout has expired or the element condition is true
func (e WebElement) WaitForCondition(fn elementCondition, int timeOut) {

    // Loop if the element condition is not true
    for i:= 0; !elementCondition(e) && i < timeOut; i++ {
        time.sleep(1000)
    }
}
因此,您的代码将成为

var session *webdriver.Session
....
// Locate the button with a css selector
var webElement := session.FindElement(CSS_Selector, '#redButton')
// Wait for the button to be red
webElement.WaitForCondition(ButtonIsRed, 10)
var session *webdriver.Session
....
// Locate the button with a css selector
var webElement := session.FindElement(CSS_Selector, '#redButton')
// Wait for the button to be red
webElement.WaitForCondition(ButtonIsRed, 10)