How can I choose which button to click in selenium if there are multiple buttons on the page with the same name, value, and id?

This particular button actually opens a popup, even though it is located on the same page.
![click here for image description][1]

I attempted to interact with the element using the following code:

driver.findElement(By.tagName("td")).findElement(By.id("leadCaptureList_leadCaptureList_assignCampaign")).click();

However, I encountered an error message stating "Unable to locate element: {"method":"id","selector":"leadCaptureList_leadCaptureList_assignCampaign"}"

Answer №1

In case the other button is not located within a td element, you can utilize the code provided below.

driver.findElement(By.xpath("//td/input[@id='leadCaptureList_leadCaptureList_assignCampaign']")).click();

Alternatively, if both buttons share the same xpath, you can incorporate an index in the xpath as shown:

driver.findElement(By.xpath("(//td/input[@id='leadCaptureList_leadCaptureList_assignCampaign'])[1]")).click(); //Make sure to replace [1] with the correct index

If the above methods do not yield results (which seem unlikely), it might be necessary to inspect the HTML of the second button to identify any disparities between the two buttons.

Answer №2

Following Husam's advice, remember to include the parent in your selector, like the div that appears.

//let's say the popup div has an id of "popup"

$("#popup #button").click() could be what you're looking for.

I hope this information proves useful.

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

The PhantomJs browser is not able to open my application URL

Recently, my scripts in PhantomJS browser have stopped running. Whenever I try to capture screens, all I get are black screens. To troubleshoot this, I manually opened a URL in PhantomJS using the command window and ran the script below to verify if it ope ...

Make a new directory and insert a JSON file when installing the APK

I'm looking to programmatically create a folder on the sdcard storage of my phone and then add 3 JSON files that I have already created after installing the APK. How can I achieve this? ...

Unable to select dropdown menu on Safari browser using MacBook Pro

I am just starting out with automation and I'm having trouble clicking on a dropdown in my application using Safari Browser. The script keeps failing with an error message. Can someone please assist me? Error : An unexpected server-side error occurr ...

Masonry layout organizes images in a vertical stack, with one image per row

After implementing Masonry on my website, I encountered an issue where all the images stack vertically in a single row once the page loads. Instead, I would like them to stack both horizontally and vertically. You can view the problem on My Jsfiddle. This ...

The isDisplayed() method is taking an unexpectedly long time to execute

Currently, I am working on a Java Selenium project where I am using the isDisplayed() method to check if certain elements are displayed or not on the page. However, each execution of this method seems to take around 45 seconds to complete. I am wondering ...

Creating dynamic links between two cells of two HTML tables within an HTML email using JavaScript

A recent project requires the creation of HTML-based emails with tables for each automation script within the test automation framework. Additionally, there is a request for an extra HTML table to provide a quick overview. The cells in the first column of ...

The "read more" button seems to be malfunctioning

I've been working on integrating a gallery of images into my posts. My goal is to display 4 images initially, followed by a "load more" button that, when clicked, will reveal another set of 4 images, and so on. I have already created the HTML, CSS, an ...

What is the process for integrating three.js code manually into an iframe?

I recently posted a question on Stack Overflow inquiring about how to input code into an iframe without using a file or URL. While I was successful with simple cases like <h1>Hello World</h1>, my ultimate goal is to integrate three.js into thes ...

Executing XSS Reflected Attack by Loading an External JS Script via POST Parameter

Experimenting with XSS attacks on my vbox machines, just for kicks! I have two .html files - one works and the other doesn't. The file that works contains: <html> <head></head> <body> <form method="post" action=&q ...

Screen the received JSON data for a specific section of the website

Is there a way to extract Json data from a website using "selenium.captureNetworkTraffic("json")"? I need to refine the results for a specific grid on my website which contains multiple grids. How can I filter the data for a particular grid? Additionally ...

Passing an array from PHP to JavaScript using AJAX

Here is the code snippet on the server side: <?php header('Content-Type: text/html; charset=utf-8'); require "../general.variables.php"; require "../functions_validation.php"; require "../functions_general.php"; require "../../db_con.php"; $ ...

Protecting String in PHP to Utilize in JavaScript

When I receive code through AJAX, I handle it as shown below: $verifiedSubject = addslashes(htmlentities($_REQUEST['subject'])); $verifiedBody = addslashes(htmlentities($_REQUEST['body'])); $verifiedAttachment1 = addslashes(htmlentitie ...

"Using Node.js Express 4.4 to efficiently upload files and store them in a dynamically

Looking for recommendations on the most efficient method to upload a file in node.js using express 4.4.1 and save it to a dynamically created directory? For example, like this: /uploads/john/image.png, uploads/mike/2.png ...

What is the best method for enabling communication between two users when the identification numbers created by socketIO are automatically generated?

Hey there, hope you're doing well. I've been pondering a question that has left me stumped. I recently came across a course that explained how to send a message to a specific user using socketIO.js by utilizing the `to()` method and passing the ...

The declaration file for the module 'bootstrap/dist/js/bootstrap' could not be located

Currently, I am developing a Next.js application and have integrated Bootstrap for styling. However, I am encountering an error when trying to import the bootstrap.bundle.js file. I am facing the following error message: Could not find a declaration file f ...

What is the process for moving the final character to the beginning of a string?

Initially, the last letter in the string is being displayed. How can I rearrange it so that the last character appears first in the value? https://i.stack.imgur.com/uGq6H.jpg contentHtml += "<td rowspan1=\"" + 1 + "\" class=\"" + ( ...

changing size when hovered over with the mouse is not consistent between entering and exiting

Hi there, I'm currently utilizing the transform: scale(); property on a website and could use some assistance with a particular issue I haven't been able to find an answer for online. Here is the code snippet I'm working with: HTML: <d ...

Changing the data type of a column in an Excel file from XLSX to

I am currently working with the XLSX npm package and attempting to download a sample Excel file, add some data to it, and then upload it back. The fields in the file include MOBILE NUMBER, DATE, TIME, and NAME. When I upload the file, the values for the DA ...

Switch the functionality of a Google Chrome extension back and forth numerous times

My Google Chrome extension works by attaching event listeners to all elements on a page and inserting CSS (lol.js and style.css) when clicked. However, I am facing an issue where I cannot remove the JS and CSS upon clicking the icon again. Right now, I am ...

Form a hierarchical data structure using a combination of three arrays

Here are three sets of data: let firstData = [ {key: value1}, {key: value2}, {key:value3} ] let secondData = [ {key: value1}, {key: value2}, {key:value3}, {key: value4} ] //same structure, different values let thirdData = [ [1,1,1,1], [1,1,1,1], [1,1,1,1] ...