Skip to content

fix: relative XPath chaining in BiDi mode#14912

Merged
christian-bromann merged 2 commits intowebdriverio:mainfrom
mccmrunal:fix/xpath-chaining-bidi-issue-14675
Jan 8, 2026
Merged

fix: relative XPath chaining in BiDi mode#14912
christian-bromann merged 2 commits intowebdriverio:mainfrom
mccmrunal:fix/xpath-chaining-bidi-issue-14675

Conversation

@mccmrunal
Copy link
Contributor

Fixes #14675

Proposed changes

This PR fixes an issue where chaining relative XPath selectors (e.g., element.$('./following-sibling::div')) fails when running in WebDriver BiDi mode (the default in v9).

Root Cause:
The WebDriver BiDi browsingContext.locateNodes command, when provided with startNodes, searches for elements within the descendants of those nodes. It does not interpret relative XPath expressions (starting with ./ or ..) as relative to the startNodes themselves in the same way WebDriver Classic does. This caused selectors like ./following-sibling::div to fail to find elements that are siblings of the context element.

The Fix:
I updated findDeepElement and findDeepElements in [packages/webdriverio/src/utils/index.ts] to detect this specific scenario:

  1. The selector strategy is xpath.
  2. The selector value starts with ./ or .. (indicating a relative path).
  3. The command is being executed on an element (i.e., elementId is present).

When these conditions are met, the code now falls back to using the WebDriver Classic findElementFromElement (or findElementsFromElement) command, ensuring the relative XPath is evaluated correctly against the context element.

