python - 使用 Selenium 和 python 保存表

标签 python selenium

我正在尝试结合使用 Selenium 和 Python 来存储表格的内容。我的脚本如下:

import sys
import selenium
from selenium import webdriver
from selenium.webdriver.common.keys import Keys

driver = webdriver.Firefox()
driver.get("http://testsite.com")

value = selenium.getTable("table_id_10")

print value

driver.close()

这会打开我感兴趣的网页,然后应该保存我想要的表格内容。我看过这个 question 中的语法它使用 browser.get_table(),但该程序的开头以我不理解的 browser=Selenium(...) 开头。我不确定应该使用什么语法,因为 selenium.getTable("table_id_10") 不正确。


编辑:

我包含了我正在使用的表格的 html 片段:

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

最佳答案

旧的 Selenium RC API 包含一个 get_table 方法:

In [14]: sel=selenium.selenium("localhost",4444,"*firefox", "http://www.google.com/webhp")
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/selenium.py
Definition: sel.get_table(self, tableCellAddress)
Docstring:
    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"

由于您使用的是较新的 Webdriver(又名 Selenium 2)API,因此该代码不适用。


也许可以试试这样的方法:

import selenium.webdriver as webdriver
import contextlib

@contextlib.contextmanager
def quitting(thing):
    yield thing
    thing.close()
    thing.quit()

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

关于python - 使用 Selenium 和 python 保存表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8143023/

相关文章:

python 网络套接字;解析json

java - 在 jenkins 中使用 Docker 从站时,Selenium 驱动程序崩溃并给出 NoSuchSessionException

python - Selenium - 等到另一个元素的子元素出现

python - 绘制连音符列表的直方图 matplotlib

python - 检测 GUI 中的特定按键

firefox - Selenium 在 Windows 中运行 headless Firefox 浏览器

node.js - 在 Ubuntu 服务器上运行 selenium-standalone npm 包

python - 如何使用 python selenium 关闭另一个 chrome 弹出窗口

python - 在 Python 中,如何创建从现在起 15 分钟后的日期时间? 1小时后?

python - ipython并行和非复制发送numpy数组