Having difficulty pressing a button using Selenium

I'm having difficulty clicking a button with Selenium.

Here is the DOM structure: http://example.com/dom

I have identified the button element using a valid Xpath that returns one node as shown below:

WebElement send_message_button = driver.findElement(By.xpath("//*[@class='mp-button-content'][.='Send Message']"));

I have attempted to click the button using the Xpath in different ways, but none of them seem to work for me.

Using WebDriver's click() method : send_message_button.click()

JavaScriptExecutor:

JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("arguments[0].click();", send_message_button);

Actions class:

Actions action = new Actions(driver);
action.moveToElement(send_message_button).click().build().perform();

I have also checked if the button is located inside a frame/iframe, but it does not appear to be the case.

Answer №1

After updating the xpath to

.//mp-button[@class='mp-button-primary submit_button']
, the problem was successfully resolved. However, there is a concern about the fragility of this xpath. I am open to any suggestions or recommendations on how to improve it!

Answer №2

Attempt to select the main button

xpath = "//*[@class='mp-button-content'][.='Send This Message']/.."

or its ancestor

xpath = "//*[@class='mp-button-content'][.='Send This Message']/../.."

and implement a waiting period until the element is clickable.

WebDriverWait wait = new WebDriverWait(driver, 30);
WebElement elem = wait.until(ExpectedConditions.elementToBeClickable(By.xpath(xpath)));
elem.click();

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

Whoops! The input buffer seems to be containing an image format that is not supported while attempting to utilize Next JS next-opt

I initially used the default Image Optimization component in my Next JS app, only to realize that the site could only be hosted on Vercel and not on other web hosting platforms. This limitation prompted me to explore the next-optimized-images package, whic ...

Mastering the Sequence of JS Functions (or How Promises Still Confuse Me)

Let me explain my objective in simple terms: I have a set of functions that must be executed in a specific order, and each function has its own sub-order. This is how my code looks currently: async function LastModuleFunction() { await FirstModuleFun ...

Spin the sphere texture in Three.js

Currently, I am working on rendering a sphere through three.js. While applying a texture to the sphere works well, I am having some difficulties rotating the texture to align it with marker positions. The issue arises when trying to place markers over Kag ...

Steps for performing a runtime cast

When working on a web application written in TypeScript, there is a feature where users can add additional JavaScript functions that will be parsed at runtime (new function(Function as String)) for execution. These functions should return an object defined ...

Every time I try to access a module, it immediately crashes

I've encountered an issue while working on my android project. I expected the foodlist module to display image data with descriptions, but instead, it crashes and the logcat shows the following error: Caused by: java.lang.NullPointerException: Attemp ...

How can I retrieve the selected value from an Angular 2 dropdown menu when it changes, in order to utilize it within a function?

I am currently working on creating a dropdown menu with multiple options. When a user selects an option, I need to make an API call that requires an ID parameter. Inside my component.ts file, I have defined an array containing the values like this: valu ...

What is the best way to access an Ajax response outside of its function using JQuery?

Just starting out with JQuery and wondering how to utilize the Ajax response ( HTTP GET ) outside of the function in my code snippet below: $.ajax ({ type: "GET", url: "/api", success: success, error: error ...

An Alternative Approach for Executing a Function within an AngularJS Directive

I am currently working on a unique element directive that displays a user's picture and name. This directive has various configuration attributes, one of which is the uc-on-hover attribute. The purpose of this attribute is to determine what element sh ...

The certification for this company has been rescinded, specifically for Internet Explorer version 11

The certificate belonging to this organization has been invalidated. Issues with security certificates could suggest an effort to deceive you or intercept any information you transmit to the server. I attempted to address this problem by going to Tools - ...

The LinkedIn API encountered an error when attempting to retrieve historical follower data, resulting in a HTTP

I've scoured the depths of the internet in search of a solution to my problem, but none seem to fit what I need. My goal is to retrieve historical follower data from LinkedIn's API using this call: ${companyId}/historical-follow-statistics?time- ...

What is the process for transferring a variable between two different scripts?

The creation of an automated WEB GUI test using a python script (FireFox_FirstTests.py) was successful. To run this test multiple times, a new script (OSNR_Cycle.py) was developed to utilize an index variable "i" for cycle counting. The objective is to inc ...

Storing bytes in Java until saving the Ajax file upload

As a Java developer utilizing Tapestry5, I have integrated a jQuery plugin to facilitate file uploads via ajax. My challenge lies in determining the most effective way to temporarily store the attachments until the page is saved. The current process involv ...

javascript - audio is not working on the web

I've been trying to incorporate sound into my website using this code. Strangely, the sounds only seem to play in Adobe Dreamweaver and not in any browsers. Any advice would be greatly appreciated! :) var audio1 = new Audio('sound1.mp3'); v ...

Issue with JSON-to-MUI card mapping causing absence of UI components or error messages

I'm facing a challenge with mapping this JSON data to MUI Cards. Despite no error messages, the content isn't being displayed as expected. Surprisingly, when I make changes unrelated to the issue, the console.log(questionGroups) only shows the JS ...

Save the input from an HTML text area tag as a Word or PDF file using C# code

In the midst of a challenging ASP .NET project, there is a need to download the content of a text area as a file in formats like .doc, .pdf, and .txt. While it's common knowledge that plain text can be downloaded as .txt using JavaScript, the real qu ...

Tips for creating multiple popups using a single line of JavaScript code

I am new to JavaScript and I am attempting to create a popup. However, I am facing an issue in opening two divs with a single line of JavaScript code. Only one div opens while the other remains closed despite trying various solutions found on this website. ...

Error: The function sort cannot be applied to the result of calling the calendar method on the moment object

I retrieve the data from this URL. I am looking to display the data sorted by a recent date. I attempted to use the map method to render the data and then proceeded to sort it based on the date, but encountered an error. To organize the dates, I made use ...

Enhance the functionality of the custom transaction form in NetSuite by incorporating new actions

I'm currently working on incorporating a new menu option into the "Actions" menu within a custom transaction form in NetSuite. While I can successfully see my selection in the Actions Menu on the form, I'm running into an issue with triggering th ...

Incorporate a video within the royal slider feature

Next is my deluxe slider code. This slider displays only images but I am looking to incorporate video as well. I have searched online and tried different solutions, but haven't found one that works for me. Can someone please guide me on how to achieve ...

Save the test outcomes in HTML format using cypress

Can Cypress test results be exported to an HTML or another format, similar to cucumber-report.html? ...