skip to Main Content

I’m using a servicenow testing instance where I’m trying to find the element for the search box, and I’ve tried it by ID, by XPath, and other things but so far it keeps giving me the NoSuchElementException. HTML snippet: https://pastebin.com/1Yx7yxmi

In there you’ll see it has an ID for what I think is the searchbox as (showing the xpath I’m copying to try and find it by that): //*[@id="sncwsgs-typeahead-input"]

I’ve tried things like: var searchElement = driver.FindElement(By.Id("sncwsgs-typeahead-input"));

Any ideas? I’m sure its entirely possible I am just really bad with selenium and HTML, so anybody who has tested this and had it working and might know any tricks, that would be handy right now!

I also tried this full xpath:

/html/body/macroponent-f51912f4c700201072b211d4d8c26010//div/sn-canvas-appshell-root/sn-canvas-appshell-layout/sn-polaris-layout//div[2]/div[2]/div[1]/sn-polaris-header//nav/div/div[3]/div[1]/div[1]/div/sn-search-input-wrapper//sn-component-workspace-global-search-typeahead//div/div/div/div/input

MORE INFO:

What works and what doesn’t is shown here:

var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(3));
var myElement = wait.Until(x => x.FindElement(By.CssSelector("macroponent-f51912f4c700201072b211d4d8c26010"))); // this works
var shadowRootElement = myElement.GetShadowRoot(); // no error from here
// this below does not work
//var myElement = wait.Until(x => x.FindElement(By.CssSelector("macroponent-f51912f4c700201072b211d4d8c26010 /deep/ dxcsnas-root")));
//var shadowRootElement = myElement.GetShadowRoot();

The element is nested inside multiple shadow-root items – I’m not certain if that is the problem here. I’ve tried using the selenium c# function "element.GetShadowRoot()" and it doesn’t give me any error on the first element I found when searching further and further "up" the xpath. But all subchildren / sub-shadow-roots don’t allow me to get them for some reason.

If you want to see the whole site html:
https://drive.google.com/file/d/1muvP1FPBh-MGuzKRxGcE8CVZ4lvGWGpp

If it matters I’m trying to use chrome driver – I did see an issue a while back with others having problems accessing things inside shadow dom on chromedriver after a version in 2022 – though I don’t know if that has anything to do with this.

Would be nice to do this the right way and learn how rather than manually move the mouse into position and click it with other non-selenium code!

2

Answers


  1. Chosen as BEST ANSWER

    What ended up working:

    var shadowParent = driver.FindElement(By.CssSelector("macroponent-abc123"));
    var shadowRoot = shadowParent.GetShadowRoot();
    
    var somethingInsideShadowRoot = shadowRoot.FindElement(By.CssSelector("table"));
    

    I had to wrap my head around shadow DOM and its purposes on websites, then I was able to get it sorted out. Thanks to Rahul for pointing me in the right direction!


  2. Can you double check the search box in not a Frame then you need to switch the Frame using its locator other wise have already switched frame prior to perform this action, then you need to switch back to Default

    Tryout this below example from the browser console and see if if this is identifying the element

    If you want to find an element by its id and interact with it using JavaScript directly in the browser console, you can use the document.getElementById method. Here’s the equivalent code in JavaScript:

    var searchElement = document.getElementById("sncwsgs-typeahead-input");
    

    After you retrieve the element, you can manipulate it or trigger actions like setting its value, focusing on it, or even simulating a click. For example, if you want to type some text into the input field, you could do:

    searchElement.value = "your text here"; // Sets the value
    searchElement.focus(); // Focuses on the element
    

    Or, if you want to simulate a click:

    searchElement.click();
    

    Just paste these lines into the browser console to execute them.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search