仅列出表的前3列的<webelement>

时间:2016-11-17 17:01:29

标签: java selenium selenium-webdriver junit

如果说findElements(By.xpath("td[]")),那么如何只获取td[1]中的td[2]td[3]List元素?

WebElement Webtable = driver.findElement(By.xpath(""));
List<WebElement> TotalRowCount = Webtable.findElements(By.xpath(""));
int RowIndex = 1;
for (WebElement rowElement : TotalRowCount) {
    List<WebElement> TotalColumnCount = rowElement.findElements(By.xpath("td[]"));
    int ColumnIndex = 1;
    for (WebElement colElement : TotalColumnCount) {
        ColumnIndex = ColumnIndex +1;
    }
    RowIndex = RowIndex + 1;
}

2 个答案:

答案 0 :(得分:0)

只需将这些项目添加到您的列表中即可。

import io
import os
from notebook.utils import to_api_path

_script_exporter = None

def script_post_save(model, os_path, contents_manager, **kwargs):
    """convert notebooks to Python script after save with nbconvert

    replaces `ipython notebook --script`
    """
    from nbconvert.exporters.script import ScriptExporter

    if model['type'] != 'notebook':
        return

    global _script_exporter
    if _script_exporter is None:
        _script_exporter = ScriptExporter(parent=contents_manager)
    log = contents_manager.log

    base, ext = os.path.splitext(os_path)
    py_fname = base + '.py'
    script, resources = _script_exporter.from_filename(os_path)
    script_fname = base + resources.get('output_extension', '.txt')
    log.info("Saving script /%s", to_api_path(script_fname, contents_manager.root_dir))
    with io.open(script_fname, 'w', encoding='utf-8') as f:
        f.write(script)

c.FileContentsManager.post_save_hook = script_post_save

然后,您想要的列元素将存储在数据中。

答案 1 :(得分:0)

您应该尝试使用xpath的{​​{3}}函数,如下所示,每行只会选择前三列: -

List<WebElement> TotalColumnCount = rowElement.findElements(By.xpath("td[position() <= 3]"));