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

标签 javascript html selenium selenium-chromedriver

我正在使用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/

相关文章:

javascript - 为什么控制台提供完整的 jsFiddle 链接?

javascript - 根据先前的选择选项禁用选择输入

javascript - HTML5 游戏通过按下按钮移动 Sprite

python - 使用 scrapy 遍历 selenium 中的链接

python - 等待+刷新页面

javascript - Internet Explorer 8 兼容模式下的 YouTube api 错误

javascript - 无法可视化百分比数字(得到 NaN)

python - 如何使用 Selenium-Python 从多选列表中选择多个选项?

javascript - Canvas 未正确显示

javascript - 函数未在 javascript 中定义。为什么?