Having trouble clicking or interacting with JavaScript using Selenium and Python

Issue: Unable to interact with elements on a specific webpage after opening a new tab.

Using WebDriver Chrome.

I can successfully navigate the initial webpage until I click a link that opens a new tab, at which point I am unable to perform any actions.

Here is what I have tried after thorough investigation:

1- After switching to the new tab using driver.switch_to.window, I confirmed the correct page title was displayed.

2- I attempted both implicit and explicit waiting methods (including time.sleep) to ensure the page fully loaded.

3- Although there were no iframes present, I checked the number of frames (2) and switched between them, trying to locate elements using full xpath without success.

4- Attempted to maximize the window.

Despite trying various approaches, including inspecting the elements revealing JavaScript functions with arguments, such as:

 href="JavaScript:SWESubmitForm(document.SWEForm2_0,s_10,"s_2_1_96_0","VRId-0")" tabindex="2997" id="s_2_1_96_0">Track Faults 

Further investigation uncovered that s_10 is a variable within the script containing different options from s_0 to s_14.

var s_0 = {action:"/esales_enu/start.swe",target:"_sweview",SWECmd:"GotoView",SWEMethod:"GotoView",SWEView:"L2C Track Channel Reference View BT",SWEApplet:"L2C New CZ Home Page Applet BT",SWEReqRowId:"0",SWESP:"false",SWENeedContext:"true",SWEKeepContext:"0",SWEDIC:"false"};

var s_10 = {action:"/esales_enu/start.swe",target:"_sweview",SWECmd:"GotoView",SWEMethod:"GotoView",SWEView:"New Portal Fault Search View ORH BT",SWEApplet:"L2C New CZ Home Page Applet BT",SWEReqRowId:"0",SWESP:"false",SWENeedContext:"true",SWEKeepContext:"0",SWEDIC:"false"};

In an attempt to execute the JavaScript function using driver.execute_script(), I inputted:

driver.execute_script("SWESubmitForm(document.SWEForm2_0,s_10,'s_2_1_96_0','VRId-0')")

The above code resulted in the following error:

selenium.common.exceptions.JavascriptException: Message: javascript error: SWESubmitForm is not defined 

What could be the missing element here? Is it a syntax issue? Any guidance on how to proceed would be highly appreciated.

Thank you.

Answer №1

I successfully resolved this issue and gained a deeper understanding in the process. The concept of JavaScript that I had been studying didn't click for me initially because I was mistaken about how driver.execute_script() functions; it's more about injecting code or locating elements.

Interestingly, the key to solving the problem turned out to be frames :)

I had been so fixated on maneuvering between the two frames I initially discovered that it never occurred to me that there could be additional frames nested inside!

In the end, after switching back to the main frame, I navigated again to the second frame within that main one and successfully located elements using xpath.

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

Issue encountered: The differ cannot recognize the provided object '[object Object]', which is of type 'object'. NgFor is limited to binding Iterables only

I have been following a tutorial on Ionic created by Paul Halliday, focusing on a shopping list project that utilizes Firebase and Angular. However, I am encountering an error every time I try to run the application: Error: Uncaught (in promise): Error: ...

Dropbox menu within an extended webpage

I am looking to create a dropdown menu that behaves like the one on this website. The goal is for the dropdown to cover the entire webpage, hide the scroll bar, and "unmount" the other elements of the page, while still displaying them during the transition ...

Having trouble retrieving parameters within the expressjs router.delete endpoint implementation

