自动化 javascript 按钮点击
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5626168/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Automating a javascript button click
提问by MadMod
So I have a button with code like this:
所以我有一个按钮,代码如下:
<div style="text-indent: 0pt; visibility: inherit;" id="button5061">
<a title="Continue" href="javascript:if(%20button5061.hasOnUp%20)%20button5061.onUp()" name="button5061anc">
<img width="82" height="25" border="0" style="cursor: pointer;" alt="Continue" src="images/continueoff.gif" name="button5061Img">
</a>
</div>
And I need to click it with javascript. Right now I am using the firefox extension Chickenfoot which allows me to script the site with a javascript interpreter with some custom commands.
我需要用javascript点击它。现在我正在使用 Firefox 扩展 Chickenfoot,它允许我使用带有一些自定义命令的 javascript 解释器编写站点脚本。
http://groups.csail.mit.edu/uid/chickenfoot/api.html
http://groups.csail.mit.edu/uid/chickenfoot/api.html
I have tried selecting it with xPath (//div/a[@title='Continue']/..) which finds it, but when I click() it nothing happens. Here are some of the things I have tried:
我尝试使用找到它的 xPath (//div/a[@title='Continue']/..) 选择它,但是当我 click() 时它什么也没发生。以下是我尝试过的一些方法:
click(find(new XPath("//img[@alt='Continue']/..")))
click(find(new XPath("//img[@alt='Continue']/../..")))
click("continue")
click("Continue")
click("images/continueoff.gif")
click("continueoff.gif")
click(find("Continue"))
click(find("Continue").element)
click(find("images/continueoff.gif"))
I know this is a rather specific quesiton but any ideas on what to try would be much appreciated.
我知道这是一个相当具体的问题,但任何关于尝试什么的想法都将不胜感激。
回答by penguinrob
If you're trying to simulate a user clicking on it:
如果您尝试模拟用户点击它:
You can simulate a click like this: document.getElementById('theSubmitButton').click();
您可以模拟这样的点击: document.getElementById('theSubmitButton').click();
Here's an example for ya: http://jsfiddle.net/gasWZ/1/
这是你的一个例子:http: //jsfiddle.net/gasWZ/1/
If that's not what you're trying to do, could you explain a bit more?
如果这不是你想要做的,你能解释一下吗?
回答by Matt Sephton
html:
html:
<div>
<a href="#" onclick="alert('ok')">
<img src="">
</a>
</div>
javascript:
javascript:
var xPathRes = document.evaluate ('//div[1]/a[1]', document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null);
xPathRes.singleNodeValue.click();
jsfiddle:
jsfiddle:
回答by Craig
Wouldn't it be effective to pass the anchor object to your function and evaluate the name attribute?
将锚对象传递给您的函数并评估 name 属性不是很有效吗?
- set the href attribute to #
- call the function and pass the obj
- 将 href 属性设置为 #
- 调用函数并传递 obj
<div style="text-indent: 0pt; visibility: inherit;" id="button____"> <a title="Continue" href="#" name="button____anc"> <img width="82" height="25" border="0" style="cursor: pointer;" alt="Continue" src="images/continueoff.gif" name="button____Img"> </a> </div>