任何waitForJs函数都可以等待一些JavaScript代码返回true。

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

any waitForJs function to wait for some javascript code returns true

问题

这是关于golang selenium webdriver的问题。

是否有一个函数,在某个js代码返回true之后才返回。

var session *webdriver.Session
...
session.waitForJs(`$('#redButton').css('color')=='red'`)
// 只有在`#redButton`变成红色之后才执行下一段代码

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

英文:

This is question about golang selenium webdriver.

Is there any function that returns only after some js code return true.

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

The problem is that method session.waitForJs do not exist.

答案1

得分: 2

我在golang的Selenium绑定中没有看到任何等待函数,所以你很可能需要自己定义一个。这是我在golang中的第一次尝试,所以请谅解:

type elementCondition func(e WebElement) bool

// 在超时时间到期或元素条件为真时返回
func (e WebElement) WaitForCondition(fn elementCondition, timeOut int) {
    
    // 如果元素条件不为真,则循环
    for i := 0; !fn(e) && i < timeOut; i++ {
        time.Sleep(1000)
    }
}

有两种方法可以定义elementCondition。你使用JavaScript的方法看起来可以通过webdriver.go中记录的ExecuteScript函数来实现。

// 将一段JavaScript代码注入到当前选定的框架中执行。执行的脚本被假定为同步的,并且评估脚本的结果将返回给客户端。

另一种方法是通过Selenium访问元素属性:

func ButtonIsRed(e WebElement) bool {
    return (e.GetCssProperty("color") == "red")
}

所以你的代码将变成:

var session *webdriver.Session
....
// 使用CSS选择器定位按钮
var webElement := session.FindElement(CSS_Selector, "#redButton")
// 等待按钮变为红色
webElement.WaitForCondition(ButtonIsRed, 10)
英文:

I don't see any wait functions in the golang bindings to Selenium, so you'll most likely need to define your own. This is my first attempt at golang, so bear with me:

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) &amp;&amp; i &lt; timeOut; i++ {
	    time.sleep(1000)
    }
}

There are two options to define the elementCondition. Your method of using Javascript looks like it could work with the ExecuteScript function documented in webdriver.go

> // Inject a snippet of JavaScript into the page for execution in the
> context of the currently selected frame. The executed script is
> assumed to be synchronous and the result of evaluating the script is
> returned to the client.

An alternative is to access the element properties through Selenium

func ButtonIsRed(WebElement e) (bool) {
	return (e.GetCssProperty(&#39;color&#39;) == &#39;red&#39;)
}

So your code would become

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

huangapple
  • 本文由 发表于 2015年1月14日 09:42:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/27934554.html
匿名

发表评论

匿名网友

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

确定