在java中使用xpath和selenium解析HTML表格数据

Parsing HTML table data with xpath and selenium in java(在java中使用xpath和selenium解析HTML表格数据)
本文介绍了在java中使用xpath和selenium解析HTML表格数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获取数据并在没有标签的情况下对其进行组织.它看起来像这样

I want to take the data and organize it without the tags. It looks something like this

<table class="SpecTable">
    <col width="40%" />
    <col width="60%" />
    <tr>
        <td class="LightRowHead">Optical Zoom:</td>
        <td class="LightRow">15x</td>
    </tr>
    <tr>
        <td class="DarkRowHead">Digital Zoom:</td>
        <td class="DarkRow">6x</td>
    </tr>
    <tr>
        <td class="LightRowHead">Battery Type:</td>
        <td class="LightRow">Alkaline</td>
    </tr>
    <tr>
        <td class="DarkRowHead">Resolution Megapixels:</td>
        <td class="DarkRow">14 MP</td>
    </tr>
</table>

我希望能够提取所有信息字符串,以便我可以将其存储在纯文本文件中:

and I want to be able to extract all the strings of information so that I can store in a plaintext file with just this:

光学变焦:15 倍数码变焦:6 倍电池类型:碱性分辨率百万像素:14 MP

Optical Zoom: 15x Digital Zoom: 6x Battery Type: Alkaline Resolution Megapixels: 14 MP

public static void main(String[] args) {

        FirefoxProfile profile = new FirefoxProfile();
        profile.setPreference("general.useragent.override", "some UA string");
        WebDriver driver = new FirefoxDriver(profile);

        String Url = "http://www.walmart.com/ip/Generic-14-MP-X400-BK/19863348";
        driver.get(Url);
        List<WebElement> resultsDiv = driver.findElements(By.xpath("//table[contains (@class,'SpecTable')//td"));

        System.out.println(resultsDiv.size());
        for (int i=0; i<resultsDiv.size(); i++) {
            System.out.println(i+1 + ". " + resultsDiv.get(i).getText());
        }

我正在使用 Selenium 进行 Java 编程,但我无法为它找出正确的 XPath 表达式.

I am programming in Java with Selenium and I cannot figure out the correct XPath expression for it.

有人能弄清楚我为什么会犯错,或许能给我一些关于如何正确解析这些数据的指示吗?我对 Selenium 和 XPaths 很陌生,但我需要这个来工作.

Can someone figure out why I err on this and maybe give me some pointers on how I can parse this data correctly? Im very new to Selenium and XPaths but I need this for work.

另外,如果有人有任何好的资源让我快速学习 Selenium 和 XPath,我们也将不胜感激!

Also if anyone has any good sources for me to learn Selenium and XPath fast, those would also be greatly appreciated!

推荐答案

这可能会满足您的需求:

Probably this will suite your needs:

string text = driver.findElement(By.cssSelector("table.SpecTable")).getText();

String text 将包含来自具有类 SpecTable 的表的所有文本节点.我更喜欢使用 css,因为它受 IE 支持并且比 xpath 更快.但是对于 xpath 教程,请尝试 this 和 这个.

String text will contain all text nodes from the table with class SpecTable. I prefer using css, because it's supported by IE and faster than xpath. But as for xpath tutorials try this and this.

这篇关于在java中使用xpath和selenium解析HTML表格数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

How can create a producer using Spring Cloud Kafka Stream 3.1(如何使用Spring Cloud Kafka Stream 3.1创建制片人)
Insert a position in a linked list Java(在链接列表中插入位置Java)
Did I write this constructor properly?(我是否正确地编写了这个构造函数?)
Head value set to null but tail value still gets displayed(Head值设置为空,但仍显示Tail值)
printing nodes from a singly-linked list(打印单链接列表中的节点)
Control namespace prefixes in web services?(控制Web服务中的命名空间前缀?)