Launching selenium app from electron require is not defined error

Viewed 43

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 Answers

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

Related