python - 在 Selenium 中快速获取表格的文本

标签 python selenium selenium-webdriver html-table webdriver

我正在尝试使用 Selenium 将表中的多个列解析为字典,但我的速度似乎很慢。我正在使用 python、Selenium 2.0 和 webdriver.Chrome()

table = self.driver.find_element_by_id("thetable")
    # now get all the TR elements from the table
    all_rows = table.find_elements_by_tag_name("tr")

    # and iterate over them, getting the cells
    for row in all_rows:
        cells = row.find_elements_by_tag_name("td")
        # slowwwwwwwwwwwwww
        dict_value = {'0th': cells[0].text,
                 '1st': cells[1].text,
                 '2nd': cells[2].text,
                 '3rd': cells[3].text,
                 '6th': cells[6].text,
                 '7th': cells[7].text,
                 '10th': cells[10].text}

问题似乎是获取每个 td 元素的“文本”属性。有没有更快的方法?

最佳答案

备选方案。

如果稍后(在循环之后),您不需要 selenium 为您提供的交互性 - 您可以传递当前的 HTML source code页面的 lxml.html ,以速度着称。示例:

import lxml.html

root = lxml.html.fromstring(driver.page_source)
for row in root.xpath('.//table[@id="thetable"]//tr'):
    cells = row.xpath('.//td/text()')
    dict_value = {'0th': cells[0],
                  '1st': cells[1],
                  '2nd': cells[2],
                  '3rd': cells[3],
                  '6th': cells[6],
                  '7th': cells[7],
                  '10th': cells[10]}

关于python - 在 Selenium 中快速获取表格的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27592444/

相关文章:

c# - 我如何将整个路径从 python 传递到 c#?

java - ios - appium 无法从下往上滑动

java - 如何从不是 Selenium 下拉菜单的列表中获取值

ruby - 如何在 Capybara 中获取 DOM 元素的 HTML?

html - python 3 Selenium : can't find element visible on screen

python - 如何检查元素是否在屏幕上完全可见?

Python Selenium - 尝试按类名查找元素时出错

python - oursql 中的参数化查询

python - 尝试在循环下打印多个变量

java - 如何使用selenium,java下载文件