Tips for creating a CSS selector for a button

<div class="test" data-credit-card-index="1">

    <button class="test1 test">Delete</button>
    <button class="test1 test">Edit</button>

I'm trying to create a CSS locator specifically for the Edit button within the [data-credit-card-index="1"] segment, which is part of a section containing multiple credit cards. So I need to combine [data-credit-card-index="1"] with Edit in order to accurately locate my selector.

Can anyone provide assistance with this? Thank you so much!

Answer №1

Recent Update

For working with Selenium:

By.cssSelector('.test[data-credit-card-index="1"] button.test:eq(1)')

Alternatively, using XPath:

By.xpath('div[@data-credit-card-index="1"]/button[contains(text(),"Edit")]')

Prior solution provided (with jQuery)

To start with:

$('.test[data-credit-card-index="1"] button:contains("Edit")');

Next:

$('.test[data-credit-card-index="1"] button.test:eq(1)');

A more recommended approach would be to introduce an additional class if feasible:

<div class="test" data-credit-card-index="1">

    <button class="test1 test delete-button">Delete</button>
    <button class="test1 test edit-button">Edit</button>

following by utilizing:

$('.test[data-credit-card-index="1"] .edit-button');

Answer №2

If you're working with jQuery, one practical approach is to utilize the :contains selector:

$("button:contains('Edit')")
// or
$('[data-credit-card-index="1"]').find("button:contains('Edit')")

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

Adding a luminous glow effect to the <a> tag upon selection

Currently, I am working on integrating a navigation bar into a test site. I have noticed that some navigation bars highlight the current selected page by adding a bar underneath or creating a glowing effect around the <a> tag. I am unsure of the corr ...

What is the best way to increase the padding in a Colorbox modal window?

Is there a way to increase the padding in a Colorbox modal window? I want some extra space between the edges of the modal window and the actual content. I experimented with the innerWidth and innerHeight properties, but unfortunately, I didn't notice ...

Customizing jquery mobile styling

I am dealing with a table where the challenge lies in aligning the text to center in the header. .info-header { font-size: small; text-align: center; } Despite setting the alignment, the row still inherits left alignment from jQuery mobile. Seeking ...

IntelliJ Issue: NullPointerException in Java

I am a newcomer to Selenium and I am using IntelliJ, Selenium WebDriver, JUnit. My issue is with the java.lang.NullPointerException that I am encountering while working on my project. Let's start by looking at the HomePage page: package PageObjectPa ...

Change the location of an HTML element within the page structure

Let's consider having 3 elements like this: <h1>Hello</h1> <p>hello</p> <h1>Hello</h1> I am looking to remove the second <h1> as I only want it to appear once on the page. However, I would like to have flexib ...

Rotation using CSS in Internet Explorer 8

Can anyone help me with a CSS solution for rotating elements in IE8? I've tried some solutions that claim to work in IE8, but they're not working for me. What am I doing wrong? Here's what I've attempted: <!DOCTYPE html> <htm ...

Encountering an error while attempting to update the selenium version: [debug] [MJSONWP] Bad parameters: BadParameters

Everything was running smoothly with the Automation suit until the selenium upgrade to version 3.9.1. After the update, I encountered the following error while initializing the Android driver. [HTTP] --> POST /wd/hub/session {"desiredCapabilities":{"ap ...

The XPath element number function retrieves all elements instead of just the first one

When attempting to access a link using the code below (for phpunit/selenium): //td[normalize-space() ='Test title 2']/following-sibling::td[3]/a[.='delete'] An XPath checker in FireFox indicates there are 7 elements (matching "test ti ...

What steps can I take to ensure my header appears perfectly aligned? (HTML/CSS)

My header on my website is all messed up with the links appearing incorrectly. I suspect it's an issue with style.css, so I attempted to modify the style.css file but had no luck. I have very limited experience with .css and cannot seem to figure out ...

Having difficulty accessing the link button in an email template using Selenium WebDriver

How can I use Selenium WebDriver to identify and interact with links in email body templates? I've been working on automating a process where I need to click on a link within an email body to complete a registration. However, when using a public mail ...

The Ineffectiveness of the Form Class

I have been trying to personalize the appearance of my bootstrap form by applying my own custom CSS. However, despite creating a class for my form and making changes to its CSS properties, the style of the form remains unchanged. Take a look at my form: ...

Embed video with a custom thumbnail in an iframe

Hello everyone! I'm a first-time user on stackoverflow seeking some help. Currently, I am using Dreamweaver to work with HTML and CSS in order to build a website. My task involves sourcing a video from a television archive and embedding it onto my si ...

Generate an adjustable grid layout (with rows and columns) to display information retrieved from an API request

I have recently started learning React JS and JavaScript. To practice, I am working on a small project where I am facing an issue with creating dynamic rows and columns. My aim is to display data in 4 columns initially and then move to a new row and column ...

Prevent the movement of the first column in a table when rearranging rows up or down by utilizing

Feeling a bit stuck here, can't seem to figure out what I've missed. I've managed to move up or down a row in a table, but I need the value of the cell in the first column to remain unchanged. Here's my code: This is my HTML file: &l ...

An error has been identified in the script while attempting to extract names and addresses from the generated results

I have developed a script to extract the names and addresses of various results (56 in this scenario) from a website. The addresses are only visible once a click is triggered on each result. However, when executing the script, I am able to obtain two resul ...

Angular 8 is facing an issue where classes defined dynamically in the 'host' property of a directive are not being properly applied to the parent template

In my Angular 8 application, I am working on implementing sorting using the ng-bootstrap table. I referred to the example available at . In the sample, when I hover over the table header, it changes to a hand pointer with a highlighted class applied as sho ...

Implement responsive data tables by setting a specific class for hiding columns

Having trouble assigning a specific class name to individual columns in datatables? It seems that when columns are hidden using the responsive extension, the desired class is not applied. Looking for a solution or workaround. Check out this example from D ...

Obtaining a list of child span elements using xpath in Selenium

I am looking to retrieve all child span elements based on the following xpath //label[@for='someText']/span Expected result: <span class="someClass">"Find this text"</span> <span id="someId">" ...

What is the reason for the additional line being generated?

Can anyone explain why there is an extra line being produced here? I came across another question where it was mentioned that this issue is not due to CSS but rather caused by HTML. Why is that? This is completely new to me, and I'm curious as to wh ...

Python Error: Selenium element cannot be clicked at current position

Currently, I am in the process of creating an automated test using Python with Selenium. While I have successfully navigated through multiple pages, I have encountered a roadblock when attempting to click on a specific button on one page. The snippet of c ...