1 回答

TA貢獻1785條經驗 獲得超8個贊
我在與 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)
}
}
有兩個選項可以定義elementCondition. 您使用 Javascript 的方法看起來可以與webdriver.go 中ExecuteScript記錄的函數一起使用
// 將一段 JavaScript 代碼注入頁面,以便在當前選定框架的上下文中執行。假設執行的腳本是同步的,并且評估腳本的結果將返回給客戶端。
另一種方法是通過 Selenium 訪問元素屬性
func ButtonIsRed(WebElement e) (bool) {
return (e.GetCssProperty('color') == 'red')
}
所以你的代碼會變成
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 關注
- 360 瀏覽
添加回答
舉報