簡體   English   中英

python selenium 僅當 function 可用時如何執行_script

[英]python selenium how to execute_script only when function is available

我想做driver.execute_script('getDate();')

這個 function getDate()在網頁的 javascript 中定義。 如何確保首先加載了 function 定義? 否則我會收到一條錯誤消息, javascript error: no such function...

更新:我的用例是有一個 datepicker 元素在某個時候調用這個 function。 我想用這個 function 直接獲取/設置日期,而不是編寫代碼來查找日期選擇器元素,單擊日、月、年等,我覺得這會很復雜

您可以嘗試/捕獲錯誤或使用以下內容擴展您執行的 JavaScript:

//Call the function if it exists.
if (typeof getDate === "function") {
  getDate();
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM