使用JSE(Selenium c#)设置值和文本的区别

The difference between setting the value and the text using JSE (Selenium c#)

我正在使用 IjavaScriptExecutor 来设置属性 value。 但有时会发生我的文本框包含我设置的值,但它没有将其显示为文本。因此,从字面上看,对于某些文本框,它会正常发送输入,而对其中一些则不会。它只是设置值。但不显示文本。 为什么会这样?

代码:

 public static void SendKeysJavaScript(IWebElement element, string message)
    {
        //Instance of IJavaScriptExecutor class
        var js = (IJavaScriptExecutor) Program.Driver;
        //Local variable holding formated string
        var stringFormat = string.Format("arguments[0].setAttribute('value','{0}')", message);
        //Send "message" to the "element"
        js.ExecuteScript(stringFormat, element);
        //confirm that input has been sent
        GetSentInput(element, message);
    }

同时,我注意到文本框即使在清除后仍保留值。这可能是我无法将输入发送到同一个文本框两次的原因之一,重置值的方法是什么?

C# 的 .Text 属性 return 是 预期的 return 值大致是纯文本浏览器的内部文本显示。。参见 this

另一方面,value 是这里的属性,至少是您使用的方式。因此,唯一的操作方法是使用 JavaScript Executor。

现在,深入研究问题,如果我理解正确,clear() 方法不会触发应用程序预期的事件更改,因此您看不到任何更改。要触发更改,有多个选项,包括我从 Selenium Java 源代码中获取的选项。

/** * If this element is a text entry element, this will clear the value. Has no effect on other * elements. Text entry elements are INPUT and TEXTAREA elements. * * Note that the events fired by this event may not be as you'd expect. In particular, we don't * fire any keyboard or mouse events. If you want to ensure keyboard events are fired, consider * using something like {@link #sendKeys(CharSequence...)} with the backspace key. To ensure * you get a change event, consider following with a call to {@link #sendKeys(CharSequence...)} * with the tab key. */

void clear();