亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

任何等待一些javascript代碼的waitForJs函數返回true

任何等待一些javascript代碼的waitForJs函數返回true

Go
守候你守候我 2021-09-10 21:00:23
這是關于golang 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不存在。
查看完整描述

1 回答

?
慕的地10843

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)


查看完整回答
反對 回復 2021-09-10
  • 1 回答
  • 0 關注
  • 360 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號