Coder Perfect

How do I find an element that contains specific text in Selenium WebDriver (Python)?


I’m trying to test a complicated JavaScript interface with Selenium (using the Python interface, and across multiple browsers). I have a number of buttons of the form:

<div>My Button</div>

I’d like to be able to search for buttons using “My Button” as a key phrase (or non-case-sensitive, partial matches such as “my button” or “button”).

I’m having a lot of trouble with this, to the point that I think I’m missing something obvious. So far, the best I’ve come up with is:

driver.find_elements_by_xpath('//div[contains(text(), "' + text + '")]')

However, this is case-sensitive. I’ve also tried iterating through all of the divs on the page and inspecting the element.text property. However, whenever you encounter a situation of the following nature:

<div class="outer"><div class="inner">My Button</div></div>

“My Button” is also the text in div.outer. To correct this, I tried to see if div.outer is the parent of div.inner, but I couldn’t figure out how (element.get element by xpath(‘..’) returns an element’s parent, but it doesn’t test equal to div.outer).

It also appears that iterating through all of the components on the page, at least using the Chrome webdriver, is quite slow.


Here’s a more specific version of the question I posed (and received a response to): How can I acquire the text of an element in Selenium WebDriver without include the text of its children?

Asked by josh

Solution #1

Try the following:

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

Answered by Ricky Sahu

Solution #2

You could use the following XPath expression:

'//div[contains(text(), "{0}") and @class="inner"]'.format(text)

Answered by andrean

Solution #3

In the HTML you’ve provided, you’ve written:

<div>My Button</div>

The content of the text Because my button is innerHTML and there are no whitespaces around it, you can use text() as follows:

my_element = driver.find_element_by_xpath("//div[text()='My Button']")

If the relevant text has whitespaces at the beginning or end:

<div>   My Button</div>

Alternatively, at the end:

<div>My Button   </div>

Alternatively, at both ends:

<div> My Button </div>

You have two possibilities in these situations:

If the text is a variable, you can use the following syntax:

foo= "foo_bar"
my_element = driver.find_element_by_xpath("//div[.='" + foo + "']")

Answered by undetected Selenium

Solution #4

//* This function will look for any HTML tag. If some text is shared between the Button and the div element, and //* is a category, it will not work as planned. If you need to pick a specific element, use the HTML Element tag to do so. Like:

driver.find_element_by_xpath("//div[contains(text(),'Add User')]")
driver.find_element_by_xpath("//button[contains(text(),'Add User')]")

Answered by Ishita Shah

Solution #5

It can also be used with Page Object Pattern, for example:

Try this code:

@FindBy(xpath = "//*[contains(text(), 'Best Choice')]")
WebElement buttonBestChoice;

Answered by Krzysztof Walczewski

Post is based on