Riggs
Riggs

Reputation: 49

Selenium webdriver xpath filtering

What is wrong with my xpath syntax? The nodes that I am trying to filter through are and my code is:

div class="cmeTableBlockWrapper cmeContentSection cmeContentGroup"
table id="quotesFuturesProductTable1" 
tr[1:]
   td id="quotesFuturesProductTable1_ZCZ4_last"
   td id="quotesFuturesProductTable1_ZCZ4_change"
   td id="quotesFuturesProductTable1_ZCZ4_priorSettle"

url = "http://www.cmegroup.com/trading/agricultural/grain-and-oilseed/corn.html"

driver = webdriver.Chrome() 

driver.get(url) 

table = driver.find_element_by_xpath('//div[class="cmeTableBlockerWrapper cmeContentSection cmeContentGroup"]/table[@id="quotesFuturesProductTable1"]') 

# or table = driver.find_element_by_xpath('//td[contains(div[@class="fixedpage_heading"], "CORN")]/table[@class="homepage_quoteboard"]')

for row in table.find_elements_by_tag_name('tr')[1:]:  
    priorsettle = str(row.find_element_by_id('quotesFuturesProductTable1_ZSX4_priorSettle').text)

Upvotes: 1

Views: 770

Answers (1)

alecxe
alecxe

Reputation: 473753

Find the table by id, get the appropriate row by index (easiest way here):

table = driver.find_element_by_id('quotesFuturesProductTable1')
for row in table.find_elements_by_tag_name('tr')[2:]:
    print row.find_elements_by_tag_name('td')[4].text

Prints (Prior Settle column):

338'2
350'6
359'2
366'2
374'0
384'2
393'6
400'4
404'4
402'0
402'0
418'2
407'4

Note that we need to skip 2 rows, not a single one, since there are two tr tags before the actual table content starts.

Upvotes: 1

Related Questions