Get specific element in webdriver containing text

What are some good ways to get a specific element in WebDriver / Selenium2, based only on the text inside the element?

<div class="page"> <ul id="list"> <li>Apple</li> <li>Orange</li> <li>Banana</li> <li>Grape</li> </ul> </div> 

Essentially, I would like to write something like this to get a specific element:

 @driver.find_element(:id, "list").find_element(:text, "Orange") 

This is very similar to the way I will use the selector when searching for text inside a link (i.e :link_text or :partial_link_text ), but I would like to find elements by text inside regular, unrelated links.

Any suggestions? How do you deal with this problem? (If you're interested, I use Ruby.)

+4
source share
2 answers

A few years later, but I just wanted to ask this question and answer it so that others could find it ...

I used the css selector to get all li elements, and then filtered the array based on text:

 @driver.find_elements(css: '#list > li').select {|el| el.text == 'Orange'}.first 

You can then select .click or .send_keys :return .

+3
source

You can do this with xPath. Something like this for your example:

 @driver.find_element(:id, "list").find_element(:xpath, './/*[contains(., "Orange")]') 
+6
source

Source: https://habr.com/ru/post/1402521/


All Articles