处理发生在 keyup 事件上的 javascript 弹出窗口 [英] Handling a javascript popup occurring on a keyup event

查看:33
本文介绍了处理发生在 keyup 事件上的 javascript 弹出窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 HTML 页面上有一个文本字段,用于检查您是否输入了 1 到 365 之间的值.如果用户输入了无效值,例如非数字字符或不在范围内的值,它显示一个弹出窗口.我在 watir wiki 上看到有一个 select_no_wait 方法,用于在您从列表中选择无效值时关闭弹出窗口.

I have a text-field on a HTML page which checks whether you have entered a value between 1 to 365. If the user enters an invalid value like a non-numeric character or a value which does not fall inside the range, it shows a popup. I saw on the watir wiki that there is a select_no_wait method which is used to dismiss popups when you select an invalid value from the list.

处理在 keyup 事件上发生的弹出窗口的好方法是什么?我是否需要按照 select_no_wait 方法的实现方式继续进行,或者我们可以启动一个不同的进程来消除调用 set 方法时可能出现的弹出窗口.

What is a good way to handle a popup occurring on a keyup event? Do i need to proceed along the lines the way select_no_wait method is implemented or can we launch a different process which will dismiss the popups which might occur when the set method is called.

在 Javascript 中带有验证函数的 HTML 文件示例如下:

A sample example of the HTML file with a validate function in Javascript would be:

<html>
<head>
<script type="text/javascript">
var num = 0
function validate(e)
{
 var charPressed = String.fromCharCode(e.keyCode);
if(charPressed >= '0' && charPressed<= '9')
{
  num = parseInt(document.getElementById('foo').value);
   if ((document.getElementById('foo').value).length <= 3 && (num > 365 || num==0))
    alert ("Values can only be from to 1 to 365");
    }
  else
  {
  alert("Invalid character entered");
  document.getElementById('foo').value = "";
  }
}
</script>
</head>
<body>
<label id="code"> Sample </label>
<input type="text" id ="foo" onkeyup="validate(event)" maxLength="3"/>
</body>
</html>

我非常感谢有关此问题的任何指示.提前致谢.

I would really appreciate any pointers on this issue. Thanks in advance.

推荐答案

对于 Watir:

目前有一些动作会等待页面加载,例如 .click.select,因为在这些动作之后页面通常会通过回发或表单进行更新邮政._no_wait 变量存在于那些 ,作为一种告诉脚本继续进行的方式,而无需等待弹出窗口发生时发生页面加载.

For Watir:

There are some actions that currently wait for a pageload, such as .click, .select, because after such actions the page is often updated via a postback or form post. The _no_wait variant exists for those , as a way to tell the script to proceed without waiting for the page load to occur when a popup will be occurring.

目前 .set 没有等待页面加载逻辑(据我所知),因此没有该方法的 _no_wait 变体.因此,您可能可以直接从设置值到查找弹出窗口.

Currently .set has no wait for page load logic (as far as I know anyway) and thus no _no_wait variant of the method. So you can probably just proceed straight from setting the value, to looking for the popup.

请注意,如果您在使用 .set 后没有看到弹出窗口,您可能需要触发 'onkeyup' 事件以触发您的客户端脚本.还要确保它真的是一个 JS 弹出窗口,还有很多其他方法可以做一些看起来像 JS 弹出窗口的东西,但实际上只是像 div 之类的东西.

Note that if you don't see the popup after using .set, you may need to fire the 'onkeyup' event to trigger your client side scripting. Also be sure it's really a JS popup, there's lots of other ways of doing stuff that looks like the JS popups, but are really just things like divs etc.

由于这是一个 Javascript 弹出窗口,您应该能够通过类似于 Watir Wiki

Since this is a Javascript Popup, you should be able to get rid of it via code similar to the examples in the Watir Wiki

browser.javascript_dialog.button('OK').click


如果您使用 Watir-Webdriver,则情况会有所不同.我将在此处详细介绍,因为此信息尚不那么容易获得(尚)

If you are using Watir-Webdriver then things are a bit different. I'm going into a bit more detail here because this info is not as easily available (yet)

Webdriver 有一个警报 API,而 Watir-Webdriver 有一个警报助手,它利用了它,其中有几种不同的方法来处理各种样式的 javascript 弹出窗口.方法在 RDoc for watir-webdriver 下查找alerthelper"主要的 watir 类.这些是那里给出的例子.

Webdriver has an Alerts API, and Watir-Webdriver has an alerts helper which makes use of that, with a few different methods in it to deal with various styles of javascript popups. The methods are described in the RDoc for watir-webdriver look for 'alerthelper' under the main watir class. These are the examples given there.

require "watir-webdriver/extensions/alerts"   #add to require section at top of script)

browser.alert do
  browser.button(:value => "Alert").click
end #=> "the alert message"

browser.confirm(true) do
  browser.button(:value => "Confirm").click
end #=> "the confirm message"

browser.prompt("hello") do
  browser.button(:value => "Prompt").click
end #=> { :message => "foo", :default_value => "bar" }

请注意,这些示例几乎直接取自 alerthelper 代码的 rspec 测试.因此,如果不了解这个特定的小细节,它们对您来说可能并不完全有意义.循环内的 click 方法是使警报出现的操作. 测试网页有三个按钮,其值分别为 Alert、Confirm、Prompt,当针对该页面运行测试时,它们会导致弹出窗口出现.

Note that these examples are taken pretty much straight from the rspec tests for the alerthelper code. As such they may not make perfect sense to you without knowing this particular little detail. The click method inside the loop is the action that makes the alert appear. The test webpage has three buttons, with values Alert, Confirm, Prompt, which cause the popups to appear as the tests run against that page.

要在您的代码中使用这些内容,请将中间行替换为脚本中导致弹出窗口的任何操作.例如,在我的一个脚本中,我单击一个按钮以删除用户的信用卡,然后会弹出一个确认对话框.所以我的代码最终看起来像这样(请注意,此代码旨在同时适用于 watir 和 watir-webdriver,$webdriver 根据使用的情况设置为 true 或 false.

To use this stuff in your code, replace that middle line with whatever action in your script is causing the popup. For example in one of my scripts I click a button to delete the user's credit card, and there is a confirm dialog that pops up. So my code ends up looking like this (note this code is designed to work for both watir and watir-webdriver, $webdriver gets set to true or false depending on which is being used.

if $webdriver #watir-webdriver
  $browser.confirm(true) do
    $browser.div(:id => 'credit_cards').link(:text =>'Delete').click
  end 
else #watir
  $browser.div(:id => 'credit_cards').link(:text =>'Delete').click_no_wait
  $browser.javascript_dialog.button('OK').click
end

顺便说一句,在每个示例中结束"之后的注释?如果您将循环的输出分配给变量,这就是您得到的结果

BTW the comment after 'end' in each of those examples? That's what you get back if you assign the output from the loop to a variable

confirm_message = browser.confirm(true) do
  browser.link(:text => "Add Lasers").click
end 

puts confirm_message
> "do you really want to put lasers on sharks?"

这篇关于处理发生在 keyup 事件上的 javascript 弹出窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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