1 回答

TA貢獻(xiàn)1785條經(jīng)驗(yàn) 獲得超8個(gè)贊
我在與 Selenium 的 golang 綁定中看不到任何等待函數(shù),因此您很可能需要定義自己的等待函數(shù)。這是我第一次嘗試 golang,所以請(qǐng)耐心等待:
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)
}
}
有兩個(gè)選項(xiàng)可以定義elementCondition. 您使用 Javascript 的方法看起來(lái)可以與webdriver.go 中ExecuteScript記錄的函數(shù)一起使用
// 將一段 JavaScript 代碼注入頁(yè)面,以便在當(dāng)前選定框架的上下文中執(zhí)行。假設(shè)執(zhí)行的腳本是同步的,并且評(píng)估腳本的結(jié)果將返回給客戶端。
另一種方法是通過(guò) Selenium 訪問(wèn)元素屬性
func ButtonIsRed(WebElement e) (bool) {
return (e.GetCssProperty('color') == 'red')
}
所以你的代碼會(huì)變成
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)
- 1 回答
- 0 關(guān)注
- 351 瀏覽
添加回答
舉報(bào)