Unable to Click on Shadow dom elements Salesforce application

Hi ,

In my salesforce application there are multiple shadow dom elements present inside an iframe. I have used the below link and tried both the solution, but i am getting stale element reference exception, when the script tries to click on the shadow DOM element. Please suggest how can I resolve the error.
I have created a child element which I need to click ‘paper-button’ and created an another object for parent shadow dom element ‘sbPageContainer’. I have selected the Shadow root parent in the child test object and added the parent shadow dom element. I have added the iframe to the parent test object. While script tries to click the element getting the below error.

Caused by: org.openqa.selenium.StaleElementReferenceException: stale element reference: stale element not found
At object: ‘Object Repository/Page_Q-296116 Quote Salesforce/paper-button’
Reason:
com.kms.katalon.core.exception.StepFailedException: Unable to click on object ‘Object Repository/Page_Q-296116 Quote Salesforce/paper-button’ (Root cause: com.kms.katalon.core.exception.StepFailedException: Unable to click on object ‘Object Repository/Page_Q-296116 Quote Salesforce/paper-button’
Caused by: org.openqa.selenium.StaleElementReferenceException: stale element reference: stale element not found.

Thanks!




1 Like

You have got a notorious “Stale Element Reference”.

For your information, have a look at the following topic

But I would warn you; this topic is long and winding. It may take you hours to read through.

Smart Wait may help you, but not necessarily.

image

Now you lost me. I see three nested shadow dom elements and that statement is near impossible parse and track against the code I’m seeing. And to top it off, you’re inside an <iframe>

Please PLEASE make it easier for us to follow.

That said, my advise is to talk directly at the page using JavaScript. You’ll find a ton of great support for accessing shadow dom stuff from JS.


shadow dom is an abomination and makes HTML insanely complicated. There is no need at all for HTML to be written like that. Encapsulated objects in a stream of text? Not clever ← stupid.

Hi Russ & kazurayam , Thanks for your time and looking into the issue. The issue is resolved now using js path.

WebUI.switchToFrame(findTestObject('Object Repository/Page_Quote Salesforce/shadow_DomIframe'), 15, FailureHandling.STOP_ON_FAILURE)

WebUI.delay(10)

//SaveAndCalculate

def SaveAndCalculate = '''
document.querySelector("#sbPageContainer")
    .shadowRoot.querySelector("#content > sb-line-editor")
        .shadowRoot.querySelector("#actions > sb-custom-action:nth-child(7)")
            .shadowRoot.querySelector("#mainButton")
'''

CustomKeywords.'web.ShadowDom.clickDomElement'(SaveAndCalculate)
package web
import ...
class ShadowDom {

    @Keyword
    def clickDomElement(String JSPath) throws StepFailedException {
	    WebDriver driver = DriverFactory.getWebDriver();
	    JavascriptExecutor jsExecutor = ((JavascriptExecutor) driver)

	    WebElement elementLocator = ((jsExecutor.executeScript("return " + JSPath) as WebElement))

	    if (elementLocator.isDisplayed() && elementLocator.isEnabled()) {
		    WebUI.delay(3)
		    elementLocator.click()
		    KeywordUtil.markPassed("---> Element is clicked successfully")
	    }
	    else {
		    KeywordUtil.markFailedAndStop("---> Element is not clicked successfully")
	    }
    }
}

Thanks!

Excellent. I knew JS would give you better oversight and control. Good job.

@naveenkumar.murugesa

I slight edited your post to introduce “code formatting syntax” of Discourse to make it better readable

I hope you accept my edit

1 Like