selenium clear() command doesn't clear the element

前端 未结 8 1234
旧时难觅i
旧时难觅i 2021-01-11 21:14

I have been writing selenium scripts for a while in Java. I encountered a very weird issue today.

Here is the issue: I cleared a text field using webelement.clear()

8条回答
  •  臣服心动
    2021-01-11 21:54

    I had a similar issue with a text field that used an auto-complete plugin. I had to explicitly clear the attribute value as well as do a SendKeys. I created an extension method to encapsulate the behaviour, hopefully the snippet below will help:

    public static void SendKeysAutocomplete(this IWebElement element, string fieldValue)
    {
       element.SendKeys(fieldValue);
       element.SetAttribute("value", fieldValue);
    }
    
    public static void SetAttribute(this IWebElement element, string attributeName, string attributeValue)
    {
       var driver = WebDriverHelper.GetDriverFromScenarioContext();
    
       var executor = (IJavaScriptExecutor)driver;
       executor.ExecuteScript("arguments[0].setAttribute(arguments[1], arguments[2]);", element, attributeName, attributeValue);
    }
    

提交回复
热议问题