Select a specific element to apply a CSS selector for a button

One of the challenges I am facing involves a webpage with multiple submit buttons. My goal is to iterate through each button and click on them one by one.

While I am aware that this can be achieved using xpath like this (//button[@class='submit'])[i] (where i = loop number).

I'm curious if it's possible to do so via CSS selector as well?

I attempted button.submit, but it always interacts with the first button, whereas I aim to cycle through all of them. I also experimented with button.submit:first-child, but experienced similar results.

The HTML structure resembles the following:

<div>
    <button class="submit" type="button"></button>
</div>
<div>
    <button class="submit" type="button"></button>
</div>
<div>
    <button class="submit" type="button"></button>
</div>

Answer №1

A possible solution to achieve this is the following approach:

For Java versions lower than 8, you can use the following code snippet:

List<WebElement> elements = driver.findElements(By.cssSelector("button.submit"));

WebElement confirm = driver.findElement(By.cssSelector("selector_for_confirm"));

for(WebElement element: elements){
     element.click();
     confirm.click();
}

If you are using Java version 8 or higher, you may consider this alternative method:

List<WebElement> elements = driver.findElements(By.cssSelector("button.submit"));

WebElement confirm = driver.findElement(By.cssSelector("selector_for_confirm"));

  elements.forEach(e->{
        e.click();
        confirm.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

Setting up preferences for Chrome in Selenium using Python is a useful skill to have

Setting preferences for Firefox is simple with the code snippet provided below. set_preference = profile.set_preference set_preference("network.http.response.timeout", 30) set_preference("media.autoplay.enabled", False) set_preference("browser.cache.memor ...

Achieving a persistent footer at the bottom of the page within Material Angular's mat-sidenav-container while using the router-outlet

I am looking to keep my ngx-audio-player fixed at the bottom of the screen, similar to what you see on most music streaming websites. I currently have a structure with divs and various elements for dynamic content and playing music. The issue is that the ...

Is it possible to pass multiple IDs into document.getElementById?

I am working on a form that contains 45 dropdown lists and currently, I am using the following code for validation. Is there a way to modify this code so that I can use a single function to validate all 45 dropdown lists? Below is the Function: function ...

What is the best way to display a book cover and position it in a specific location?

There is a dynamically populated HTML table using AJAX: (shown in the image below) If I click on any cell in the table, except for headers c1 through c4, I want to display a cover hiding the cells to the left of the clicked cell: (as shown in the image be ...

Chrome is the only browser that displays the correct number of columns, unlike IE and Firefox

[Link removed] Is anyone else experiencing an issue with the columns on a website layout? I have 5 columns set up with each post taking up 20% width. It looks fine in Chrome, but in IE and Firefox, the last column gets pushed below so there are only 4 col ...

Animate your menu on hover with jQuery!

I am experiencing an issue with centering the background image below a link. When using mouseleave, the image does not return exactly to the center of the list. Any assistance on how to resolve this would be greatly appreciated. Thank you! Here is the ref ...

Adjust text size based on device orientation changes

I'm struggling to dynamically change the font size based on screen orientation and width. How can I adjust the font size when switching between landscape and portrait mode? I attempted using an event listener, but it's not updating the font size. ...

`I encountered difficulty in finding the radio button element using Selenium WebDriver`

I am a beginner trying to grasp automation using the tool Selenium. My goal is to automate operations on this website - Specifically, I am attempting to log in and interact with a radio button (for selecting one way or round trip) in the flight finder sec ...

Elevate the Appearance of Material UI Elements with custom CSS Sty

I am currently facing an issue while trying to customize the styling of a Material UI component using CSS. Here is the code snippet: <IconButton className="my-class"> <Close /> </IconButton> CSS: .my-class { float: right ...

Align bootstrap button to the center on xs screens

I've spent countless hours on this issue - I just can't seem to get the button centered properly in xs mode. It keeps aligning itself based on the left side, no matter what I try. I've searched through Bootstrap but couldn't find a clea ...

Steps for selecting the Check Interactions button with Selenium

My current project involves using the website drugs.com to check for interactions between two drugs. I have entered the names of two drugs into the search box on this site and I need to click on the "check interactions" button to view the interaction resul ...

I attempted to apply vertical-align and color properties to the static_nav div, but they didn't have any effect. Could this be due to its placement within the header? What steps should I take to resolve this issue?

<!DOCTYPE HTML> <html lang ="en"> <meta charset = "utf-8" /> <head> <title>Sample Page</title> <link rel="stylesheet" type="text/css" href="Web_Design_01_Stylesheet.css" /> </head> <body> &l ...

What is the best way to include an external JavaScript file in a Bootstrap project?

I am brand new to front-end development and I'm attempting to create an onClick() function for an element. However, it seems like the js file where the function is located is not being imported properly. I've tried following some instructions to ...

how to change the class of a div when clicking on a <p> element

Check out this code snippet: <p class="faq" onclick="toggleVisibility('answer1');"> How can I restrict email sending on my website? </p> <div id="answer1" class="hide"><sometext></div> these are my CSS styles! ...

Disabling hover effects in Chart.js: A step-by-step guide

Is there a way to disable hover effects, hover options, and hover links on a chart using chart.js? Here is the code I have for setting up a pie chart: HTML.. <div id="canvas-holder" style="width:90%;"> <canvas id="chart-area" /> </div ...

Error in regular expression format

I want to extract data from a JSON format like this: {"response":[9816,{"vid":166941761,"owner_id":-460389,"title":"Хочу спиться!","description":"Вкусная группа БОРЩ - http:\/\/vk.com\/borsch<br\/ ...

What is the best way to create a slideshow using an IFrame?

Currently seeking a solution for an iframe slideshow that can smoothly transition between multiple HTML iframes. I have a total of 5 iframes that need to rotate automatically and continuously. Any suggestions on how to build a lively and dynamic iframe sl ...

Having trouble getting the img src to work in Django 2.1 template?

I'm having trouble displaying images in my Django template file. Despite uploading the images to media static files, they do not appear on the template. When I click on the image link in the Django admin page, it shows a "Page not found(404)" error me ...

Incorporating a header include on every page throughout my website

Is there a way to include headers and footers in your HTML web pages without having to duplicate code on every page? I understand that this can be done using PHP, but what if you are creating a website solely with HTML? If there is no solution to avoid r ...

Choosing particular contenteditable divisions using jQuery

Consider the following HTML structure for a specific type of blog post editor: <div class="entry"> <div class="title" contenteditable="true"> <h2>Title goes here</h2> </div> <div class="content" contenteditable ...