Using Selenium and python to save a table

I'm trying to use Selenium with Python to store the contents of a table. My script is as follows:

import sys import selenium from selenium import webdriver from selenium.webdriver.common.keys import Keys driver = webdriver.Firefox() driver.get("") value = selenium.getTable("table_id_10") print value driver.close()

This opens up the webpage I am interested in, and then should save the contents of the table that I want. I've seen the syntax in this question which uses browser.get_table(), but the beginning of that program begins with browser=Selenium(...) which I did not understand. I'm not sure what syntax I should be using as selenium.getTable("table_id_10") is incorrect.


I included a html snippet of the table that I am using:

<table class="datatable" cellspacing="0" rules="all" border="1" id="table_id_10" style="width:70%;border-collapse:collapse;"> <caption> <span class="captioninformation right"><a href="Services.aspx" class="functionlink">Return to Services</a></span>Data </caption><tr> <th scope="col">Read Date</th><th class="numericdataheader" scope="col">Days</th><th class="numericdataheader" scope="col">Values</th> </tr><tr> <td>10/15/2011</td><td class="numericdata">92</td><td class="numericdata">37</td> </tr><tr class="alternaterows"> <td>7/15/2011</td><td class="numericdata">91</td><td class="numericdata">27</td> </tr><tr> <td>4/15/2011</td><td class="numericdata">90</td><td class="numericdata">25</td> </table>

-------------Problems Reply------------

The old Selenium RC API included a get_table method:

In [14]: sel=selenium.selenium("localhost",4444,"*firefox", "")
In [19]: sel.get_table?
Type: instancemethod
Base Class: <type 'instancemethod'>
String Form: <bound method selenium.get_table of <selenium.selenium.selenium object at 0xb728304c>>
Namespace: Interactive
File: /usr/local/lib/python2.7/dist-packages/selenium/
Definition: sel.get_table(self, tableCellAddress)
Gets the text from a cell of a table. The cellAddress syntax
tableLocator.row.column, where row and column start at 0.

'tableCellAddress' is a cell address, e.g. "foo.1.4"

Since you are using the newer Webdriver (a.k.a Selenium 2) API, that code does not apply.

Perhaps try something like this instead:

import selenium.webdriver as webdriver
import contextlib

with contextlib.closing(webdriver.Firefox()) as driver:
for tr in driver.find_elements_by_xpath('//table[@id="table_id_10"]//tr'):
if tds:
data.append([td.text for td in tds])
# [[u'10/15/2011', u'92', u'37'], [u'7/15/2011', u'91', u'27'], [u'4/15/2011', u'90', u'25']]

Category:python Views:3 Time:2011-11-15

