Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Text From All Elements Matching a Pattern in Selenium

I have a site with elements of the form:

<td id="subject_23432423">content I want to read</td>

How do I use Selenium RC (with the Python bindings specifically) to read the content from all these elements? I've gone through all the commands, and while there's a lot of options to find a single element, none of the commands seem to handle lists of multiple matches. For example, I can find the content of a specific element using:

content = sel.get_text("td[@id='subject_23432423']")

but this assumes I already know the id, which I don't because it's generated dynamically.

like image 492
Cerin Avatar asked May 30 '26 02:05

Cerin


1 Answers

What I would do is one of the following techniques

count = sel.get_xpath_count("xpath=//td[starts-with(@id,'subject_')]")
someArray = []
for i in count:
  someArray[i] = sel.get_text("xpath=//td[starts-with(@id,'subject_')][" + i + "]")

or for a more effiecent way to use BeautifulSoup or lxml

html = sel.get_html_source()
from BeautifulSoup import BeautifulSoup
soup = BeautifulSoup(html)
#use beautifulsoup to do what you want
like image 65
AutomatedTester Avatar answered Jun 01 '26 14:06

AutomatedTester