0

I have created a simple selenium app and now I want it to launch from a gui.

Here is my code

index.html:

<html>
  <head>
    <meta charset="UTF-8" />
    <title>App</title>
    <link rel="stylesheet" href="index.css" />
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bulma@0.9.4/css/bulma.min.css">
    <script defer src="selenium.js"></script>
  </head>
  <body>
    <div class="align">
      <h1>App</h1>
      <button id="start" class="button is-primary">Start</button>
      
  </div>
  </body>
</html>

selenium.js:

startButton.onclick = selenium;

const {Builder, Browser, By, Key, until} = require('selenium-webdriver');

async function selenium() {
    let driver = await new Builder().forBrowser("chrome").build();
    await driver.get('https://google.com');
    await driver.findElement(By.id("L2AGLb")).click()
    await driver.findElement(By.xpath("/html/body/div[1]/div[3]/form/div[1]/div[1]/div[1]/div/div[2]/input")).sendKeys("selenium",Key.RETURN);
}

I keep on getting Uncaught ReferenceError: require is not defined

What should I be doing differently?

1 Answers1

0

In selenium.js you should delete:

startButton.onclick = selenium; // (This is Javascript for the browser)

selenium.js is a Node.js file, you can not use in index.html using:

<script defer src="selenium.js"></script>

Npm selenium-webdriver

You can not import it, instead you need to use Node.js and to access from the html file to interact with your Selenium code you have to do it from a server like:

Express is very easy to use

ManuelMB
  • 1,254
  • 2
  • 8
  • 16
  • How do I import it then? Thanks for the answer btw – elite.dragon Sep 07 '22 at 14:20
  • I edited my previous answer to answer to your question – ManuelMB Sep 07 '22 at 15:05
  • Could you please explain a bit more? (Im quite new to node...) ,Thanks – elite.dragon Sep 08 '22 at 10:42
  • There are lot of videos in internet available about express, like this https://www.youtube.com/watch?v=Oe421EPjeBE from freeCodeCamp.org called Node.js and Express.js - Full Course It could be a good starting point to learn about it. – ManuelMB Sep 08 '22 at 11:05
  • Ok. But what exactly do I have to implement? – elite.dragon Sep 08 '22 at 12:45
  • From your index.html you make a GET request to the Express server when the Start button is clicked, in the Express route you do the Selenium code. – ManuelMB Sep 08 '22 at 15:58
  • You need to start the house from the floor, not from the root, so I recommend you first to learn the basic things before going for more complex. – ManuelMB Sep 08 '22 at 16:08
  • So the electron app will have to be bundled with an express server? – elite.dragon Sep 09 '22 at 16:16
  • Yes, the electron app need to launch the Express Server, there is a lot of info about how to do it https://www.google.com/search?q=electron+express&rlz=1C5CHFA_enES961ES961&oq=electron+express&aqs=chrome..69i57j0i512l5j0i7i30l4.1314j0j4&sourceid=chrome&ie=UTF-8 – ManuelMB Sep 09 '22 at 16:30