I have written this code in which I am able to get the shadow element:
package org.example;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class Main { public static void main(String[] args)
{System.out.println("Hello and welcome!");
System.setProperty("webdriver.chrome.driver", "chromedriver.exe");
WebDriver driver= new ChromeDriver();
driver.get("https://shop.mercedes-benz.com/en-au/shop/vehicle/srp/demo");
JavascriptExecutor jse = (JavascriptExecutor) driver;
WebElement agreeBtn= (WebElement) jse.executeScript("return document.querySelector('cmm-cookie-banner') .shadowRoot.querySelector('div').querySelector('div').querySelector('cmm-buttons-wrapper').querySelector('wb7-button:nth-of-type(2) ').shadowRoot.querySelector('button.button')");
((JavascriptExecutor)driver).executeScript("arguments[0].click();", agreeBtn); } }
The below is the output when I run this program:
Nov 05, 2024 10:03:15 PM org.openqa.selenium.devtools.CdpVersionFinder findNearestMatch
WARNING: Unable to find an exact match for CDP version 130, returning the closest version; found: 129; Please update to a Selenium version that supports CDP version 130 Exception in thread "main"
org.openqa.selenium.JavascriptException: javascript error: Cannot read properties of null (reading 'shadowRoot') (Session info: chrome=130.0.6723.92)
Build info: version: '4.25.0', revision: '030fcf7918'
System info: os.name: 'Windows 11', os.arch: 'amd64', os.version: '10.0', java.version: '17.0.8'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Command: [647d6b65e33549f28a4ea4e137e0a4f5, executeScript {script=return document.querySelector('cmm-cookie-banner').shadowRoot.querySelector('div').querySelector('div').querySelector('cmm-buttons-wrapper').querySelector('wb7-button:nth-of-type(2) ').shadowRoot.querySelector('button.button'), args=[]}] Capabilities {acceptInsecureCerts: false, browserName: chrome, browserVersion: 130.0.6723.92, chrome: {chromedriverVersion: 129.0.6668.9 (ab04602ab643c..., userDataDir:
C:\Users\us\AppData\Local\T...}, fedcm:accounts: true, goog:chromeOptions: {debuggerAddress: localhost:54013}, networkConnectionEnabled: false, pageLoadStrategy: normal, platformName: windows, proxy: Proxy(), se:cdp: ws://localhost:54013/devtoo..., se:cdpVersion: 130.0.6723.92, setWindowRect: true, strictFileInteractability: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior: dismiss and notify, webauthn:extension:credBlob: true, webauthn:extension:largeBlob: true, webauthn:extension:minPinLength: true, webauthn:extension:prf: true, webauthn:virtualAuthenticators: true}
Session ID: 647d6b65e33549f28a4ea4e137e0a4f5 at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:77) at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:499)
at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:480)
at org.openqa.selenium.remote.ErrorCodec.decode(ErrorCodec.java:167) at org.openqa.selenium.remote.codec.w3c.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:138) at org.openqa.selenium.remote.codec.w3c.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:50) at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:190) at org.openqa.selenium.remote.service.DriverCommandExecutor.invokeExecute(DriverCommandExecutor.java:216) at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:174) at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:545) at org.openqa.selenium.remote.RemoteWebDriver.executeScript(RemoteWebDriver.java:476) at org.example.Main.main(Main.java:20)
Process finished with exit code 1.
Also, when I check for the DOM element into browser, I am able to locate it.
Can you please advise how can I resolve this? I am really in need to solve this. I will be obliged of your help. Thanks