Dhanush S
Dhanush S

Reputation: 1

Cannot locate text inside #shadow-root (user-agent)

I'm using Selenium - Java with testNG for automating webpage.

As you can see in the image of the website, there is a 'Timezone' dropdown menu with 'Please Select' as the default value. I want to locate the 'Please Select' text which is present in the <div> aria-hidden = "true" Please Select</div> line under #shadow-root (user-agent).

If I try to locate the text - 'Please Select' , I was unsuccessful.

  1. Getting org.openqa.selenium.NoSuchShadowRootException: no such shadow root exception when I tried the below method.
WebElement element = driver.findElement(By.id("timezone"));
        WebElement shadowRoot1 = (WebElement) ((JavascriptExecutor)driver).executeScript("return arguments[0].shadowRoot", element);
        String text = shadowRoot1.findElement(By.cssSelector("div")).getText();
        System.out.println(text);

2. Also tried to locate the element using getShadowRoot() method but the same exception occurs again.

Also attaching the debugging screenshot for reference.

Upvotes: 0

Views: 242

Answers (1)

pburgr
pburgr

Reputation: 1778

I don't see any #shadow-root in the html source code.

Extracting desired text using element.getText() is working just fine.

Code example:

package tests;

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.Select;

import selenium.ChromeDriverSetup;

public class DhanushS extends ChromeDriverSetup {

    public static void main(String[] args) {
    
        WebDriver driver = startChromeDriver();
        driver.get("https://dash.bling-center.com/platform/home.html");
        driver.findElement(By.id("email")).sendKeys("[email protected]");
        driver.findElement(By.id("password")).sendKeys("testuser2704");
        driver.findElement(By.xpath("/html/body/div/div[2]/div/div/button/p")).click();
        
        driver.findElement(By.id("phone-tab")).click();
        driver.findElement(By.id("phonenumber_1")).click();
        driver.findElement(By.id("area_code")).sendKeys("+420");        
        driver.findElement(By.id("phonenumber_continue")).click();
        
        WebElement setBusinessHours = driver.findElement(By.id("setBusinessHours"));
        WebElement businesshoursCheck = setBusinessHours.findElement(By.tagName("input"));
        
        JavascriptExecutor js = (JavascriptExecutor) driver;
        js.executeScript("arguments[0].click();", businesshoursCheck);

        WebElement timezone = driver.findElement(By.id("timezone"));
        
        String tzText = timezone.getText();
        System.out.println("All options text:" + System.lineSeparator() + tzText);
        
        Select select = new Select(timezone);       
        String selectedTzText = select.getFirstSelectedOption().getText();
        System.out.println("Selected option text:" + System.lineSeparator() + selectedTzText);

        driver.quit();
    }

}

Output:

Starting ChromeDriver 111.0.5563.64 (c710e93d5b63b7095afe8c2c17df34408078439d-refs/branch-heads/5563@{#995}) on port 23727
Only local connections are allowed.
Please see https://chromedriver.chromium.org/security-considerations for suggestions on keeping ChromeDriver safe.
ChromeDriver was started successfully.
[1682600077.367][WARNING]: This version of ChromeDriver has not been tested with Chrome version 112.
Dub 27, 2023 2:54:37 ODP. org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: W3C
All options text:
                          Please Select
                          US/Hawaii
                          America/Los Angeles
                          America/Vancouver
                          Canada/Pacific
                          America/Chicago
                          Canada/Central
                          America/New York
                          America/Montreal
                          America/Toronto
                          Canada/Eastern
                          America/Phoenix
                        
Selected option text:
Please Select

Upvotes: 0

Related Questions