Selenium specifically targets and interacts with the immediate child elements of a WebElement

Is there a way to select only the direct children of a WebElement using cssSelector?

webElement.findElements(By.cssSelector("> li");

I want to specifically target the immediate children of an element.

Although I am familiar with the css selector "parent > children" which works fine, my scenario is different.

Consider the following HTML structure:

<ul class="x">
    <li>
        <ul>
            <li>
            </li>
        </ul>
    </li>

    <li>
    </li>
</ul>
List<WebElement> webElements = driver.findElements(By.cssSelector("ul.x > li"));

This code snippet retrieves only the direct sub-children of the ul tag, resulting in a List size of 2. However, I encounter a different scenario:

WebElement webElement = driver.findElement(By.cssSelector("ul.x"));
webElement.findElements(By.cssSelector("li");

The issue here is that this code fetches all 'li' elements instead of just the direct children. This time, the List size is 3.

I attempted the following:

webElement.findElements(By.cssSelector("> li");

but unfortunately, it resulted in an error:

org.openqa.selenium.InvalidSelectorException: invalid selector: An invalid or illegal selector was specified

Answer №1

To locate elements, use xpath:

WebElement element = driver.findElement(By.cssSelector("ul.x"));
element.findElements(By.xpath("./li");

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

Selenium can be inconsistent when it comes to sending send_keys

I'm currently working on a script to login to Facebook, search for a specific webpage using the search bar, and select the top result. However, there seems to be an issue with the script not always sending keys to the search bar and failing instead. A ...

Is it possible to identify the beginning of a download using Selenium?

Currently, I am using Python and Selenium to download a large batch of files. To ensure that each file is successfully downloaded, I am implementing a basic time.sleep() function, but I want to enhance efficiency and guarantee the completion of each downlo ...

Adding a Ruby/Selenium value to an array

Can anyone assist me with Ruby/Selenium? I have several elements on the page structured like this: <a class="Name" title="Migel" href="/profiles/users/_1324567980000000c/">Migel</a> I would like to extract the HREF values into an array. Whe ...

Displaying jsp variable in the browser console

When working in JSP, using <p>${ob}</p> to retrieve an object can be challenging due to the size of the object. Is there a way to utilize JavaScript to print it instead? Keep in mind that since this involves handling email content, JavaScript ...

What is the best way to detect and handle JavaScript redirects in Selenium testing?

How can I capture all page redirects performed in JavaScript? For example, consider a web page using window.location for redirection: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8> <title>Redirect JS< ...

The ajax call is not yielding a successful response, consistently redirecting to the error function

My attempt to retrieve JSON data from a servlet is resulting in an error block response for every call, even though the backend is providing the correct response. Here is my JavaScript code: $(document).ready(function() { $("#submit").on("click", fun ...

Issue encountered: Module 'jasmine-expect' not found [Protractor]

I am facing an issue while trying to execute a protractor test that connects to my application. Upon running the command (git bash/terminal): protractor conf.js An error is displayed as follows: " Error: Cannot find module 'jasmine-expect&apo ...

Having trouble installing Webdriver Driver Manager 2.7.0 through Nuget on .Net framework 4.5

When attempting to install the WebDriverManager reference from the Nuget Package Manager in my code, an error occurs. I have attempted to update and degrade the .Net framework but without success. Is it possible to use WebDriverManager 2.7.0 with .Net fram ...

Verifying a parameter from a request URL using Selenium - A step-by-step guide

I found myself on the following webpage: http://localhost:8080/login?error=true How do I verify if the error variable contains the value true? https://i.stack.imgur.com/F5TkF.png This is what I have tried so far and failed: https://i.stack.imgur.com/3 ...

Double Serialization with Java's Jackson Library

In my code, I have a class that includes a String field and a Map field. My goal is to serialize this class into JSON using Jackson. public class Mapping private String mAttribute; @JsonIgnore private Map<String, String> mMap; @J ...

Prevent the "Disable Developer Mode Extensions" Popup from Appearing When Installing an Extension in Selenium

I am looking to prevent the "Disable Developer Mode Extensions" popup from appearing, while still using an extension mentioned in this thread keep "Disable developer mode" closed while adding extension Since no solution was provided, I am also a ...

Every time I try to automate the code, the browser opens briefly before abruptly closing with an error

Oops! Looks like the session could not be created. The ChromeDriver version you are using only supports Chrome version 83. Here are some details about the build: Version: '3.141.59' Revision: 'e82be7d358' Time: '2018-11-14T08:17:0 ...

Implementing Ajax for displaying panels in Apache Wicket framework

I am currently learning the Wicket framework and I encountered an issue while working with AJAX. I attempted to display a panel with the selected radio button's company name, but unfortunately, I'm experiencing an error. I have included all the r ...

Java: Wait for the website to finish loading before continuing

When using Selenium to open a website and extract text via XPath, I've noticed that I can only get the text if I wait for the entire website to load. It would be better to check if the website is fully loaded before extracting the text. I believe chec ...

Begin the execution of JMeter using a JUnit test

Can a recorded JMeter script, used to test the load of multiple user actions, be integrated into a Selenium/JUnit test case? I want to run the Selenium/JUnit test case with Java and receive performance results in the JUnit report, but most resources only ...

Strategies for styling the contents within <details> while excluding <summary> in CSS

I'm attempting to apply a 10px padding to the Story box using CSS, without introducing any additional HTML elements into the website code. Is there a way to include the padding in the Story box without incorporating a new HTML element? To clarify: H ...

pursuing the team (without achieving the expected components)

My attempt to scrape data from a website using Selenium is facing an issue. Despite removing the text that contains "team," Selenium fails to detect this element. Why is it not grabbing all the elements following "team"? Is there a better way or a method l ...

The Magento TAF encountered an error: "curl_init() function is undefined."

Currently, I am attempting to integrate Selenium into Magento TAF within Netbeans using a Wamp Server. However, when I execute the test, I encounter the following error: Call to undefined function curl_init() in C:\wamp\bin\php\php ...

Tips for navigating the cursor in Selenium?

My goal is to replicate mouse movements along a simulated curve line or parabola in order to mimic natural user behavior on websites. While I am familiar with using Selenium to click on elements, this method does not accurately simulate human interaction ...

Tips for deactivating a Chrome extension using Selenium

Is there a way to prevent chrome extensions from starting up every time I run my Selenium Chrome code? Example Code public static void main(String[] args) throws IOException { System.setProperty("webdriver.chrome.driver", "C:\\ ...