0

Got a classic JS button :

<button class="my-button" OnClick="(windows.open('https://anotherwebsite.com'))">CLICK ME</button>

I would like to trigger this button with a CLI tool like cURL for example.Is that possible ?

I did my research before asking, i found a lot of questions about the opposite (how to curl into a javascript) but not mine.

I already know that u can use selenium w/ python/java... or any other alternatives like Jaunt w/ java but it has to come almost out of the box, i don't want to be stuck with some dependencies and i want it light. I have heard about XHR request but i'm kinda confuse since i saw how to run them with a specific language through methods/functions but not with a cli line tool.

PS : I'm not interested in :

curl https://anotherwebsite.com

It HAS to trigger the javascript just like if i go straight to my chrome dev tool>Console and type windows.open('blabla').

j08691
  • 204,283
  • 31
  • 260
  • 272
nihi
  • 5
  • 1

2 Answers2

1

You can accomplish the desired behavior using Chrome Driver and cURL. But prepare, it will take a lot of steps :)

You need to perform the following actions:

  • Download the driver from the aforementioned link and unarchive it to any folder
  • Start the driver (./chromedriver & on Unix or chromedriver.exe on Windows). You'll see the message, find the port. In my case the message was%

Starting ChromeDriver 80.0.3987.106 (f68069574609230cf9b635cd784cfb1bf81bb53a-refs/branch-heads/3987@{#882}) on port 9515

  • Connect to the driver session via cURL curl -d '{ "desiredCapabilities": { "caps": { "nativeEvents": false, "browserName": "chrome", "version": "", "platform": "ANY" } } }' http://localhost:9515/session. In the output of this command, find the sessionId, in my case it was e2fafa7a017a6be7136a02af694f5493
  • Open the page with the button curl -d '{"url":"<your url>"}' http://localhost:9515/session/e2fafa7a017a6be7136a02af694f5493/url
  • Find the button (I use name as a locator, but I believe class will work as well) curl -d '{"using":"name","value":"<your-value>"}' http://localhost:9515/session/27f4262ab044392b05138540055a8fd6/element. In the output of this command you have to find the ELEMENT value. In my case it was 0.044231492091793445-1
  • Finally, click it! curl -X POST http://localhost:9515/session/e2fafa7a017a6be7136a02af694f5493/element/0.044231492091793445-1/click
  • You did it, now close the session curl -X DELETE http://localhost:9515/session/e2fafa7a017a6be7136a02af694f5493

As you see, you have to perform a lot of actions, but I believe it matches your requirements. For more information please refer to this and this links. Good luck!

  • Thanks a lot this is a really good answer, unfortunately after 1 hour of try hard i did find my button with element but the way to find it is wrong so it does not work all the time. It make the solution not viable. I simplified for the purpose of the answer but i have a full table of 12 buttons i need to activate, theses buttons are validated with a token, so when i trigger one, page reload and ELEMENT id changes and i can't find 2nd button ELEMENT id easily. Thanks tho, i will find another way to do this – nihi Feb 27 '20 at 16:14
  • I'd try to use https://github.com/puppeteer/puppeteer Sorry I didn't manage to fully cover your needs :( Happy coding! – Vyacheslav Palamar Feb 27 '20 at 16:16
0

An easy way would be to use jQuery ajax, but if you want vanilla js then you do need to create an XHR request via XMLHttpRequest. The following will create a GET request. Something you need to keep in mind is CORS. It is a mechanism that allows restricted resources on a web page to be requested from another domain outside the domain from which the first resource was served (source). Unfortunately, unlike curl browsers do have that extra layer of security because they are much more popular then terminals.

Source for the below code

 var xhReq = new XMLHttpRequest();
 xhReq.open("GET", "sumGet.phtml?figure1=5&figure2=10", false);
 xhReq.send(null);
 var serverResponse = xhReq.responseText;
 alert(serverResponse); // Shows "15"
Michael Artman
  • 319
  • 1
  • 14