Unable to locate the hidden element

Attempt to access the attribute of a shadow element resulted in encountering

ElementNotVisibleException Element with CSS input[type='checkbox'] is not present on screen

<checkbox _ngcontent-ebv-c14="" label="User Access" ng-version="8.0.3" class="ng-star-inserted">
  #shadow-root(open)
    <label>
      <input type="checkbox" name="" value="true">
      <span class="checkmark"></span>
      <!---->
      <span class="label">User Access</span>
   </label>
</checkbox>

Utilizing Selenium for fetching the element's attribute

WebElement parentElement = webdriver.findElement(By.xpath("//checkbox[@label='User Access']"));
webdriver.getShadowElement(parentElement,"input[type='checkbox']").getAttribute("value").contains("true");

Is there anything I overlooked?

Answer №1

This is how I tackled my issue

public class solution {
    WebDriver browser;
    
    public WebElement expandRootElement(WebElement element) {
        WebElement expandedElement = (WebElement) ((JavascriptExecutor) browser)
                .executeScript("return arguments[0].shadowRoot", element);
        return expandedElement;
    }

    @Test
    public void executeTest() {
        browser.get("https://example.com");
        WebElement rootElement = browser.findElement(By.tagName("//input[@type='text']"));
        WebElement shadowRoot = expandRootElement(rootElement);
        String textValue = shadowRoot.findElement(By.cssSelector("input[type='text']")).getText();
        Assert.assertEquals("Sample Text", textValue);
    }
}

To interact with the element, you can use the Javascript executor to expand it

Answer №2

To fix this issue, I followed the steps below:

  1. First, I copied the JavaScript path from the DOM element (by going to the inspect window, right-clicking on the element, selecting copy, and then choosing copy jspath)
  2. Next, I located the element using the JavaScript executor, converted it to a web element, and performed the necessary action

WebElement ele = (IWebElement)executor.ExecuteScript("return Paste your jspath here"); ele.Click();

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

Is there a way to change the fill color of an SVG circle using Thymeleaf?

I am struggling to change the background color of a circle in an SVG image on my HTML page using Thymeleaf. I attempted to use inline style tags, but it resulted in the circle's background color turning black. <svg id="svg" height="50" width=" ...

Is there a way to seamlessly incorporate this URL into my script without disrupting the current loop using Selenium in Python?

while True: try: time.sleep(2) browser.find_element_by_class_name("submit").click() except: time.sleep(2) browser.find_element_by_xpath("//a[contains(.,'Restart Battle&ap ...

What is the best way to eliminate the gap between these two div elements?

There's a gap between two divs in my HTML that I can't seem to remove. Here are the relevant CSS styles: #tab-2-content { display: grid; grid-template-rows: 1fr 2fr; width: 70%; margin: 0 auto; grid-gap: 0; } ... (CSS code continues) ...

Combining Tricentis Tosca with Selenium - A Dynamic Integration

I'm having trouble connecting my C# Selenium WebDriver Script with Tosca. When attempting to execute the script, I received the following error message: The assembly SeleniumProject was skipped because it references an unsupported version of nunit. ...

Merging double borders in a div using CSS is essential for creating

Upon examining this sample, it's evident that the borders do not blend together. css: div{ float:left; background-color:moccasin; width:100px; height:100px; border:1px solid tomato; } The number of divs is arbitrary, and only on ...

Tips for managing CSS/style conflicts in JavaScript/AngularJS applications

I am new to AngularJS and I have a requirement to assign CSS properties to a component at the JavaScript file level. When I debug my code after applying the CSS styles to the component, I can see that all the applied CSS properties are behaving as expected ...

issue with JavaScript canvas

My task is to develop a Blackberry application, but I have limited knowledge in Java. Since the application requires drawing capabilities, I decided to use HTML5 and JavaScript instead. I started reading some JavaScript tutorials to prepare for this proj ...

The Scrapy CSS selector is not fetching any prices from the list

Having trouble with the price CSS-selector while scraping an interactive website using Scrapy. Check out the HTML screenshot I captured: https://i.stack.imgur.com/oxULz.png Here are a few selectors that I've already experimented with: price = respon ...

What is the best way to align a Font Awesome icon within an SVG circle?

Can anyone help me position the trophy inside the circle, just below the number 2000? <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="1b7974746f686f697a6b5b2e352835 ...

Adjusting the overflow of a parent div based on the position of the div within it by scrolling

I'm trying to create a page with 3 different sections: <div class="container" id="main-container"> <div class="section" id="profile"> Hello World </div> <div class="section" id="projects"> Hello World 2 ...

Tips for saving JavaScript results to a form

Hey there! I'm looking to figure out how to save a JavaScript calculation within a form instead of just displaying an alert or outputting it on another page. Below is the JavaScript code I have for calculating prices. <script type="text/javascript ...

Experiencing difficulties replicating two auto-scrolling divs

I have implemented a script to automatically slide two different divs. Here is the code I am using: The HTML: <div id="gallery"> <div id="slider" style="width: 6000px; left: -500px;"> <div><aside class="widget widget_testimoni ...

Error: Element not found - Selenium Python unable to locate element

I am encountering an issue while attempting to click on a button. The error message I receive states that the element does not exist, despite it being present on the page: Message: no such element: Unable to locate element: {"method":"xpath","selector":"// ...

issues with the visual studio website code (Struggling to establish a connection between the site and a database)

Currently, I am in the process of developing a website using Visual Studio and attempting to connect it to a Microsoft SQL Server 2014 database. My learning journey has led me to a tutorial video on YouTube focusing on creating a coffee-themed website alon ...

Remove the option to select specific minutes from the time input on an HTML

My HTML code includes a time input that looks like this: <input id="appt-time" type="time" name="appt-time" value="13:30" /> I want to figure out how I can remove the minutes from the input or ensure that the ...

Guide on selecting an element from a dropdown menu by its label using Selenium in Java

https://i.sstatic.net/4Gsj9.png <div class="left"> <select id="hidebox" class="ays-ignore" size="8"> <ul><option value="103249">260 - OMAHA 30TH ST</option></ul> <ul><option value="103266">540 - UNION CITY& ...

The radio button selection cannot be transmitted via email

Only the first radio button is selected <div class="field_radio" name="preference" > <input class="radio1" type="radio" name="preference" id="preference" value="team" onclick="ShowHideDiv()" /><label for="radio1"> ...

Is it possible to utilize Javascript instead of PHP Curl to present JSON API data?

After successfully displaying JSON API data from openweathermap.org on my HTML webpage using PHP Curl, I am now seeking help to achieve the same output using Javascript. My PHP script retrieves JSON data from the source, and here is a snippet of that PHP c ...

Attempting to position the calendar control at the center in Visual Studio using C#

I'm currently working in Visual Studio using C# and I've encountered an issue with centering a Calendar control within a Panel. While I have successfully centered other elements such as radiobuttons, checkboxes, and textboxes in the panel, the t ...

Text encoded in UTF-8 emerges from the surrounding frame

As someone who is relatively new to frontend development, I recently created a blog using a template that utilizes Next.js, Tailwind, and React. However, when I input UTF-8 text in Korean, the text overflows outside of the parent frame on mobile devices. ...