css How to target a specific row and column within a table using css selector in Selenium WebDriver

When attempting to locate

css=table#Salarytable.dataTable.3.4 

in Selenium IDE, the cell highlights correctly. However, when using this locator in my Selenium code...

String salary=driver.findElement(By.cssSelector("table#Salarytable.dataTable.3.4")).getText();

I encounter an error stating that an illegal string is present. EDIT: The HTML code is as follows:

<table id="Salarytable" class="dataTable" cellspacing="0" border="0" style="border-collapse:collapse;">

The stacktrace reads:

java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    (...)
    at org.openqa.selenium.remote.ErrorHandler$UnknownServerException: An invalid or illegal string was specified
    Build info: version: '2.35.0', revision: '8df0c6b', time: '2013-08-12 15:43:19'
    System info: os.name: 'Windows 2003', os.arch: 'x86', os.version: '5.2', java.version: '1.6.0_35'
    Driver info: driver.version: unknown

I understand how to achieve this using XPath, but I specifically want to utilize CSS. How can this be accomplished? EDIT: Currently, I have implemented "table#Salarytable > tbody > tr:nth-of-type(3) > td:nth-of-type(4)" in my WebDriver code and it functions properly. However, I am puzzled as to why a locator (css=table#Salarytable.dataTable.3.4) that worked in Selenium IDE does not work in WebDriver code. Thank you.

Answer №1

Learn more about CSS class naming conventions

In the discussion, it was pointed out that a CSS class name cannot begin with a number and must consist of more than just a single digit. The selector you used (table#Salarytable.dataTable.3.4) suggests that the HTML table tag should be structured like this:

<table id="Salarytable" class="dataTable 3 4">
. However, this structure is not valid, as '3' and '4' are not allowed within HTML classes. Therefore, the correct selector should simply be table#Salarytable.dataTable.

It is also recommended to target the table by its id only:

driver.findElement(By.id("Salarytable"))

UPDATE I may have misconstrued the rules regarding naming conventions. Referencing the CSS Grammar (section G3), it appears that numbers can indeed be included in a class name, but they must be properly escaped when referenced (as explained in the linked document). Unfortunately, my knowledge of CSS is limited to further elaborate on this topic.

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

Guide to transforming a vertical tabbed content panel into a responsive collapsible using media queries and jQuery

I am in the process of creating a new content navigation design that incorporates vertically stacked tabs to toggle hidden panels adjacent to the tabs. Unfortunately, this layout seems to encounter issues at narrower screen widths. Check out my work on Fi ...

Creating a JavaScript alert in Selenium Java WebDriver with a concise message

While running a Selenium Java program, I am attempting to create a JavaScript alert window with a specific string message. I came across a method that involves executing JavaScript within Selenium by interacting with hidden elements: WebDriver driver; Java ...

Adjust the size of a collapsed element in a bootstrap accordion panel

I am working with a classic bootstrap accordion in my code. I want to adjust the width of the expanded ul/li elements when a h5 element is clicked (decrease width). Currently, the width expands fully when the h5 element is clicked. Can anyone provide ass ...

how to position one div above another using css

I currently have a page layout with 7 different div elements structured like this; <div id="container"> <div id="head"></div> <div id="A"> <div id="A1">A1</div> <div id="A2"></div> ...

Images that are automatically generated cannot be interacted with by clicking on them

I currently have 4 images loaded onto my website. Whenever I click on one of the images, it becomes highlighted as desired using the following code: JavaScript Function 1 : var ImageSelector = function() { var imgs = null; var selImg = null; retu ...

Issue encountered while attempting to log in using Webdriver (Python Selenium)

I've been attempting to scrape data from hellofresh.fr using Python Selenium, but I'm encountering issues right from the start during the login process. After clicking on the submit button, an error message pops up in the browser (loosely transl ...

Troubleshooting issues with JPanel and JScrollPane rendering

Despite my limited English skills, I'll do my best to explain my question. In my JPanel, I am drawing lines that should extend beyond the panel area and be displayed in full by the JScrollPane. However, the JScrollPane is not updating properly and is ...

What is the process for retrieving a txt file and comparing it with an array to create a dropdown list using webdriver in java?

Let's imagine this scenario: 1) Start by creating a file with the input string "Sep 2015". 2) Gather the drop-down list options into an array. 3) If the array matches the string in the file, exit the loop. Otherwise, download a new month report and o ...

What is the process for changing a matrix into a string array?

Suppose the values provided are as follows: A: [2, 0, 2, 0, 1, 0] B: [0, 1, 0, 0, 0, 1] C: [0, 1, 0, 1, 1, 0] D: [0, 0, 0, 1, 0, 1] These values indicate the occurrences of characters (A, B, C, D) in a two-row character matrix where each row consist ...

Determining the background image size of a div when the window is resized

I'm facing a problem that I can't seem to solve. I have a div with a background image. <div class="a"></div> I want to make a specific point of this background image clickable. I know I can achieve this by adding a div with a z-inde ...

Pass a parameter to an Angular class

I am encountering difficulties in inputting a value into an element with a ng-class Attempted the following code snippet: driver.FindElement(By.Id("limit")).SendKeys("10.00"); without success. I also tried - driver.FindElement(By.Id("limit_display")).Sen ...

Some websites are experiencing issues with incomplete HTML responses when using Requests & BeautifulSoup or Selenium

When attempting to extract information from various URLs using Requests and BeautifulSoup in Python, I encountered a problem where some websites returned only a partial HTML response without the actual content of the page. Below is the code that failed to ...

How to eliminate items from arrays?

While browsing a forum, I stumbled upon the following code snippet related to a question I had. Unfortunately, I needed 50 reputation points in order to comment on the thread. So, I decided to share it here: array = ArrayUtils.removeElement(array, element ...

Dividing the screen into multiple sections to display cards

I have a question about integrating product cards into a split screen layout. Despite my efforts, the cards are stacking on top of each other instead of appearing side-by-side. Does anyone have a solution or suggestion? Below is the code I have been work ...

Pythonanywhere Flask app denies connection

from selenium import webdriver from flask import Flask, render_template import random from pyvirtualdisplay import Display from selenium.webdriver.firefox.options import Options as FirefoxOptions app = Flask(__name__) with Display(): options = Firefo ...

Extracting text with XPath from Child elements can be done easily by following these steps

I am working with a set of data rows that do not have trackable ids or classes, so I need to create an XPath based on parent-child relationships. Below is the HTML content: <tr class="v-formlayout-row v-formlayout-firstrow" xpath="1"> <td cla ...

Asserting that there are multiple elements being returned

I need assistance creating a method that can return two elements simultaneously. Here is the code I have so far: this.Wait.Until(ExpectedConditions.ElementExists(By.Id("mm_date_8"))); this.Wait.Until(ExpectedConditions.ElementExists(By.Id("dd_date_8"))) ...

Can curly braces be utilized in the style section of Vue.js?

Can I utilize curly braces in the style section of vue.js to set a value? For instance .container { background: url({{ image-url }}; color: {{ color-1 }} } ...

Inquiring about the implementation of hashCode, equals, and toString methods in Entities

What are the best practices for determining which instance variables should be included when overriding the hashcode and equals methods for entities with one-to-many relationships? For example, let's consider an entity where a User can have many Cont ...

"Anomaly Detected: isAbstract() Modifier Returning Unexpected Outcome - Investigating the Cause

In my understanding, based on the code provided, the expected output should be False. Surprisingly, when I ran the code snippet, it resulted in printing True instead. Referencing the Java docs: The documentation states that it will return true if the ...