Check out my code snippet below where I utilized Express router and Mongoose Model. I am encountering an issue accessing the id parameter. router.delete('/task/:id', function (req, res) { Task.remove({ ...

What is the best method in JavaScript to create three different shades of a color?

My sass function generates lighter and darker versions of a base color, here is the code snippet: $colors: ( betpawa-green: #107A3D, lime-green: #8DC63F, yellow: #FBCD00, ); @mixin color-generator { @each $name, $hex in $colors { &-#{$nam ...

Is it possible to create a button on-click feature without the traditional button appearance?

Currently, I am creating a button using React, but I don't want it to have the traditional button appearance. Is there a way for me to make it a clickable div without the default button style? Below is the code I am working with: <div style={style ...

:after pseudo class not functioning properly when included in stylesheet and imported into React

I am currently utilizing style-loader and css-loader for importing stylesheets in a react project: require('../css/gallery/style.css'); Everything in the stylesheet is working smoothly, except for one specific rule: .grid::after { content: ...

Ways to verify whether a callback function supplied as a parameter in Javascript has any arguments

My task involves implementing the following: doSomething .then(success) .catch(failure); The success and failure functions are callbacks that will receive their value at runtime (I am developing a module). Therefore, I need to ensure that the fa ...

Having trouble displaying an image in p5.js on Visual Studio Code

I can't seem to figure out how to load images in visual studio code for p5.js. Can someone help me understand the process of loading images in p5.js? I've set up a new project, but the images I try to load are not displaying correctly. How can I ...

Unable to import CSV file

I'm currently in the process of developing a CSV editor that involves importing a CSV file and converting it into HTML tables. Below is the code I have been working on: <html> <head> <title></title> </head> <body& ...

Failure to display alert message upon completion of AJAX request

I am attempting to use AJAX to send data to the database without refreshing the page when a user favorites a message. Even though the data is successfully sent to the DB, the page still reloads and the alert message I want to display is not showing up. Th ...

How can I use Python with Selenium to verify if the element text includes specific text?

Checking if an element contains specific text in Selenium I have successfully implemented this functionality in Selenium IDE, but I am unsure of how to achieve the same using Python and Selenium. I need to create a function that waits until the spec ...

Can multiple `npm install` commands run at the same time?

Is it possible to have concurrent runs of npm install, or will they conflict on shared resources and create potential race conditions? Analyzing the code might not provide a clear answer, but for those working on developing npm, this is likely an implicit ...

Should DOM tags be utilized in xpath locators for better practice?

Often in my test cases, I rely on XPath locators to verify text attributes. For instance: public By label(String text) { return By.xpath("//label[contains(text(),'"+text+"')]"); } and then in my test cases, I utilize it as follows: webUI.v ...

Sending a C# variable to an HTML file

I’m struggling to understand how I can use jQuery to access a variable from C# named sqlReq. My main objective is to be able to input my own SQL data into a PieChart. However, I’m unsure about how to call and define C# SQL requests in HTML or jQuery. ...

The functionality of Directive hinges on the use of a template

I am exploring a more web-component approach to using Angular. As part of this, I have developed an http-request directive with url and response attributes. The implementation is successful, but I find that my directive relies on a template unnecessarily. ...

Is it possible to retrieve the index of a particular element within an array during an update operation in MongoDB?

After executing the following update statement const result = await Post.updateOne({_id: postId},{ $pull: {reacts: {publisher: req.query.publisher}}, $inc: {postPoints: - reactsEnum[ReactType]} }); I am interested in obtaining the ...

How to use the sha512 hash function in Node.js for Angular2 and Ionic2 applications

I'm attempting to generate a SHA512 Hash in Angular2 (Ionic2) that matches the PHP function hash('sha512'). After trying out different modules like crypto-js, crypto, and js-sha512, I keep getting a different Hash compared to PHP. I even a ...

How can I effectively monitor and manage updates in my Vue app that is performing CRUD operations?

In my current project, I am developing a Vue application that utilizes Vuex to retrieve objects from an API. These objects are displayed in tables with paging functionality and are retrieved in batches from the API, sometimes containing nested entities. Th ...

"Auth.currentSession is indicating that there is no user currently logged in

I am currently working on a basic React app with authentication using aws-amplify. My user pool is set up in Cognito and I can successfully redirect the user to the hosted UI for login. However, when trying to retrieve the current session, I am receiving a ...

It's impossible to remove a dynamically added class from a button

I'm facing an issue with adding and removing classes from a button using jQuery. I added a new class to the button, then removed it, but now when I click the button again I want to revert back to the initial class. Unfortunately, my code is not workin ...