question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Find element by text (content)

See original GitHub issue

Is there a built-in locator to find an element by its html content?

This is for example the xpath expression to locate any element containing the string “You have successfully signed up!”

by.xpath("//*[contains(text(),'You have successfully signed up!')]");

I wonder if there is a nicer DSL for this, e.g.

by.text('You have successfully signed up!');

Or see an example of how to write this with Protractor.By.addLocator

Issue Analytics

  • State:closed
  • Created 10 years ago
  • Reactions:1
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

31reactions
elhigucommented, Sep 30, 2015

I was mislead by this issue… currently correct answer seems to be:

var dog = element(by.cssContainingText('.pet', 'Dog'));

https://angular.github.io/protractor/#/api?view=ProtractorBy.prototype.cssContainingText

0reactions
rolobmcommented, Apr 27, 2017

Hi guys, somebody knows how declare locators for this case:

  • Phone_assign_43534
  • image

    Read more comments on GitHub >

    github_iconTop Results From Across the Web

    How to get element by innerText - javascript - Stack Overflow
    Use document.getElementsByInnerText() to get multiple elements (multiple elements might have the same exact text), and use document.
    Read more >
    Find Element by Content using JavaScript | bobbyhadz
    To find elements by content, use the `document.querySelectorAll` method to select dom elements by tag. Then, use the `for...of` loop to iterate over...
    Read more >
    How to Find Element by Text in Selenium: Tutorial
    Launch the Chrome browser · Navigate to BrowserStack's website · Locate the CTA with the text value 'Get started free' using the XPath...
    Read more >
    Selenium Find Element By Text Tutorial with Examples
    Selenium Find element by text is used to locate a web element using its text value. The text value is generally used when...
    Read more >
    How To Find Element By Text In Selenium WebDriver
    The 'findElement' method returns a WebElement object. This object has a 'getText' method, which returns the visible text on that element. To ...
    Read more >

    github_iconTop Related Medium Post

    No results found

    github_iconTop Related StackOverflow Question

    No results found

    github_iconTroubleshoot Live Code

    Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
    Start Free

    github_iconTop Related Reddit Thread

    No results found

    github_iconTop Related Hackernoon Post

    No results found

    github_iconTop Related Tweet

    No results found

    github_iconTop Related Dev.to Post

    No results found

    github_iconTop Related Hashnode Post

    No results found