Related post

  • Using Selenium's Python API - How do I get the number of rows in a table? 2011-02-04

    How do I get the number of rows in an HTML table using Selenium's Python API? I'd have a 2 column table of Keys and Values and I'd like to read it into a dictionary. Something like: browser = Selenium(...) ... rows = ? (this is what I need) for r = r

  • Python: Parsing Word document Table and Save CSV file 2012-04-03

    I would like to save some tables in word document to CSV file or Excel doesn't matter. I tried to "readlines()" it doesn't work! I don't know know. Tables in word document are like this.. Name Age Gender Alex 12 F Willy 14 M . . . However, I would li

  • How to save a table (with its rows, columns and cells) in one single field in a SQL database 2009-07-17

    How to save a table (with its rows, columns and cells) in one single field in a SQL database and then display it on a webpage --------------Solutions------------- You can select table as xml, and the XML result insert/update into the target cell as s

  • fastercsv - save object table in one go (ruby) 2010-02-11

    I read my csv using the line below data = FCSV.table("test.csv", {:quote_char => '"', :col_sep =>',', :row_sep =>:auto, :headers => true, :return_headers => false, :header_converters => :downcase, :converters => :all} ) QUESTION

  • advice how to save html table to SQL server 2010-03-03

    I am trying to save html table to sql server table with unique name and save its data in a database. For example, I have a html table like this(it is dynamically created in browser by user). I use, C#, Sql Server 2008 express. How can after c

  • Selenium Webdriver python bindings 2010-05-22

    I can't make python bindings for webdriver workable. Here is tutorial for installing. easy_install webdriver Won't find webdriver package so I have to install it manually from sources. I've downloaded source from trunk, set WEBDRIVER and PYTHONPATH v

  • How to save html table data to SQL Server 2008 Table Value 2010-07-21

    **** above url contain a html table.I want to save this table value on XML and also want to save this table value on database MS2008. How to save html table values on database --------------Solutions---------

  • selenium 2 web driver save as dialog 2011-01-18

    using selenium 2 with webdriver one of my scripts needs to save a report in pdf or csv both just bring up a save as dialog. I do not know how to interact with it. im trying in firefox for now but would like to get it working in all browsers. I read a

  • Python: pretty-printing ascii tables? 2011-05-06

    I'm looking for a python library for printing tables like this: ======================= | column 1 | column 2 | ======================= | value1 | value2 | | value3 | value4 | ======================= I've found asciitable but it doesn't do the border

  • How to Save SQL Table as XML file? 2011-07-08

    I've done some searching around regarding the procedures used to store an SQL table as an XML file, but I'm still fuzzy about how to achieve it. It seems like the XML AUTO method is the most used method for generating the XML formatted table, but thi

  • Calling custom assert function from Selenium RC (Python) with a custom locator function in user-extensions.js 2011-07-24

    I have written a custom assert function in user-extensions.js that uses a custom locator function - also implemented in user-extensions.js - to locate a particular element on a page. Without getting into details; I need the custom locator function be

  • Attempting to run Selenium IDE Python scripts as test suite 2011-08-29

    I have a couple of problems with running more than one Python test script exported by Selenium IDE Python Remote Control plugin formatter. 1) After a python script is completed the browser window automatically closes. I am running tests in Firefox, f

  • Running javascript in Selenium using Python 2011-10-17

    I am totally new to Selenium. I want to execute a javascript snippet in the following code(as commented in the code), but can't do so. Please help. from selenium import webdriver import selenium from selenium.common.exceptions import NoSuchElementExc

  • Is there a way to perform a mouseover (hover over an element) using Selenium and Python bindings? 2011-11-24

    Reading here ,there apparently used to be a RenderedWebElement class with a hover method.It,however,was exclusively for Java (Searched the python bindings documentation here to no avail) and has since been deprecated for Java. A hover can't be perfor

  • Upload file with Selenium in Python 2011-12-08

    Is it possible to upload file attachment with selenium in Python script? --------------Solutions------------- If there is a form with file input on the page, I think it's straightforward to fill value in the input and submit the form with python api

  • Periodic workspace save could not save safe table? 2012-03-02

    I'm a newbie and I'm using the IBM RAD which is based on Eclipse. After install I deleted some files that seemed not needed in C:\workspace. Now I get this error: "Periodic workspace table can not be saved, file could not be found, could not save fil

  • how do i find node and its child nodes using selenium with python 2012-03-03

    Please let me know the code syntax how to use Node.childNodes from the documentation I am beginner to python and selenium. I have tried using: elem = self.browser.find_element_by_id("pie4") x

  • Send keys not working in selenium with python 2012-04-04

    send keys are not working in selenium with python. This was working earlier but it fails now. I don't know what may be the issue . It gives following error : u"Element is disabled and so may not be used for actions\nCommand duration or timeout: 11 mi

  • I made a table to put in my tax info, now I want to save the table for next year without the info in it. How do I do that 2014-10-11

    I made a table to put in my tax info, now I want to save the table for next year without the info in it. How do I do that --------------Solutions------------- Delete the variable content of the table? Save with a new name. Without more information th

Copyright (C), All Rights Reserved.

processed in 0.085 (s). 11 q(s)