0

I am looking for a way to auto click a button or a link when page loads. Currently the Xpath method seems like a good option for me. What I have tried is,

document.evaluate('/html/body/div/div[4]/div[2]/form/p/input', document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue.click();

I am trying to auto click this button on ezgif site. I have also tried to click using the X and Y coordinates of the button but it didn't work either. I know I am doing something wrong because of my little to no experience of coding. Can someone put me in the right direction.

enter image description here enter image description here

SarmadK
  • 123
  • 1
  • 2
  • 14
  • Possible duplicate of [jquery select element by xpath](https://stackoverflow.com/questions/6453269/jquery-select-element-by-xpath) – wasipeer Jan 01 '19 at 08:09

1 Answers1

1

If you check the value of your singleNodeValue, you will notice it is null. This is because your XPath is wrong.

The correct full XPath is: /html/body/div[2]/div[4]/div[2]/form/fieldset/p[4]/input

However, this path is very likely to break in the future, because it relies on the <div>s and <p>s to be located in specific positions. You can make a more "relaxed" XPath which will still find the right element. In fact, you can easily do this in Google Chrome by right clicking the button, click Inspect, and then right click the DOM element and choose Copy Xpath.

The resulting code would be:

document.evaluate('//*[@id="tool-submit-button"]/input', document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue.click();

If you want to use it on page load, you should first check if the video has already been converted. You may also need to wait for the button to load:

var checkTimer = setInterval(function() {
    var button = document.evaluate('//*[@id="tool-submit-button"]/input', document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
    if (button) {
        clearInterval(checkTimer);

        // Only convert if the gif hasn't already been converted
        if (!document.getElementsByTagName("video")[0])
            button.click();
    }
}, 50);
Anders Carstensen
  • 2,949
  • 23
  • 23
  • Have you tested it? Because somehow its not working for me. (No auto click at all) – SarmadK Jan 01 '19 at 11:55
  • 1
    It works. I have added a GIF showing me writing it in the Chrome console and it clicks the button (and then complains that I didn't choose a file). How are you executing the code line? – Anders Carstensen Jan 01 '19 at 12:02
  • I am using the code injector extension for firefox. Can you point me to some better option? – SarmadK Jan 01 '19 at 12:05
  • 1
    It also works in Firefox with Code Injector. Added a screenshot of it. You must be doing something else than simply executing that line of code? – Anders Carstensen Jan 01 '19 at 12:14
  • What I want is to run this command as soon as the page finishes loading, Automatically. Inject method is working but is not helping me in what I need – SarmadK Jan 01 '19 at 12:17
  • 1
    I have now added some code that waits for the button to appear, then checks if the video has already been converted, and if not, then clicks the button. – Anders Carstensen Jan 01 '19 at 12:25
  • Wow. Its working fine now. Thanks for your time. But how have you generated the Xpath. I have used the same method for what I have posted but my Xpath is different – SarmadK Jan 01 '19 at 12:31