使用C#Selenium WebDriver将文本发送到具有onkeypress事件的文本框时遇到问题 [英] Having issues while sending text to a textbox which has onkeypress event using c# selenium webdriver

查看:167
本文介绍了使用C#Selenium WebDriver将文本发送到具有onkeypress事件的文本框时遇到问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有onkeypress事件的文本框,当将值输入到文本框时,该事件会执行页面渲染.在这里,我使用硒webdriver填充文本框.在填充文本框之前,我正在使用textbox.clear().因此,会调用onkeypress事件并呈现页面.因此,文本框控件已从webdriver实例中删除.

I am having a textbox which has onkeypress event which performs a page render when the value is entered to textbox. Here I am using selenium webdriver to fill the textbox. Before filling the textbox I am using textbox.clear(). So the onkeypress event get called and page gets rendered. So the textbox control is removed from webdriver instance.

onkeypress事件发生后,该元素未在网络驱动程序中列出. 这是特定文本框的源标签:

After the onkeypress event taking place, the element is not getting listed in the webdriver. This is the source tag of the particular textbox:

<input id="ctl00_ctl00_ContentPlaceHolder1_cphMainContent_wzrDREvent_txtNftnTime" class="OpCenter_DateBox" type="text" onkeypress="if (WebForm_TextBoxKeyHandler(event) == false) return false;" onchange="javascript:setTimeout('__doPostBack(\'ctl00$ctl00$ContentPlaceHolder1$cphMainContent$wzrDREvent$txtNftnTime\',\'\')', 0)" name="ctl00$ctl00$ContentPlaceHolder1$cphMainContent$wzrDREvent$txtNftnTime">

您可以看到此处使用的onkeypress事件. 请提出一些有关在文本框中填充值的想法.

And you can see the onkeypress event used here. Please suggest some ideas for filling the values in textbox.

我正在使用以下代码填充我的文本框:

I am using the following code to fill my text box:

element.Clear();
element.SendKeys(value);

谢谢.

推荐答案

我使用以下JavaScript代码来克服此问题.

I have used the following JavaScript code to overcome this issue.

if (!(webDriver.GetType().FullName).Contains("IE"))
            {
                // To stop the page postback in Firefox and chrome
                ((IJavaScriptExecutor)webDriver).ExecuteScript("window.stop();");
            }

上面的代码停止了页面加载,并且运行正常.

The above code stops the page load and it works perfectly.

但是,在Firefox 23.0及更高版本中,此JavaScript无法正常工作.否则,对于Chrome 31.0和IE 9,JavaScript可以正常工作.

However, in Firefox 23.0 and above, this JavaScript is not working. Otherwise, for Chrome 31.0 and IE 9, the JavaScript works fine.

这篇关于使用C#Selenium WebDriver将文本发送到具有onkeypress事件的文本框时遇到问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