Mikhail Ramendik
Mikhail Ramendik

Reputation: 1115

Selenium xpath text() - simple selection seemingly not working

I use Selenium WebDriver 2.53.1 on Java, Chrome.

I find an element and save it in a WebElement variable named dropdownMenuList . here is its outerHTML, formatted.

<ul tabindex="-1" class="dropdownMenu apmurldropdownmenu thing menu" role="menu" aria-label="Menu region">
    <li class="menuitem apmurldropdownmenu" tabindex="-1" role="menuitem">
        <div class="thing text">CardNameWebpage</div>
    </li>
    <li class="menuitem apmurldropdownmenu" tabindex="-1" role="menuitem">
        <div class="thing text">CardNameWebpage</div>
    </li>
    <li class="menuitem apmurldropdownmenu" tabindex="-1" role="menuitem">
        <div class="thing text">CardNameWebpage</div>
    </li>
    <!-- react-text: 9 -->
    <!-- /react-text -->
</ul>

Then I search in it by xpath:

dropdownMenuList.findElement(By.xpath("*[text()='CardNameWebpage']"));

And I get a NoSuchElementException. But the HTML has all of three elements with this text. What am I doing wrong?

Upvotes: 1

Views: 3255

Answers (2)

Edited: You need to change to, //*[text()='value'] or //*[contains(text(),'value')].

Upvotes: 1

splash58
splash58

Reputation: 26153

You should put two slashes (//) before because elements which you are looking for are not on top level

"//*[text()='CardNameWebpage']"

To find descendant element of current one (paul tremberth is absolutely right :)), add point at start of the xpath

".//*[text()='CardNameWebpage']"

Upvotes: 3

Related Questions