選單
GSS 技術部落格
在這個園地裡我們將從技術、專案管理、客戶對談面和大家分享我們多年的經驗,希望大家不管是喜歡或是有意見,都可以回饋給我們,讓我們有機會和大家對話並一起成長!
若有任何問題請來信:gss_crm@gss.com.tw
1 分鐘閱讀時間 (248 個字)

測試隨筆-超好用的 Selenium 內建 Action Class

clean-macbook-pro-mockup-desk-scene

用 Action Class 解決了很多問題:


    • 問題:在 contenteditable = true 中使用 SendKeys 會有錯誤訊息:Cannot focus web element to send keys 。不需要用 JavaScript 的方式來處理,簡單很多。
      網路上蒐到使用 JavaScript 的解法比較多,但可以用 Action 簡單處理完成。
 原始 html 碼
<ul data-id="xxx" contenteditable="ture">
<li data-id="yyy:">
<li data-id="xxx:">
 driver.FindElement(By.XPath("locator")).SendKeys("xxx");
→ 會有錯誤訊息:Cannot focus web element to send keys
 IWebElement visibleInput = driver.FindElement(By.XPath(locator));
IJavaScriptExecutor executor = (IJavaScriptExecutor)driver;
executor.ExecuteScript("arguments[0].click();", visibleInput);
driver.FindElement(By.XPath(locator)).SendKeys("xxx");
→ 目前遇到是無法把字輸入成功的。
 IWebElement element = driver.FindElement(By.XPath(locator));
Actions action = new Actions(driver);
action.MoveToElement(element).Click().Perform();
action.MoveToElement(element).SendKeys("xxx").Perform();
→ 可以運作成功, 有 innerHTML 的也可以用 Action 簡單處理。
  • 問題:沒有 DoubleClick 可以選時Action 也支援 Mouse、Keyboard 事件。
 driver.FindElement(By.XPath(locator).Click();
這裡沒有 doubleClick method 可以選。
 action.MoveToElement(element).DoubleClick().Perform();
承上所述,直接使用 DoubleClick() method
  • 問題:IE11 的這種錯誤【error: Cannot click on element (WARNING: The server did not provide any stacktrace information) 】(相同腳本 Chrome 是可以跑的),改用 Action 簡單處理。(舉例同第一項)
  • 要引用 using OpenQA.Selenium.Interactions;
參考:
如何在.Net專案中開始使用TypeScript
無法載入檔案或組件 'log4net, Version=1.2.10.0, Culture=neut...
 

評論

尚無評論
已經注冊了? 這裡登入
Guest
2024/05/05, 週日

Captcha 圖像