Encountering a "require is not defined" error when trying to launch a Selenium

I have developed a basic selenium application and now I am looking to implement a graphical user interface for it.

Here is the code snippet:

index.html:

<html>
  <head>
    <meta charset="UTF-8" />
    <title>Selenium App</title>
    <link rel="stylesheet" href="index.css" />
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="9cfee9f0f1fddcacb2a5b2a8">[email protected]</a>/css/bulma.min.css">
    <script defer src="selenium.js"></script>
  </head>
  <body>
    <div class="align">
      <h1>Selenium App</h1>
      <button id="start" class="button is-primary">Start</button>
      
  </div>
  </body>
</html>

selenium.js:

startButton.onclick = initiateSelenium;

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

async function initiateSelenium() {
    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 encountering an error message:

Uncaught ReferenceError: require is not defined

Any suggestions on how to solve this issue?

Answer №1

If you are working with selenium.js, make sure to remove this line of code:

startButton.onclick = selenium; // (This is specific to browser Javascript)

Keep in mind that selenium.js is a Node.js file and cannot be directly used in an index.html file like this:

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

Look into using Npm selenium-webdriver instead.

To access your Selenium code from an html file through a server, consider using technologies like:

Express is known for its user-friendly features, making it a popular choice for developers.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Utilizing Jquery to locate a specific word within a sentence and then appending a span element around

I need help fixing a function that is supposed to find a specific word in a sentence and add a span element around it. Here is the code I have so far, but for some reason it's not working as expected. Can anyone spot the mistake? Thank you! ...

Tips for creating a Selenium WebDriver script to automate login functionality on a specified webpage referenced in the provided link

<a class="login" data-toggle="modal" href="/login/">Log in</a> The snippet of code above is designated for the log-in functionality when clicking on a specified icon. It pertains to accessing the log-in page of a particular website. Unfortunat ...

Obtaining an array element from mongoose at the corresponding index of the query

My Schema looks like this: const PublicationSchema = mongoose.Schema({ title: { type: String, required: true }, files:[{ contentType: String, data: Buffer, name: String }] }) I am attempting to re ...

Best practices for aligning the widths of input-group-addon elements

Hey there, I'm working with an HTML file that has 3 input options. Here's a snippet: <script type="text/ng-template" id="dashboard_assigngroup_popup.html"> <div class="modal-header modal-header2"> <h3 class="modal-tit ...

Are there any limitations imposed by Google on utilizing XPath to locate a form through its id?

All: Is it possible that Google somehow blocks the searching of elements by id using Xpath? WebDriver wbdriver = new HtmlUnitDriver(); wbdriver.get("https://www.google.co.in/?gfe_rd=cr&ei=oB5BVNXnOKzV8gePnYDQBA"); WebElement element = w ...

What steps should be taken to resolve the error message "TypeError: Cannot read properties of undefined (reading 'toLowerCase')?"

After creating a basic search bar, I encountered an issue when typing in it for the first time: TypeError: Cannot read properties of undefined (reading 'toLowerCase') However, when I closed the pop-up and tried again, the search bar worked prope ...

The problem of having an undefined state in Vuex arises

https://i.stack.imgur.com/52fBK.png https://i.stack.imgur.com/GcJYH.jpg There is an error occurring: TypeError: Cannot read property 'state' of undefined ...

How can one properly iterate through an HTML Collection in JavaScript?

I need help with creating a slider using JavaScript. The code I have written below calculates the widths of the slides, but it's throwing an error in the console. Can someone advise on how to properly loop through and assign width to these elements? ...

Take out property that was placed in the "style" tag

One particular static HTML file that I have consists of the following code: <style> p { overflow-wrap: break-word; word-break: break-word; hyphens: auto; } </style> <div id="wrapper"> <p>Insert text here ...

How can I make a basic alert box close after the initial click?

After clicking a button, I have a simple jQuery script that triggers a fancybox. However, the issue is that the fancy box does not close after the first click of the button; it only closes after the second click... Another problem arises inside the fancy ...

Retrieving JSON information from a lone field in a MySQL table

I am currently working with a PHP REST Web service that is being accessed from JavaScript. Everything works fine when I run a MySQL SELECT statement to select text fields and then use json_encode to convert the returned array into JSON objects. However ...

What is the best way to incorporate JavaScript code as React syntax?

For my project, I am using the OpenLayers API Map. I'm unsure of the correct way to incorporate JavaScript code in React or vice versa. Here is an answer on how to use markers in the map, but I am struggling to implement it in my current code since I ...

issue with horizontal scrolling in react menu component

**Hi there, I'm encountering an issue with react-horizontal-scrolling-menu. When scrolling, it moves to the right excessively and causes other elements to disappear. Additionally, adding overflowX: 'scroll' to the BOX doesn't activate t ...

Transforming JSON data into a dynamic Tableview

I've been experimenting with this issue for quite some time now, but I can't seem to find a solution. My API returns tasks in JSON format. When I print the data using Ti.API.info(this.responseText), it looks like this: [INFO] [{"created_at":"20 ...

Utilizing JS setTimeout to iterate through AJAX requests in a loop, however the excessive frequency of setTimeout calls becomes an issue

I recently encountered an issue that was affecting my code functionality. After some investigation, I discovered that the function myEventMoveTrainManaul() was being called from multiple locations within the code, causing unexpected behavior. I want to exp ...

Combine all div elements to create a unified image and then proceed to save it as a jpg file before uploading to the server

These are the divs below: <div style="width:800px; height:420px; position:absolute; top:0px; left:0px; z-index:10; background-image: url('https://3t27ch3qjjn74dw66o1as187-wpengine.netdna-ssl.com/wp-content/uploads/2016/05/052516-800x420-vege-Wallp ...

I am having issues with the Load More Posts Ajax Button on my WordPress site and it is

I'm trying to display more posts when a button is clicked. Upon inspecting and checking the network, everything seems to be working fine, or at least that's what I assume. https://i.sstatic.net/44VS1.jpg https://i.sstatic.net/KEkiz.jpg I&apos ...

"Discover the best way to sort through an array of complex objects with varying levels of nesting using a specified search

I have come across similar answers, but none of them go beyond two levels deep. Therefore, I believe this is not a duplicate problem. My task is to filter an array of objects where each object may contain nested objects of unknown depth. In my data structu ...

Using single quotation marks in Javascript

When the variable basis contains a single quotation mark, such as in "Father's Day", I encounter an issue where the tag is prematurely closed upon encountering the single quotation mark. 'success' : function(data) { div.innerHTML = &apo ...

Ways to avoid executing JavaScript code that is outputted in PHP

My question relates to the code snippet below, which is obtained via the jquery Data object on a php page. echo " var $d = $('<div/>', { id: 'hi' + $('#textResp').children().length, class: 'even ...