Selenium is unable to locate the XPath, yet it functions properly with jQuery

After encountering an issue with locating and clicking on a specific element using XPath in WebDriver, I discovered an interesting difference between the console in Chrome DevTools and the Find function.

The XPath that was causing trouble was:

//table[@title="someTitle"]//td[@id="someId"][3]

When attempting to click on this element through WebDriver's element(By.xpath(someXpathString)).click();, it resulted in an "ElementNotFound" exception. However, when using the same XPath in the Chrome DevTools console, the desired element was successfully located using:

$x('//table[@title="someTitle"]//td[@id="someId"][3]');

Interestingly, changing the XPath slightly to include parentheses like so:

(//table[@title="someTitle"]//td[@id="someId"])[3]

solved the problem and allowed WebDriver to find and interact with the element without any issues.

  • Note: I need to be able to select multiple elements by index within the same column of a table for my workarounds.

This experience made me question why the Chrome console could identify the XPath without parentheses, while WebDriver had trouble without them. What is the reasoning behind these two variations of the XPath syntax?

In my exploration, I have observed that on some other elements, the use of parentheses is unnecessary to target the 2nd or higher element of that type. Therefore, I am seeking a comprehensive explanation on how this form of XPath operates and the underlying logic governing its behavior.

Answer №1

Comparing two different xpaths:

  1. //table[@title="someTitle"]//td[@id="someId"][3]
    -- specifically selects the third column elements in a table.
  2. (//table[@title="someTitle"]//td[@id="someId"])[3]
    -- selects all columns and then returns only the element at index 3.

Imagine a table with 4 rows and 4 columns for illustration purposes. The first xpath picks out the elements in the third column of each row (total: 4). However, the second xpath grabs just one specific element, which is the third column of the first row regardless of the number of rows in the table.

If we were to use find element method, both would yield just one result. But when using find elements, the first xpath would return 4 elements while the second xpath would return only one single element.

Answer №2

In case you find yourself on a page where the element is from another source, consider refreshing the page:

driver.navigate().refresh();

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

Extracting elements using Node.js and Selenium

I'm currently experimenting with Selenium and nodeJS for the first time. I'm trying to parse an HTML page and am running into issues when attempting to perform a deeper search using XPATH on returned elements. Below is my code: async function par ...

Executing Selenium tests: utilizing the webdriver.wait function to repeatedly call a promise

Currently, I am using Selenium ChromeDriver, Node.js, and Mocha for testing purposes... I am facing a dilemma at the moment: The driver.wait function seamlessly integrates with until. I have a promise, which we'll refer to as promiseA. This pro ...

Look up the attribute in the parent node and retrieve the value of the child node's attribute

Simplified Version: Find attribute in parent node and retrieve attribute values from child nodes. Detailed Version: Within an XML file, I have the following snippet: <include template="directory/file.xml"> <param name="permission" va ...

Facing difficulties in initiating Selenium with PhantomJS/GhostDriver as child processes

I am currently working on a Node script that involves using the child_process module in order to run a Selenium server with PhantomJS's GhostDriver. I have imported the module: Child = require "child_process" This is how I am attempting to start the ...

How would you execute a simple protractor test with the provided file hierarchy?

I have attempted to follow a tutorial located HERE for running protractor tests. Despite my efforts, I am facing an issue where the tests do not seem to run when I attempt to start them. While my webdriver-manager is functioning correctly, nothing seems to ...

The issue with Protractor's sendkeys function is that it requires an X display for converting keycodes

I've encountered an issue while attempting to execute Protractor e2e tests in a Vagrant VM using headless Chrome. Despite successfully configuring Xvfb, I face an error when trying to fill out a form: unknown error: an X display is required for keycod ...

Steps to effortlessly retrieve a CSV file using Selenium without dealing with the bothersome file dialog

I've been attempting to access a URL with a CSV file for download in a specific directory by using the Selenium Webdriver for Firefox (geckodriver) in a NodeJS environment on Linux-Mint. Here's the code I'm working with: const {Builder} = r ...

Having trouble locating the web element within a div popup while utilizing Node.js and WebdriverIO

I need assistance with a seemingly simple task. I am currently learning webdriverjs and attempted to write a short code to register for an account on the FitBit website at URL: www.fitbit.com/signup. After entering my email and password, a popup appears as ...

Transform a protractor screenshot into a PDF file

I'm currently working on a small Protractor code that captures screenshots, but my goal is to save these screenshots as PDF files. Below you can find the code snippet I have written. await browser.get(url); const img = await browser.takeScreenshot(); ...

The setTimeout functionality is executing faster than expected

In my selenium test, I've noticed that the setTimeout function consistently finishes about 25% faster than it should. For example, when waiting for 20 seconds, the function completes after only 15 seconds. test.describe('basic login test',f ...

Utilizing Selenium JavaScript to insert a cookie into a request

Trying to add a cookie to the request in Selenium using JavaScript. I followed the documentation at this link, but my code snippet doesn't seem to pass any cookies to the PHP script below on the server. Here is the client-side JavaScript code: var w ...

Utilizing the same WebDriverJS instance repeatedly

As a beginner in Selenium, I successfully launched a website using the following Node.js code snippet: var webdriver = require('selenium-webdriver'); var driver = new webdriver.Builder() .forBrowser('chrome') .build(); console ...

Tips for interacting with a custom web component using Selenium WebDriver

As a newcomer to writing selenium tests, I am attempting to create an automated test for a carousel feature on our homepage. The objective is to click on one of the carousel navigation buttons and then confirm that a specific inline style has been applied ...

The functionality of Webdriver.waitUntil is not meeting the expected outcomes

I'm currently utilizing webdriverio version 4.5: ./node_modules/.bin/wdio -v v4.5.2 In my scenario, I am in need of waiting for the existence of a specific element and handling the situation if it doesn't exist. Here is an example code snippet ...

Issue with installing chromedriver and selenium-driver on Windows 7 using NPM arise

I have a relatively basic package.json file with some development dependencies included. Here is what it looks like: "devDependencies": { "cssnano": "3.3.2", "cucumber": "0.9.2", "diff": "2.2.0", "grunt": "0.4.5", "jit-grunt": "0.9.1", " ...

Is it possible to run nightwatch.js tests on a Linux machine?

Struggling to kick off the nightwatch automated UI tests on a Linux machine running CentOS OS? Starting is proving to be quite challenging. This is the test_settings configuration in my nightwatch.json: "test_settings": { "default": { ...

The error message "NoSuchSessionError: invalid session id" pops up in Selenium, despite the fact that the application is running smoothly

Scenario and Background: I have recently developed a script to access an external website and extract specific data. The script's purpose is to retrieve grades of students and convert them into usable data for plotting. In order to streamline the dat ...

Having difficulty incorporating custom JavaScript files into a testing framework

I am facing a unique challenge while integrating the Page Object design pattern into my test suite using selenium-webdriver and node.js. The first page object, pageObject/admin/login/index.js, works seamlessly. It contains selectors and methods to fill ou ...

Ubuntu compatibility with chrome-driver 83

Currently using Ubuntu 18.04.1 LTS with Google Chrome version 83 and corresponding chrome-driver version 83 successfully for a while, I recently encountered an issue after updating to the latest stable versions: google-chrome-stable v83.0.4103.61 and chrom ...

Combining multiple storageStates in a playwright context for efficient loading

I am trying to load multiple storageStates into a single context in playwright, but I am facing some issues. When using the following code: const context = await browser.newContext({ storageState: "telegram.json",storageState: "google. ...