Morten S
Morten S

Reputation: 107

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

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 class="hidden-not-printing" b-0rz2d3qv9n=""></div>
    <div class="hidden-printing" b-0rz2d3qv9n="">
        <div class="main-layout" b-d47k65u4lc="">
            <nav class="nav-menu__container  nav-close-mobile" id="app-nav-menu" b-cpvcfm2reo="">
            </nav><div class="page">
                <nav class="page-header" b-9p487gxfqe="">
                </nav><div class="page-content">
                    <aside class="left-side-content  aside-panel-flex" b-6cu9cqksy9="">
                    </aside>
                    <div class="primary-content ">
                        <div class="page-content__navigation hidden-desktop"><div><i class="uil uil-angle-right-b"></i>Alle ressourcer</div></div>
                        <div class="page-header-actions__container" b-2n7grz7ttq="">
                            <div class="flex-fit-content" b-2n7grz7ttq="">
                            </div>
                            <button type="button" class="btn btn-light btn-sm searchbutton hidden-desktop" role="button" _bl_20="">
                            </button>
                            <div class="searchbar searchbar-visible" b-nz34ntt3ce="">
                                <button type="button" class="btn btn-light btn-sm searchbutton hidden-mobile hidden-tablet" role="button" _bl_21="">
                                </button>
                                <form b-nz34ntt3ce="">

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

                                </form>
                                <button type="button" class="btn btn-light btn-sm closebutton" 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 :)

Upvotes: 1

Views: 341

Answers (1)

Prophet
Prophet

Reputation: 33361

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.

Upvotes: 2

Related Questions