find element by text code example

Example 1: selenium find element by content

driver.find_elements_by_xpath("//*[contains(text(), 'My Button')]")

Example 2: how to locate element by using text

The only locator that works with text is xpath. 
Matching exact text		:    //tag[.=‘text’]
Matching partial text		:    //tag[contains(text(), ’text’)]

Example 3: find web element by text

/**
	 *Get the Web element from its text
	 * @param name
	 * @param elementText
	 * @return WebElement
	 */
    public WebElement getElementByText(String name, String elementText) {
    	
    	return WebUI.getWebElement("xpath","//a[starts-with(@name, '" + name + "') and (text() = '" + elementText + "' or . = '" + elementText + "')]");
    	
    }

Tags:

Misc Example