我正在使用IjavaScriptExecutor来设置Atrribute值。 但有时我的文本框包含我设置的值,但它没有将其显示为文本。因此,从字面上看,它对某些文本框正常发送输入,而对其中一些文本框则不然。它只是设置值。但不显示文字。 为什么会出现这种情况?
代码:
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
属性返回内部文本,预期返回值大致与纯文本浏览器显示的内容相同。。请参阅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. */
voidclear();
关于javascript - 使用JSE(Selenium c#)设置值和文本的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32440076/