Types of changes

  • Polish (an improvement for an existing feature)
  • Bugfix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation update (improvements to the project's docs)
  • Specification changes (updates to WebDriver command specifications)
  • Internal updates (everything related to internal scripts, governance documentation and CI files)

Checklist

  • I have read the CONTRIBUTING doc
  • I have added tests that prove my fix is effective or that my feature works
  • I have added the necessary documentation (if appropriate)
  • I have added proper type definitions for new commands (if appropriate)

Backport Request

//: # (The current main branch is the development branch for WebdriverIO v9. If your change should be released to the current major version of WebdriverIO (v8), please raise another PR with the same changes against the v8 branch.)

  • This change is solely for v9 and doesn't need to be back-ported
  • Back-ported PR at #XXXXX

Further comments

I verified this fix by:

  1. Creating a local reproduction case with element.$('./following-sibling::div') which failed in BiDi mode.
  2. Confirming the test passes with this fix applied.
  3. Adding unit tests in [packages/webdriverio/tests/utils/index.test.ts]

Reviewers: @webdriverio/project-committers

@pkg-pr-new
Copy link

pkg-pr-new bot commented Dec 3, 2025

Open in StackBlitz

create-wdio

npm i https://pkg.pr.new/webdriverio/webdriverio/create-wdio@14912

eslint-plugin-wdio

npm i https://pkg.pr.new/webdriverio/webdriverio/eslint-plugin-wdio@14912

@wdio/allure-reporter

npm i https://pkg.pr.new/webdriverio/webdriverio/@wdio/allure-reporter@14912

@wdio/appium-service

npm i https://pkg.pr.new/webdriverio/webdriverio/@wdio/appium-service@14912

@wdio/browser-runner

npm i https://pkg.pr.new/webdriverio/webdriverio/@wdio/browser-runner@14912

@wdio/browserstack-service

npm i https://pkg.pr.new/webdriverio/webdriverio/@wdio/browserstack-service@14912

@wdio/cli

npm i https://pkg.pr.new/webdriverio/webdriverio/@wdio/cli@14912

@wdio/concise-reporter

npm i https://pkg.pr.new/webdriverio/webdriverio/@wdio/concise-reporter@14912

@wdio/config

npm i https://pkg.pr.new/webdriverio/webdriverio/@wdio/config@14912

@wdio/cucumber-framework

npm i https://pkg.pr.new/webdriverio/webdriverio/@wdio/cucumber-framework@14912

@wdio/dot-reporter

npm i https://pkg.pr.new/webdriverio/webdriverio/@wdio/dot-reporter@14912

@wdio/firefox-profile-service

npm i https://pkg.pr.new/webdriverio/webdriverio/@wdio/firefox-profile-service@14912

@wdio/globals

npm i https://pkg.pr.new/webdriverio/webdriverio/@wdio/globals@14912

@wdio/jasmine-framework

npm i https://pkg.pr.new/webdriverio/webdriverio/@wdio/jasmine-framework@14912

@wdio/json-reporter

npm i https://pkg.pr.new/webdriverio/webdriverio/@wdio/json-reporter@14912

@wdio/junit-reporter

npm i https://pkg.pr.new/webdriverio/webdriverio/@wdio/junit-reporter@14912

@wdio/lighthouse-service

npm i https://pkg.pr.new/webdriverio/webdriverio/@wdio/lighthouse-service@14912

@wdio/local-runner

npm i https://pkg.pr.new/webdriverio/webdriverio/@wdio/local-runner@14912

@wdio/logger

npm i https://pkg.pr.new/webdriverio/webdriverio/@wdio/logger@14912

@wdio/mocha-framework

npm i https://pkg.pr.new/webdriverio/webdriverio/@wdio/mocha-framework@14912

@wdio/protocols

npm i https://pkg.pr.new/webdriverio/webdriverio/@wdio/protocols@14912

@wdio/repl

npm i https://pkg.pr.new/webdriverio/webdriverio/@wdio/repl@14912

@wdio/reporter

npm i https://pkg.pr.new/webdriverio/webdriverio/@wdio/reporter@14912

@wdio/runner

npm i https://pkg.pr.new/webdriverio/webdriverio/@wdio/runner@14912

@wdio/sauce-service

npm i https://pkg.pr.new/webdriverio/webdriverio/@wdio/sauce-service@14912

@wdio/shared-store-service

npm i https://pkg.pr.new/webdriverio/webdriverio/@wdio/shared-store-service@14912

@wdio/smoke-test-cjs-service

npm i https://pkg.pr.new/webdriverio/webdriverio/@wdio/smoke-test-cjs-service@14912

@wdio/smoke-test-reporter

npm i https://pkg.pr.new/webdriverio/webdriverio/@wdio/smoke-test-reporter@14912

@wdio/smoke-test-service

npm i https://pkg.pr.new/webdriverio/webdriverio/@wdio/smoke-test-service@14912

@wdio/spec-reporter

npm i https://pkg.pr.new/webdriverio/webdriverio/@wdio/spec-reporter@14912

@wdio/static-server-service

npm i https://pkg.pr.new/webdriverio/webdriverio/@wdio/static-server-service@14912

@wdio/sumologic-reporter

npm i https://pkg.pr.new/webdriverio/webdriverio/@wdio/sumologic-reporter@14912

@wdio/testingbot-service

npm i https://pkg.pr.new/webdriverio/webdriverio/@wdio/testingbot-service@14912

@wdio/types

npm i https://pkg.pr.new/webdriverio/webdriverio/@wdio/types@14912

@wdio/utils

npm i https://pkg.pr.new/webdriverio/webdriverio/@wdio/utils@14912

@wdio/webdriver-mock-service

npm i https://pkg.pr.new/webdriverio/webdriverio/@wdio/webdriver-mock-service@14912

@wdio/xvfb

npm i https://pkg.pr.new/webdriverio/webdriverio/@wdio/xvfb@14912

webdriver

npm i https://pkg.pr.new/webdriverio/webdriverio/webdriver@14912

webdriverio

npm i https://pkg.pr.new/webdriverio/webdriverio@14912

commit: 558fa5f

* we need to fall back to the regular WebDriver Classic command as BiDi
* does not support relative xpath selectors with a start node
*/
if (using === 'xpath' && (value.startsWith('./') || value.startsWith('..')) && (this as WebdriverIO.Element).elementId) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As mentioned in the other PR, can we use startNodes to enable this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

startNodes searches within descendants only. Sibling axes like ./following-sibling::* look outside, so Classic fallback is still needed. (This is my understanding - happy to be corrected if I'm wrong!)

Copy link
Member

@christian-bromann christian-bromann left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 👍

@christian-bromann christian-bromann merged commit 3c634f7 into webdriverio:main Jan 8, 2026
59 checks passed
@christian-bromann christian-bromann added the PR: Bug Fix 🐛 PRs that contain bug fixes label Jan 8, 2026
@wdio-bot
Copy link
Contributor

wdio-bot commented Jan 8, 2026

Hey mccmrunal 👋

Thank you for your contribution to WebdriverIO! Your pull request has been marked as an "Expensable" contribution.

We've sent you an email with further instructions on how to claim your expenses from our development fund.
Please make sure to check your spam folder as well. If you have any questions, feel free to reach out to us at expense@webdriver.io or in the contributing channel on Discord.

We are looking forward to more contributions from you in the future 🙌

Have a nice day,
The WebdriverIO Team 🤖

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Expensable $15 💸 PR: Bug Fix 🐛 PRs that contain bug fixes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[🐛 Bug]: XPath chaining not working in V9 in methods but same code working in v7

3 participants