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

0 votes

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 based on "My Button" (or non-case-sensitive, partial matches such as "my button" or "button").

I'm finding this amazingly difficult, to the extent to which I feel like I'm missing something obvious. The best thing I have so far is:

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

This is case-sensitive, however. The other thing I've tried is iterating through all the divs on the page, and checking the element.text property. However, every time you get a situation of the form:

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

div.outer also has "My Button" as the text. To fix that, I've tried looking to see if div.outer is the parent of div.inner, but I couldn't figure out how to do that (element.get_element_by_xpath('..') returns an element's parent, but it tests not equal to div.outer).

Also, iterating through all the elements on the page seems to be really slow, at least using the Chrome webdriver

Nov 27, 2020 in Selenium by Rajiv
• 8,910 points
5,771 views

1 answer to this question.

0 votes

Try the following:

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

I hope this helps!

Check out Selenium Training and learn more about Selenium webdriver.

Thanks!

answered Nov 27, 2020 by Gitika
• 65,910 points

Related Questions In Selenium

0 votes
1 answer
0 votes
1 answer

How do I get current URL in Selenium Webdriver using Python?

Use current_url element. Example: print browser.current_url READ MORE

answered Aug 8, 2018 in Selenium by Meci Matt
• 9,460 points
25,654 views
+1 vote
2 answers

How can I delete an element in Selenium using Python?

You can directly delete the node using ...READ MORE

answered Sep 14, 2019 in Selenium by tonystark
• 500 points
18,844 views
0 votes
1 answer
0 votes
2 answers

Finding WebDriver element with Class Name in java

The better way to handle this element ...READ MORE

answered Apr 10, 2018 in Selenium by nsv999
• 5,500 points
12,712 views
0 votes
2 answers

Problem while using InternetExplorerDriver in Selenium WebDriver

enable trusted connection  in internet explorer by ...READ MORE

answered Aug 31, 2020 in Selenium by Sri
• 3,190 points
8,608 views
0 votes
1 answer

Geo-location microphone camera pop up

To Allow or Block the notification, access using Selenium and you have to ...READ MORE

answered May 11, 2018 in Selenium by Samarpit
• 5,910 points
6,680 views
0 votes
2 answers

How to use such xpath to find web elements

xpath are two types. 1) Absolute XPath:    /html/b ...READ MORE

answered Sep 3, 2020 in Selenium by Sri
• 3,190 points
7,551 views
0 votes
3 answers

How do I resolve the ElementNotInteractableException in Selenium WebDriver?

Hi Abish, ElementNotInteractableException occurs when an element is ...READ MORE

answered Aug 21, 2019 in Selenium by Abha
• 28,140 points

edited Aug 4, 2023 by Khan Sarfaraz 102,920 views
0 votes
5 answers

How to use JavaScript in selenium to click an Element?

WebElement element = driver.findElement(By.id("id")); JavascriptExe ...READ MORE

answered Aug 31, 2020 in Selenium by Sri
• 3,190 points
69,357 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP