1

I am using I wrote a small selenium script in javascript that executes find with node (filename.js) in the terminal but when I try to execute that script with a button click it doesnt work.

I took the guts out and put an alert() and it executed.

Just wondering if anyone can spot what I am doing wrong here?

Here is the script

async function example() {

const {Builder, By, Key, util} = require("selenium-webdriver");
require("chromedriver");
let driver = await new Builder().forBrowser("chrome").build();

await driver.get("http://google.com");
await driver.findElement(By.name("q")).sendKeys("Selenium Test", Key.RETURN);

}

And here is the php file I want to execute it from

    <!doctype html>
<html lang="en">
<head>
    <title>Sidebar 01</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <script src="index.js"></script>

</head>
<body>

<center>
    <button onclick="example();">Hello</button>
</center>

</body>
</html>
Davidito
  • 21
  • 6
  • Can you find the element and use click(), instead of the Return key? – pvy4917 Sep 24 '21 at 00:41
  • 1
    I think you are confused, I am not trying to find the the element, I am trying to execute my script by my php file when I manually click on the Hello button but it doesnt execute. Does that make sense ? – Davidito Sep 24 '21 at 01:47
  • I got it now, can you show me the console logs of chrome when you actually click the Hello button? – pvy4917 Sep 24 '21 at 02:15
  • index.js:3 Uncaught (in promise) ReferenceError: require is not defined at example (index.js:3) at HTMLButtonElement.onclick – Davidito Sep 24 '21 at 02:25
  • You cannot use require!! in this case! https://stackoverflow.com/questions/19059580/client-on-node-js-uncaught-referenceerror-require-is-not-defined – pvy4917 Sep 24 '21 at 02:26
  • I tried several options including requirejs and browserfly, nothing seems to work. – Davidito Sep 24 '21 at 19:36
  • Can you try import and module exports method mentioned in thet answer? – pvy4917 Sep 24 '21 at 21:53
  • It now gives me undefined error – Davidito Sep 24 '21 at 22:53
  • So I figure out how to bypass the require error by using broswerify, my only issue now is being able to call the bundle.js on button click – Davidito Sep 25 '21 at 13:53
  • Any luck with this approach? Or are you still stuck? – pvy4917 Sep 27 '21 at 18:35
  • First I would like to thank you in still showing interest in helping me solve my problem, So my problem is that when I execute the selenium script it is being block from running, strange considering it was running before, could be a security issue ? – Davidito Sep 28 '21 at 13:17
  • Actually a dumb question and might not be related, is the browser opening up and if yes, what are the permissions coming/showing up for the webpage? – pvy4917 Sep 28 '21 at 15:12
  • Not a dumb question at all, The browser doesnt open up anymore and when it did it didnt show permissions – Davidito Sep 28 '21 at 16:47
  • Cannot find module './atoms/get-attribute.js' is where I am stuck https://waelyasmina.com/browserify-tutorial-for-total-beginners/ Also, try the -headless option. I think what you are trying to achieve requires some heavy research. – pvy4917 Sep 28 '21 at 17:34
  • Update: I am using puppeteer instead of selenium, I have the bundle.js file that properly executes my script, the problem now is like before, I can execute the script on page load but I want it to be triggered at the click of a button. – Davidito Sep 30 '21 at 02:27
  • Can you update your sample code? – pvy4917 Sep 30 '21 at 17:44

0 Answers0