I did the same and was able to locate the element but i get this null errorException in thread "main" org.openqa.selenium.JavascriptException: javascript error: Cannot read properties of null (reading 'shadowRoot') Can you advise how can i resolve this? I have tried almost every possible way to remove this null error but i am failing. Please advise. thanks
below is the code`public static void main(String[] args) throws InterruptedException {
System.out.println("Hello world!");
System.setProperty("webdriver.chrome.driver", "chromedriver.exe");
WebDriver driver = new ChromeDriver();
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
driver.get("https://shop.mercedes-benz.com/en-au/shop/vehicle/srp/demo");
driver.navigate().to("https://shop.mercedes-benz.com/en-au/shop/vehicle/srp/demo?error=login_required&sort=relevance-demo&assortment=vehicle");
JavascriptExecutor jse = (JavascriptExecutor) driver;
// jse.executeScript("return document.querySelector('cmm-cookie-banner')");
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')");
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
jse.executeScript("arguments[0].click();", agreeBtn);
// agreeBtn.click();
}
} and the output as follow: `"C:\Program Files\Java\jdk-17\bin\java.exe" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2023.2\lib\idea_rt.jar=64488:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2023.2\bin" -Dfile.encoding=UTF-8 -classpath D:\mine\learningShadowElement\target\classes;D:\users\se25048.m2\repository\org\seleniumhq\selenium\selenium-java\4.25.0\selenium-java-4.25.0.jar;D:\users\se25048.m2\repository\org\seleniumhq\selenium\selenium-api\4.25.0\selenium-api-4.25.0.jar;D:\users\se25048.m2\repository\org\jspecify\jspecify\1.0.0\jspecify-1.0.0.jar;D:\users\se25048.m2\repository\org\seleniumhq\selenium\selenium-chrome-driver\4.25.0\selenium-chrome-driver-4.25.0.jar;D:\users\se25048.m2\repository\com\google\auto\service\auto-service-annotations\1.1.1\auto-service-annotations-1.1.1.jar;D:\users\se25048.m2\repository\org\seleniumhq\selenium\selenium-chromium-driver\4.25.0\selenium-chromium-driver-4.25.0.jar;D:\users\se25048.m2\repository\org\seleniumhq\selenium\selenium-json\4.25.0\selenium-json-4.25.0.jar;D:\users\se25048.m2\repository\org\seleniumhq\selenium\selenium-manager\4.25.0\selenium-manager-4.25.0.jar;D:\users\se25048.m2\repository\org\seleniumhq\selenium\selenium-devtools-v127\4.25.0\selenium-devtools-v127-4.25.0.jar;D:\users\se25048.m2\repository\org\seleniumhq\selenium\selenium-devtools-v128\4.25.0\selenium-devtools-v128-4.25.0.jar;D:\users\se25048.m2\repository\org\seleniumhq\selenium\selenium-devtools-v129\4.25.0\selenium-devtools-v129-4.25.0.jar;D:\users\se25048.m2\repository\org\seleniumhq\selenium\selenium-devtools-v85\4.25.0\selenium-devtools-v85-4.25.0.jar;D:\users\se25048.m2\repository\org\seleniumhq\selenium\selenium-edge-driver\4.25.0\selenium-edge-driver-4.25.0.jar;D:\users\se25048.m2\repository\org\seleniumhq\selenium\selenium-firefox-driver\4.25.0\selenium-firefox-driver-4.25.0.jar;D:\users\se25048.m2\repository\org\seleniumhq\selenium\selenium-http\4.25.0\selenium-http-4.25.0.jar;D:\users\se25048.m2\repository\dev\failsafe\failsafe\3.3.2\failsafe-3.3.2.jar;D:\users\se25048.m2\repository\org\seleniumhq\selenium\selenium-ie-driver\4.25.0\selenium-ie-driver-4.25.0.jar;D:\users\se25048.m2\repository\org\seleniumhq\selenium\selenium-remote-driver\4.25.0\selenium-remote-driver-4.25.0.jar;D:\users\se25048.m2\repository\com\google\guava\guava\33.3.0-jre\guava-33.3.0-jre.jar;D:\users\se25048.m2\repository\com\google\guava\failureaccess\1.0.2\failureaccess-1.0.2.jar;D:\users\se25048.m2\repository\com\google\guava\listenablefuture\9999.0-empty-to-avoid-conflict-with-guava\listenablefuture-9999.0-empty-to-avoid-conflict-with-guava.jar;D:\users\se25048.m2\repository\com\google\code\findbugs\jsr305\3.0.2\jsr305-3.0.2.jar;D:\users\se25048.m2\repository\org\checkerframework\checker-qual\3.43.0\checker-qual-3.43.0.jar;D:\users\se25048.m2\repository\com\google\errorprone\error_prone_annotations\2.28.0\error_prone_annotations-2.28.0.jar;D:\users\se25048.m2\repository\com\google\j2objc\j2objc-annotations\3.0.0\j2objc-annotations-3.0.0.jar;D:\users\se25048.m2\repository\io\opentelemetry\semconv\opentelemetry-semconv\1.25.0-alpha\opentelemetry-semconv-1.25.0-alpha.jar;D:\users\se25048.m2\repository\io\opentelemetry\opentelemetry-api\1.42.1\opentelemetry-api-1.42.1.jar;D:\users\se25048.m2\repository\io\opentelemetry\opentelemetry-context\1.42.1\opentelemetry-context-1.42.1.jar;D:\users\se25048.m2\repository\io\opentelemetry\opentelemetry-exporter-logging\1.42.1\opentelemetry-exporter-logging-1.42.1.jar;D:\users\se25048.m2\repository\io\opentelemetry\opentelemetry-sdk-common\1.42.1\opentelemetry-sdk-common-1.42.1.jar;D:\users\se25048.m2\repository\io\opentelemetry\opentelemetry-sdk-extension-autoconfigure-spi\1.42.1\opentelemetry-sdk-extension-autoconfigure-spi-1.42.1.jar;D:\users\se25048.m2\repository\io\opentelemetry\opentelemetry-sdk-extension-autoconfigure\1.42.1\opentelemetry-sdk-extension-autoconfigure-1.42.1.jar;D:\users\se25048.m2\repository\io\opentelemetry\opentelemetry-api-incubator\1.42.1-alpha\opentelemetry-api-incubator-1.42.1-alpha.jar;D:\users\se25048.m2\repository\io\opentelemetry\opentelemetry-sdk-trace\1.42.1\opentelemetry-sdk-trace-1.42.1.jar;D:\users\se25048.m2\repository\io\opentelemetry\opentelemetry-sdk\1.42.1\opentelemetry-sdk-1.42.1.jar;D:\users\se25048.m2\repository\io\opentelemetry\opentelemetry-sdk-metrics\1.42.1\opentelemetry-sdk-metrics-1.42.1.jar;D:\users\se25048.m2\repository\io\opentelemetry\opentelemetry-sdk-logs\1.42.1\opentelemetry-sdk-logs-1.42.1.jar;D:\users\se25048.m2\repository\net\bytebuddy\byte-buddy\1.15.1\byte-buddy-1.15.1.jar;D:\users\se25048.m2\repository\org\seleniumhq\selenium\selenium-os\4.25.0\selenium-os-4.25.0.jar;D:\users\se25048.m2\repository\org\apache\commons\commons-exec\1.4.0\commons-exec-1.4.0.jar;D:\users\se25048.m2\repository\org\seleniumhq\selenium\selenium-safari-driver\4.25.0\selenium-safari-driver-4.25.0.jar;D:\users\se25048.m2\repository\org\seleniumhq\selenium\selenium-support\4.25.0\selenium-support-4.25.0.jar org.example.Main Hello world! Nov 07, 2024 10:10:42 AM 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: '8a8aea2337' System info: os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '17.0.8' Driver info: org.openqa.selenium.chrome.ChromeDriver Command: [c1cbdaa97fdd9868ea344de3e49686f8, 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.70 (df87d5cf12b1..., userDataDir: D:\users\se25048\AppData\Lo...}, fedcm:accounts: true, goog:chromeOptions: {debuggerAddress: localhost:64498}, networkConnectionEnabled: false, pageLoadStrategy: normal, platformName: windows, proxy: Proxy(), se:cdp: ws://localhost:64498/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: c1cbdaa97fdd9868ea344de3e49686f8 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:31)
Process finished with exit code 1`