Issues with CSS selectors in Internet Explorer causing problems with Selenium Webdriver C#

When automating a website with Selenium C#, I encountered a NoSuchElementException when trying to click an element using a CSS selector. However, the issue was resolved when using xpath instead. Can someone shed light on why this discrepancy between CSS and xpath selectors?

Xpath value:

//*[@id="primary-navigation"]/ul/li[2]/a

CSS Selector value:

#primary-navigation > ul > li:nth-child(2) > a

Answer №1

Using xpaths, the expression li[2] will target the second li element in the document.

On the other hand, utilizing CSS, li:nth-child(2) selects the li element that is the second child of its parent.

Consider this scenario:

<ul>
  <p> something </p>
  <li> this will be returned </li>
</ul>

In this case, the xpath li[2] would not match anything since there is only one li element present.

However, using css with li:nth-child(2), the li element will be successfully targeted as it is the second child of the parent ul element.

Therefore, it's essential to understand that while both selectors are functioning correctly, direct translation between xpath and css selectors may not always be possible or straightforward.

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

The ChromeDriver for selenium is exclusively compatible with Chrome version 91

While using selenium, I encountered the following error: selenium.common.exceptions.SessionNotCreatedException: Message: session not created: This version of ChromeDriver only supports Chrome version 91 The current browser version is 90.0.4430.212 with th ...

The formgroup label is currently displayed in uppercase, but I would prefer it to be in title case

In the angular template below, I have noticed that even though the labels are in titlecase, they appear in uppercase when the page is rendered. This wasn't the desired outcome so I attempted to use the titlecase pipe and enclose the label text within ...

"Selenium WebDriver is capable of interacting with ExtJS combo boxes that share the same XPath, IDs, and CSS paths but have dynamically changing IDs. With its functionality, it allows

I'm facing a challenge with testing a web application built on extJS that features multiple drop-down combo boxes. Each box shares the same class name, and their IDs change every time the page reloads. These combo boxes are not your typical drop-down ...

The .remove() method is ineffective when used within an Ajax success function

I am facing an issue with removing HTML divs generated using jinja2 as shown below: {% for student in students %} <div class="item" id="{{ student.id }}_div"> <div class="right floated content"> <div class="negative ui button compa ...

Align an image with text using CSS and HTML

I'm having trouble with my CSS and HTML code. I want to align the text to the right of an image in a grid format, but it keeps appearing below the image instead. Here is the code snippet: <html> <head> <style type="text/css"> #conta ...

Ways to extract the text attribute from the HTML provided using Selenium in Python

Within this section of HTML code, I am aiming to extract the phrase "Page 2 of 2" from it. HTML code <thead> <tr> <th scope="col" class="GridHeader_Sonetto"><input id="ctl00_ctl00_ContentPl ...

The background image doesn't extend to the bottom of the page due to the inability to utilize background-attachment

Currently, I am working on a project that requires a background image to cover the entire page. Unfortunately, using "background-attachment: cover" is not an option due to the need for compatibility with iOS, which does not support this property. .sky-b ...

Adding a Unique Custom Menu Item in _tk Wordpress Theme: Incorporating a Button and Search Form

I am currently working on customizing the _tk theme for WordPress, and I have hit a roadblock when it comes to the Nav Menu functionality. Adding menu items from the WordPress admin page is easy for custom links and pages. However, I am looking to include ...

Challenge with the positioning of HTML output layout

Is there a way to center the output/result of the HTML code below both horizontally and vertically on the web page using CSS? I have tried the following code but it only centers the output horizontally. Vertical alignment is not working properly. Can someo ...

Fixed position toolbar within a container positioned beside a dynamically sized sidebar navigation

I'm trying to figure out how to create a fixed toolbar that fills the remaining width of the page when a side nav is collapsible. Setting the width to 100% causes it to overflow the page due to the dynamic nature of the side nav. Using calc() isn&apos ...

Replace existing styled-component CSS properties with custom ones

One of my components includes a CheckBox and Label element. I want to adjust the spacing around the label when it's used inside the CheckBox. Can this be achieved with styled()? Debugging Info The issue seems to be that the className prop is not b ...

Discover the name of a color using its HEX code or RGB values

Is there a way to retrieve the color name from RBG or HEX code using JavaScript or JQuery? Take for instance: Color Name RGB black #000000 white #FFFFFF red #FF0000 green #008000 ...

Implementing JAVA-based Selenium web driver UI tests in Azure Devops CI/CD pipelines for efficient automation of testing processes

I am currently working on a JAVA spring boot application that includes JUnit unit tests and functional tests using Selenium Web Driver all within the same project. My goal is to automate these tests through Azure DevOps pipelines. So far, I have set up a ...

The system is unable to retrieve the value of the property which is set as null

I'm trying to figure out how to store the input value of name_enter into the variable userName. However, every time I attempt this, I encounter the following console error: Uncaught TypeError: Cannot read property 'value' of null function ...

Design: Seeking a layout feature where one cell in a row can be larger than the other cells

To better illustrate my goal, refer to this image: Desired Output <\b> Currently, I'm achieving this: current output Imagine 7 rows of data with two columns each. The issue arises in row 1, column 2 where the control needs to span 5 row ...

Tips on storing browser sessions in Selenium

I am currently utilizing Selenium for logging into an account. My goal is to save the session after logging in so that I can access it again the next time I run my Python script without having to log in again. Essentially, I want the Chrome driver to fun ...

In order to check a checkbox, you need to ensure that the scrolling div has been scrolled to the very

I have a situation where I need to ensure that a user has scrolled to the bottom of a disclaimer before they can acknowledge it by checking a checkbox. Our legal department requires this extra step for compliance reasons. Here is an example of how the mark ...

Customize the File Download Directory in Selenium Python Using Chromedriver

I need help with saving files to different locations in Python using Chromedriver. Below is the code I am currently using to set Chrome to download files to a specific folder_path without prompting for download location. After successfully downloading on ...

Positioning SVGs within a parent container while maintaining responsiveness

How can I anchor an SVG overlay with a circle in the bottom right corner while expanding the rest of the SVG to fill the remaining area of the container without distorting the circle? I've been able to position the SVG in the bottom right corner, but ...

Show image details on hover?

I have six images displayed full width using a flex grid. I would like the image information (along with a visit button) to appear on hover, and the brightness of the image should decrease. I am struggling to position the information in the center of the i ...