How to auto click a link with xpath
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.
javascript jquery button xpath click
add a comment |
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.
javascript jquery button xpath click
Possible duplicate of jquery select element by xpath
– wasipeer
Jan 1 at 8:09
add a comment |
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.
javascript jquery button xpath click
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.
javascript jquery button xpath click
javascript jquery button xpath click
asked Jan 1 at 7:52
SarmadKSarmadK
61111
61111
Possible duplicate of jquery select element by xpath
– wasipeer
Jan 1 at 8:09
add a comment |
Possible duplicate of jquery select element by xpath
– wasipeer
Jan 1 at 8:09
Possible duplicate of jquery select element by xpath
– wasipeer
Jan 1 at 8:09
Possible duplicate of jquery select element by xpath
– wasipeer
Jan 1 at 8:09
add a comment |
1 Answer
1
active
oldest
votes
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);
Have you tested it? Because somehow its not working for me. (No auto click at all)
– SarmadK
Jan 1 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 1 at 12:02
I am using the code injector extension for firefox. Can you point me to some better option?
– SarmadK
Jan 1 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 1 at 12:14
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 1 at 12:25
|
show 2 more comments
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53993882%2fhow-to-auto-click-a-link-with-xpath%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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);
Have you tested it? Because somehow its not working for me. (No auto click at all)
– SarmadK
Jan 1 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 1 at 12:02
I am using the code injector extension for firefox. Can you point me to some better option?
– SarmadK
Jan 1 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 1 at 12:14
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 1 at 12:25
|
show 2 more comments
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);
Have you tested it? Because somehow its not working for me. (No auto click at all)
– SarmadK
Jan 1 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 1 at 12:02
I am using the code injector extension for firefox. Can you point me to some better option?
– SarmadK
Jan 1 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 1 at 12:14
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 1 at 12:25
|
show 2 more comments
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);
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);
edited Jan 1 at 12:24
answered Jan 1 at 11:09
Anders CarstensenAnders Carstensen
1,6781117
1,6781117
Have you tested it? Because somehow its not working for me. (No auto click at all)
– SarmadK
Jan 1 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 1 at 12:02
I am using the code injector extension for firefox. Can you point me to some better option?
– SarmadK
Jan 1 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 1 at 12:14
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 1 at 12:25
|
show 2 more comments
Have you tested it? Because somehow its not working for me. (No auto click at all)
– SarmadK
Jan 1 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 1 at 12:02
I am using the code injector extension for firefox. Can you point me to some better option?
– SarmadK
Jan 1 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 1 at 12:14
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 1 at 12:25
Have you tested it? Because somehow its not working for me. (No auto click at all)
– SarmadK
Jan 1 at 11:55
Have you tested it? Because somehow its not working for me. (No auto click at all)
– SarmadK
Jan 1 at 11:55
1
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 1 at 12:02
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 1 at 12:02
I am using the code injector extension for firefox. Can you point me to some better option?
– SarmadK
Jan 1 at 12:05
I am using the code injector extension for firefox. Can you point me to some better option?
– SarmadK
Jan 1 at 12:05
1
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 1 at 12:14
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 1 at 12:14
1
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 1 at 12:25
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 1 at 12:25
|
show 2 more comments
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53993882%2fhow-to-auto-click-a-link-with-xpath%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Possible duplicate of jquery select element by xpath
– wasipeer
Jan 1 at 8:09