Choosing specific Nth-of-type elements using selenium

Trying to use By.cssSelector to capture the nth dom element with class c3 in a structure similar to this:

<div class="c1">
    <div class="c2">
        <div class="c3">...</div>
    </div>
</div>
<div class="c1">
    <div class="c2">
        <div class="c3">...</div>
    </div>
</div>
<div class="c1">
    <div class="c2">
        <div class="c3">...</div>
    </div>
</div>

Struggling with CSS selector testing, particularly when using pseudo-class :nth-of-type().

This selector works for the 2nd instance of c2/c3:

.c1:nth-of-type(2) 

However, the following do not select anything:

.c2:nth-of-type(2)
.c3:nth-of-type(2)

In Selenium, these selectors also yield no results. While there are other ways to identify these elements (such as XPATH), understanding :nth-of-type() continues to be a challenge. Seeking insights on why the latter two selectors fail or clarification on the usage of this pseudo-class.

Encountering this issue on Chrome (29/30) and Firefox (24/25).

Answer №1

Not entirely certain which specific element you're trying to target, but I recommend experimenting more with the :nth-* pseudo-classes in CSS. Here's an example of a CSS selector that selects all three elements with the class "c3" using nth-child():

div.c1 div.c3:nth-child(1)

As I mentioned before, it would be helpful for you to clarify which element you are aiming to select.

I'm struggling to understand why the second two examples aren't working with nth-of-type. Can someone explain where my understanding is lacking or help correct my confusion regarding this selector?

Remember that all the :nth-*() pseudo-classes rely on their parent elements. Let me break down your selector:

.c1:nth-of-type(2)

This selector targets any element with the class "c1" that is the second child of its type.

In your scenario, the parent element was likely the <body>, so...

<body>
  <div .c1 />
  <div .c1 /> // This one is highlighted as the second child of type ".c1"
  <div .c1 />
</body>

Now let me explain why your other selectors are not functioning as expected.

Both .c2:nth-of-type(2) and .c3:nth-of-type(2) also depend on the parent element. Since you have specified a parent, it expects the <body> to be the parent. In reality, the actual parent is the <div .c1 />. These selectors are actually looking for the DOM structure like this:

<body>
  <div .c1 />
  <div .c2 /> // This **would** be the second nth-of-type, but it's not how it works.
  <div .c1 />
</body>

Experiment with different CSS selectors and pseudo-classes at . It's beneficial to actively practice and experiment on your own to enhance your understanding. You'll get the hang of it!

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

Adjust the default dimensions of the background image

My CSS file includes the following code: input.myInput { background-image:url('search.png'); background-size:120px 120px; } In my HTML file, I have the following input element: <input type='text' class='myInput' > I ...

Android webview encounters issues with loading javascript and css files

I am facing an issue with my Angular web app that I want to run locally inside a WebView of my android app. However, upon opening the app, all I see is a blank screen. Upon inspecting the chrome tool, I noticed a net::ERR_FILE_NOT_FOUND error for the .js ...

Automate the login process on a website by utilizing saved passwords in the browser with Selenium

I am facing an issue with using Selenium to select my saved logins and passwords in Firefox. I have successfully specified my profile and instructed Selenium to create a temporary profile with the necessary files, but when I try to login with my saved cr ...

I'm having trouble locating the JDK that is supposed to be installed on my Mac

