Tips for selecting a nested div that contains specific text and clicking on it with Cypress

Here is the HTML code I am working with:

    <div class="main">
        <div class="box">
            <div class="cell">checkbox</div>
            <div class="cell"></div>
            <div class="cell">
                <a>link1</a>
            </div>
            <div class="cell">type</div>
            <div class="cell">description</div>
        </div>
        <div class="box"> //i want to click this element
            <div class="cell">checkbox</div>
            <div class="cell"></div>
            <div class="cell">
                <a>link2</a>
            </div>
            <div class="cell">type1</div>
            <div class="cell">description1</div>
        </div>
        <div class="box">
            <div class="cell">checkbox</div>
            <div class="cell"></div>
            <div class="cell">
                <a>link3</a>
            </div>
            <div class="cell">type2</div>
            <div class="cell">description2</div>
        </div>
    </div>

I want to specifically click on the box div that contains the link with text <a>link2</a>.

I attempted to achieve this with the following code:

    cy.get(`.main>div`).contains('link2').click();

However, this clicks directly on the link element itself rather than the surrounding div. Is there a way to target and click on the desired div containing the link instead?

If you have any advice or solutions for this issue, I would greatly appreciate it. Thank you!

Answer №1

To achieve the desired action of clicking on the specific div containing 'link2', you can target the parent element by selecting it within the '.main' class:

cy.get('.main > div').contains('link2').parent().click();

Answer №2

In my opinion, utilizing a selector within the 'contains' function is feasible as demonstrated below:

cy.contains('.main>div', 'link2').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

What is the best way to reference a div link from a different PHP file?

Struggling to access a page div from another php file, I've tried calling it using (found online) admin.php#changepass Here's an example of my HTML code: <div id="changepass" class="w3-container city" style="display:none"> <form name ...

What is the process of adding a source map for a CSS minification script generated by a gulp task?

Currently, I am focused on optimizing CSS minification through the use of gulp. An issue arises when trying to debug CSS, as all stylesheets are contained within knx.dev.css. It is necessary to implement a solution that allows for easy tracing back to the ...

Attempting to eliminate the padding from the unordered list (ul) element within the pop-up box

Upon clicking the chip with chipName="button test IPA", a popup window appears. I am attempting to remove the padding from the ul tag within that popup. The issue I'm facing is that I cannot locate the ul tag in my HTML or JSX code. I have assigned a ...

HTML: The art of aligning elements within a header

I've been working on creating my personal HTML record, but I'm facing one last challenge that I can't seem to overcome. I want to design my header like this: https://i.stack.imgur.com/FU8EJ.png With the following elements: TEXT SUMMARY wi ...

Ways to customize the design of a bootstrap button using jQuery

<a class="btn btn-small btn-block btn-warning" type="button" id="999">&nbsp;</a> Looking to customize the button style? Check out http://getbootstrap.com/2.3.2/base-css.html#buttons I'm interested in changing the button's style ...

Extracting information from websites through parsing

I am looking to create a crawler in Java that can navigate through web pages and extract specific information from the content. Can anyone provide guidance on how I can start building this type of crawler? For instance, how could I retrieve the statement ...

Tips for animating an element in a straight line while also rotating it using CSS

Whenever I apply the transform property to an element for translate3d and rotate3d, it ends up orbiting around. My goal is to have a linear motion while rotating. I have utilized webkit animations in CSS img{height:50px; width:50px; animation:tt; ...

Determine the percentage of one color in relation to another color using AngularJS

I'm trying to make a div element change color based on another div element's color. For example: <div style="background-color:{{main_color}};"> In a separate div, the color should be 70% lighter than the main color. <div style="backg ...

How to retrieve the image source using jQuery

<img src="../img/arnold.png" alt="Arnold"> Is there a way to retrieve the absolute path of this image using jQuery? Currently, when I use img.attr("src"), it only returns "../img/arnold.png", but I need it to return something like "http://site.com/ ...

Creating a dynamic and adaptive horizontal SVG line for your website

Understanding how SVGs function is still a bit unclear to me. I know that the viewBox property plays a role in scaling SVGs, and I have come across advice suggesting not to specify height and width attributes when creating responsive SVGs. While there are ...

How can I dynamically adjust the font size of content in a React Material-UI Button when it's unexpectedly

In my web application project, I have created multiple choice questions where each answer is displayed within a single button: <Button fullWidth={true} variant="contained" onClick={(answer) => this.handleAnswer(this.state.answers[0].tex ...

What steps should I take to resolve the CSS layout issue specifically on iPhone Safari browser?

It's functional on Chrome desktop However, it has issues on Safari iOS Here is the CSS code snippet .button { border: 1px outset $color3; background: $color1; color: $color4; font-size: 80%; // Reduced size due to uppercase text ...

flexbox with a table that can be scrolled vertically

I am currently working on creating a scrollable tbody within a flexbox layout. I have two flexboxes, each containing a table. When the items in the table overflow, I want to display a vertical scrollbar within the tbody. However, the tables are within f ...

Unlocking the power of video.js integration with create-next-app in Next.js

I am currently having some difficulty with integrating Video.js in my create-next-app project, specifically in loading the video.js.css file. Here is a snippet of how my component is structured: import videojs from 'video.js' import videoStyles ...

Homepage featuring a stacked image background similar to Kickstarter

I am trying to replicate the stacked image effect seen on kickstarter.com. However, I am facing an issue where the images do not overflow on the screen as desired. The arrow indicates the area that needs to be filled with the image. Check out the image he ...

Discover an effective way to display Ajax search results above a different div element!

I am having trouble with positioning my search results on top of other divs. To see the issue, you can watch the screen recording by clicking on this link: https://drive.google.com/file/d/1kU_vl2ZAmSCok0pxnTTvY hacxqcZZ9m_/view?usp=sharing These are the s ...

Challenges with organizing content using spans in Bootstrap 3

Looking for some help with TB3 here. I've created a jsFiddle to showcase my attempt at building a small minitron, which is essentially a mini jumbotron. https://i.sstatic.net/VxruB.png The idea is to have each 'minitron' contained within a ...

Distinguishing between fonts on a website and Invsion (a tool utilized by designers and front end developers)

I am having difficulty achieving the exact look of our website font as intended by the designer. We have utilized Google's 'Source Sans Pro' font file. I have attempted various strategies: Using the font directly from Google [ Downloading ...

Experiencing excessive CPU usage while utilizing a progress bar in Angular

Whenever I try to load a page with 2 progress bars, my CPU usage goes through the roof... I decided to investigate and found that once I removed them, the site's speed improved significantly. Here's a code snippet of one of the progress bars: ...

Ensure that two cells within the same row of two tables are equal in width

I have 2 tables in the same row of a large table, set up like this: <table> <tr> <td>Type</td> <td>Storage 1</td> <td>Storage 2</td> </tr> ...