SeleniumHQ / selenium

A browser automation framework and ecosystem.
https://selenium.dev
Apache License 2.0
29.77k stars 8.02k forks source link

[🐛 Bug]: Selenium Using Css Selector with tilde signature is throwing org.openqa.selenium.InvalidSelectorException: invalid selector: #14102

Closed vlaks85 closed 1 week ago

vlaks85 commented 3 weeks ago

What happened?

[🐛 Bug]: Selenium Using Css Selector with tilde signature is throwing org.openqa.selenium.InvalidSelectorException: invalid selector: Below is the locator I'm using with CSS Selector but its throwing Invalid or illegal selector - div[data-testid^='Item_asin'], ~ span[data-testid^='Item_asin']

org.openqa.selenium.InvalidSelectorException: invalid selector: An invalid or illegal selector was specified (Session info: chrome=108.0.5359.124) for the locator {Using=css selector, value=div[data-testid^='Item_asin'], ~ span[data-testid^='Item_asin']}

I want use CSS selector for my use Case. Any way to handle the ~ or + to get the siblings using CSS Selector.

How can we reproduce the issue?

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

import java.util.List;

public class Main {
    public static void main(String[] args) {
        System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");
        WebDriver driver = new ChromeDriver();

        driver.get("http://yourwebsite.com");

        // Adjust the selector based on your HTML structure
        List<WebElement> elements = driver.findElements(By.cssSelector("div.lineitem-container ~ span[data-testid^='item']"));

        for (WebElement element : elements) {
            System.out.println(element.getText());
        }

        driver.quit();
    }
}

Relevant log output

org.openqa.selenium.InvalidSelectorException: invalid selector: An invalid or illegal selector was specified
  (Session info: chrome=108.0.5359.124) for the locator {Using=css selector, value=div[data-testid^='Item_asin'], ~ span[data-testid^='Item_asin']}

Operating System

Ubuntu

Selenium version

Java 11

What are the browser(s) and version(s) where you see this issue?

108 Chrome

What are the browser driver(s) and version(s) where you see this issue?

chrome=108.0.5359.124

Are you using Selenium Grid?

No response

github-actions[bot] commented 3 weeks ago

@vlaks85, thank you for creating this issue. We will troubleshoot it as soon as we can.


Info for maintainers

Triage this issue by using labels.

If information is missing, add a helpful comment and then I-issue-template label.

If the issue is a question, add the I-question label.

If the issue is valid but there is no time to troubleshoot it, consider adding the help wanted label.

If the issue requires changes or fixes from an external project (e.g., ChromeDriver, GeckoDriver, MSEdgeDriver, W3C), add the applicable G-* label, and it will provide the correct link and auto-close the issue.

After troubleshooting the issue, please add the R-awaiting answer label.

Thank you!

github-actions[bot] commented 3 weeks ago

We need more information about this issue in order to troubleshoot.

Please turn on logging and re-run your code. Information on how to adjust logs for your language can be found in our Troubleshooting documentation.

titusfortner commented 3 weeks ago

What version of selenium? Turn on logging and we can see which component complains.

vlaks85 commented 3 weeks ago

Build info: version: '3.141.59'

titusfortner commented 3 weeks ago

We can't do anything about older versions, please try with Selenium 4.21

github-actions[bot] commented 1 week ago

This issue was closed because we did not receive any additional information after 14 days.