Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in DevOps and Agile by (19.7k points)

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 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.

Any suggestions?

1 Answer

0 votes
by (62.9k points)

You will be looking for any HTML tag, wherein some text is common for Button and div tag. If you need to select any specific-text which you are searching for then you can get it by declaring HTML Element tag like shown below:

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

Or

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

If you are interested to learn Selenium on a much deeper level and want to become a professional in the testing domain, check out Intellipaat’s Selenium online training

Browse Categories

...