开发者

Automating a javascript button click

开发者 https://www.devze.com 2023-02-23 12:24 出处:网络
So I have a button with code like this: &l开发者_开发知识库t;div style=\"text-indent: 0pt; visibility: inherit;\" id=\"button5061\">

So I have a button with code like this:

&l开发者_开发知识库t;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.

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:

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.


html:

<div>
    <a href="#" onclick="alert('ok')">
        <img src="">
    </a>
</div>

javascript:

var xPathRes = document.evaluate ('//div[1]/a[1]', document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null);
xPathRes.singleNodeValue.click();

jsfiddle:

https://jsfiddle.net/7fm89aqd/


If you're trying to simulate a user clicking on it:

You can simulate a click like this: document.getElementById('theSubmitButton').click();

Here's an example for ya: http://jsfiddle.net/gasWZ/1/

If that's not what you're trying to do, could you explain a bit more?


Wouldn't it be effective to pass the anchor object to your function and evaluate the name attribute?

  1. set the href attribute to #
  2. call the function and pass the 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>
0

精彩评论

暂无评论...
验证码 换一张
取 消