Execute a JavaScript file stored in the filesystem using Selenium

Having trouble running a YUI js script using js.executeScript Selenium method. The script is being executed by Selenium Webdriver to simulate a "click" on a hybrid mobile app (the button is in a webview).

 String IncludeYUI = "script = document.createElement('script');script.type = 'text/javascript';script.async = true;script.onload = function(){};script.src = '"
                    + YUI_PATH
                    + "';document.getElementsByTagName('head')[0].appendChild(script);";
            js.executeScript(IncludeYUI);

where the YUI_PATH is a URL - https://cdnjs.cloudflare.com/ajax/libs/yui/3.18.0/yui/.....

Encountering a problem due to the lack of access to the global network from the current site.

Considering saving the script under the project and loading it from the file system, but facing limitations with JavaScript and file system access.

Any suggestions on how to handle script loading in this scenario?

Thank you

Answer №1

Are you in the process of loading an HTML page somewhere? If so, you would probably load your JS file in a similar manner. Just like how you requested your server to load the HTML page, you would do the same for the JS file.

The syntax for loading a JS file would typically be like this:

<script src="scripts/yourFile.js">

It's uncommon to see a JS file being loaded the way it is in your code sample. It's usually recommended to simply include a script tag in your HTML for better practices.

If you can share your HTML code, we can offer more specific assistance. Feel free to update your question with the HTML code, and I will adjust my answer accordingly.

Answer №2

One solution finally emerged after numerous attempts - someone recommended working with jQuery. After some investigation, I discovered that using executeScript with jQuery's tap function did the trick...

$('#btn_login_button').trigger('tap');

I found it curious that none of the other methods involving clicks or element coordinates were successful.

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

Adjust the appearance of an element based on user selection from a dropdown menu under certain circumstances

I am working on creating a dropdown menu using a JavaScript array. My goal is to display a specific span element when certain options are selected. For instance, I want the span to be shown only when options "a" and "c" are selected, but not when option " ...

Synchronous loop in Javascript

Is there a method to execute codes asynchronously within a for loop? In this scenario, events ranging from day x to y need to be inserted into the calendar using a for loop. However, the current loop is taking too long, as it has to cover all the days fro ...

Stopping video playback when hovering over it

Can anyone help me modify this code so that a video plays only when hovering over it, and stops playing once the hover ends? What changes should I make to the JavaScript portion of the code for this functionality? <div class="padding-box height-40"&g ...

Passing arguments from the command line to an npm script

The scripts section in my package.json is currently set up as follows: "scripts": { "start": "node ./script.js server" } This allows me to use npm start command to initiate the server. All working well so far. However, I ...

What is the method to use Python in writing content within a <div> tag?

Edit To tackle this issue, I have discovered that the sned_keys() method of the selenium driver can provide a solution. Currently, my focus is on creating a webwhatsapp spamming script and the task at hand involves: <div class="input" contenteditable= ...

What is the best way to apply typography theme defaults to standard tags using Material-UI?

After reading the documentation on https://material-ui.com/style/typography/ and loading the Roboto font, I expected a simple component like this: const theme = createMuiTheme(); const App = () => { return ( <MuiThemeProvider theme={theme}> ...

Gather information from various mongodb collections and consolidate them into a single array using Javascript

Is there a way to retrieve the most recent item from multiple collections and store them in an array based on the collection name and newest item? How can I accomplish this either sequentially or asynchronously? let dat = ["test", "test2"]; ...

Is there a method to generate an endless carousel effect?

Hello, I am trying to create an infinite carousel effect for my images. Currently, I have a method that involves restarting the image carousel when it reaches the end by using the code snippet progress = (progress <= 0) ? 100 : 0;. However, I don't ...

Configuring the default download location with Selenium WebDriver in Java

Is there a way to change the default download path when using the latest Chrome driver version 2.35? The code snippet provided is currently saving files in the default location instead of the specified one. driver.get("page where download ...

Examining an array to identify palindromes

Is there a way to loop through an array and check if each word is a palindrome, instead of manually passing an argument for each word? If a word is a palindrome, return the word; otherwise, return 0. var myArray = ['viicc', 'cecarar', ...

Is it a mistake? Using React and ES6 without Babel may not be the

Have you ever considered bundling all of your classes into a single file without using Babel to polyfill it to ES5? If the browser doesn't support ES6, you could then use Babel in the browser or load the polyfilled bundle and manually add the dependen ...

Protractor unable to locate elements using by.repeater

What is the best method for targeting this repeater with Protractor? <a ng-repeat="item in filteredItems = (items | filter:'abc')">{{item}}</a> ...

It is not possible to transfer information to a JSON document using node.js filesystem and browserify

In an attempt to convert incoming input data from a form into JSON format for storage without a backend, I am exploring the use of Node.JS module "fs" or "file-system". To make this work in a browser environment, I am using Browserify for bundling as it r ...

Effortlessly submit form data in Codeigniter without the need for page refreshing using jQuery ajax

I've been working on submitting form data in the codeigniter framework using ajax and jQuery to prevent page refreshing, but I keep getting a fail message. Since I'm new to ajax, can someone help me troubleshoot this error? This is my Controlle ...

In node.js, what is the syntax for invoking a function within a struct from that same function?

Here is a brief overview of my code: exports.entity = { name: "Foo", //Etc... start: function () { this.attack(); }, attack: function () { setTimeout(attack, 1000); //Doesn't work ...

How come TinyMCE is showing HTML code instead of formatted text?

I have been working on integrating TinyMCE with React on the frontend and django(DRF) on the backend. After saving data from TinyMCE, it retains the HTML tags when displayed back, like this: <p>test</p> <div>Test inside div</div> ...

Automating web scraping tasks using Selenium for a repetitive process

I am attempting to extract contact information from companies listed on a website: I have developed the following code to achieve this: from selenium import webdriver from selenium.webdriver.common.keys import Keys from selenium.webdriver.common.by import ...

Utilizing Selenium Grid in Conjunction with Page Object Model Page Factory

I am looking to optimize my browser initialization process for testing on multiple browsers using the Page Object Model Page Factory. Currently, I have the following setup in my Base Class: // initialize driver/browser public void initDriver() throws Ma ...

Updating a form submit does not retain the value of the JQueryUI Progress Bar

I am currently working on setting up a JQuery Progress Bar that updates when the user submits a form. The code I am debugging is basic and consists of: <body> <form id="form1" method="GET" runat="server"> <div> <h1>Test</h1& ...

The getStaticProps() function is failing to send data over to the components

I am currently learning how to use Next.js by following the guide on nextjs.org. My question is, when I use the getStaticProps function, it seems to fetch and log the data correctly inside the function. However, when I pass the data (which is the props ob ...