Home > other >  XPath only works with full XPath, not by selecting id - Selenium
XPath only works with full XPath, not by selecting id - Selenium

Time:12-07

I have 2 XPath, but only 1 works:

This works: By.XPath("//*[@id=\"app\"]/div[2]/div/div/div/div[1]/div[2]/div[2]/form/input");

Not working: By.XPath("//*[@id=\"input-search\"]");

Also tried:
By.Id("input-search");

Html

<div id="app">
    <div  b-0rz2d3qv9n=""></div>
    <div  b-0rz2d3qv9n="">
        <div  b-d47k65u4lc="">
            <nav  id="app-nav-menu" b-cpvcfm2reo="">
            </nav><div >
                <nav  b-9p487gxfqe="">
                </nav><div >
                    <aside  b-6cu9cqksy9="">
                    </aside>
                    <div >
                        <div ><div><i ></i>Alle ressourcer</div></div>
                        <div  b-2n7grz7ttq="">
                            <div  b-2n7grz7ttq="">
                            </div>
                            <button type="button"  role="button" _bl_20="">
                            </button>
                            <div  b-nz34ntt3ce="">
                                <button type="button"  role="button" _bl_21="">
                                </button>
                                <form b-nz34ntt3ce="">

<input id="input-search"  type="search" placeholder="Søg..." b-nz34ntt3ce="" _bl_19="">

                                </form>
                                <button type="button"  role="button" _bl_22="">
                                </button>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

I'm trying to select my search field, so i can setup selenium to make a search on my webpage. But when i try to use the id, i get this error:

OpenQA.Selenium.ElementNotInteractableException : element not interactable

I don't know why i can't get the Id to work, when the full XPath is working fine. Hope that someone can tell me :)

CodePudding user response:

According to the shared HTML both By.XPath("//*[@id="app"]/div[2]/div/div/div/div[1]/div[2]/div[2]/form/input"); and By.XPath("//*[@id="input-search"]"); should work.
In case OpenQA.Selenium.ElementNotInteractableException : element not interactable appears for the same line i.e. for that changing the first XPath with the second it means that the first XPath is unique on the entire page while the second XPath is not unique so it matches more that 1 element while the first matching element is not what you want to access and that element is not interactable, probably hidden.

  • Related