Selenium: find element by visible Text

23,252

Solution 1

contains(text(), '...') only evaluates first text node child from current context element. This shouldn't be any problem for sample element you posted (see demo, but it wouldn't work with the following element for example, because the first text node inside button is the newline located before span :

<button class="....." data-toggle="dropdown">
<span class="....."/>
Text: Title of the button                        
</button>

To evaluate all text node children of current context element, use the following form instead :

.//*[text()[contains(.,"Text: Title of the button")]]

demo

The above XPath tests individual text node if it contains certain text.

Solution 2

Hi please do it like below

By.xpath("//button[contains(text(),'Text: Title of the button')]")
please put Text: Title of the button in single quote.
Also if more html source code is provided then i can come up with a better locator stratgey

Also you can do it on the basis of Class name as well or by talking attribute data-toggle="dropdown" in xpath as well like //*[@data-toggle='dropdown']

// take class name of the button in list

List<WebElement> buttons= driver.findElements(By.className("className"));
Now you can select your concerned button on the basis of index (in java index starts 
form zero)
Share:
23,252
user3446229
Author by

user3446229

Updated on May 25, 2020

Comments

  • user3446229
    user3446229 about 4 years

    In the html page i have the following balise:

    <button class="....." data-toggle="dropdown">
    Text: Title of the button                        
    <span class="....."/>
    </button>
    

    And I want to detect this buton By the visible text Text: Title of the button the problem is that i'm trying to detect it using the following Xpath

    .//*[contains(text(),"Text: Title of the button")]
    

    But it does'nt work. How i can detect this button?

    FYI: I can't detect it using class because there more than one matching node with this class.

  • user3446229
    user3446229 about 8 years
    In fact my question is is-there onother way to detect element By innner Text except Contains(text()," ") because all the other solution are not the better way in my case.There are more than 12 matching node with Class and more than 10 matching node with data-toggle, I have more than 5 button and I want to detect them with general solution based on the visible text.
  • eduliant
    eduliant about 8 years
    ok plz confirm By.xpath("//button[contains(text(),'Text: Title of the button')]") this line worked or not
  • user3446229
    user3446229 about 8 years
    No this line doesn't work i have tried before!. I have examples with no balise Inside the element , only the text and it work but in my cas no.
  • eduliant
    eduliant about 8 years
    can you post a complete source code of your problem area
  • user3446229
    user3446229 about 8 years
    Sorry, the source code is confidential and i can't post it. Thank you for your help.