After updating my Mac to Java 8 version 73, I am unable to locate the installed path. Previously, I had Java 8 update 65 already installed on my system. Running /usr/libexec/java_home only displays the path for my current JDK (/Library/Java/JavaVirtualMac ...

What is the best way to vertically align a Material UI component to occupy the remaining space within a container

Issue with Material UI/Grids for Login Page As a newcomer to Material UI/Grids, I am currently working on creating a login page where the layout is split into two parts. The left side occupies 5 column spaces while the right side takes up 7 column spaces. ...

Assignment on Ionic's Cascading Style Sheets classes

As I work on styling my app, I find myself struggling with the extra CSS classes that are added when I preview the source in a running app. It's particularly confusing when all I want to do is change the menu header background. In my app.html file, I ...

Tips for aligning an image on top of another image

Here is the code I am working with (also pasted below). My goal is to create a layout with two columns, where the first column features two images and the second column displays some text. In the first column, I specifically want the first image to have a ...

CSS: Eliminate unnecessary space at the top by implementing a margin and padding reset

I am puzzled by the presence of approximately 10px of whitespace at the top of my HTML file. Despite setting margin and padding to 0 in the "body" section of my CSS, the whitespace persists. Any suggestions? Thank you! Update: I discovered that removing t ...

What methods can be used to apply the styles of one CSS selector to another?

When using Bootstrap CSS to ensure consistent button design on my site with the class "btn btn-primary," I encountered a dilemma. A plugin I'm utilizing has its own button class named "ethpress-metamask-login-button." Is there a way to make the "ethp ...

The retrieval of data by JavaScript occurs before the completion of the Primefaces AJAX event

When using a TabView component in PrimeFaces, I am trying to make an ajax call on tabChange to update the bean data and then process it with JavaScript for generating charts. The issue I'm facing is that the data gets updated correctly via the ajax c ...

The bottom of the page refuses to remain anchored

I've exhausted all possible solutions and my footer just refuses to stay at the bottom of the page. Working with Opencart has made it challenging for me to pinpoint the root cause, leaving me utterly perplexed. The issue persists on pages with minim ...

Hover over image with Selenium WebDriver

Update: Firefox version: 19.0.2 Selenium WebDriver version: 2.31.2.0 Is there a way to confirm that image hover functionality is functioning properly using Webdriver? I have five stars, each representing a different title, and I am trying to find a metho ...

What is the process for arranging the elements of a one-dimensional array in a two-dimensional array in descending reverse order using Java?

Struggling with my class assignment, I find myself at a crossroads. While I'm gaining a better understanding of how everything fits together, I can't seem to pinpoint exactly what needs to be changed. Despite trying numerous variations, none have ...

Choose a sibling element using CSS styling

As I'm developing an AngularJS component, I am on the quest to identify ANY sibling of a particular tag, irrespective of whether they are div, span, or p. I'm hoping for a CSS-native solution that resembles something along the lines of div:siblin ...

"Crafting a ScalaZ Disjunction in Java: A Step-by-Step Guide

What is the purpose of writing tests for the Java-Scala Adaptor class? Is there a way to create left and right disjunction for \/[String, Int] in Java programming language? ...

There is no file or directory found in Java on Ubuntu

Upon receiving an error message stating "no such file or directory," I checked my /usr/lib/java/jdk1.8.0_144/bin folder as shown in the image below: https://i.sstatic.net/nT0qI.png My .bashrc file contains the following entries: #Java HOME directory setu ...

Cannot close the Bootstrap dropdown after selecting an option

I am encountering an issue with a dropdown list that has a select feature using Bootstrap 3.4.1. The problem is that the dropdown remains open after selection and does not close unless I click outside of the list. Here is the HTML code: <div id="c ...

Problem with using Jquery DIY slider for multiple purposes

There seems to be an issue with the DIY Slider that I'm using. Whenever I attempt to use this slider more than once on a single page, the second one fails to function. I have exhausted all possible solutions that come to mind. $(".slider").diyslider ...

Unable to capture a screenshot using the Selenium driver on a Mac machine due to Capybara issue

Below is the configuration I have set up for Capybara: require 'selenium/webdriver' # Configuring Capypara with javascript client Capybara.register_driver :chrome do |app| Capybara::Selenium::Driver.new(app, browser: :chrome) end Capybara.re ...

I'm facing a problem with the margin and display setup in my assignment

I am encountering a bit of confusion regarding the margin when implementing the following code: Section { background-color: #FFFFFF; width="100%"; } p1 { font-family: Roboto; font-size: 22px; font-height: 1px; margin-top: 45px; margin-lef ...