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

2023-07-13Java开发问题
5

本文介绍了在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表格数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

如何使用 JAVA 向 COM PORT 发送数据?
How to send data to COM PORT using JAVA?(如何使用 JAVA 向 COM PORT 发送数据?)...
2024-08-25 Java开发问题
21

如何使报表页面方向更改为“rtl"?
How to make a report page direction to change to quot;rtlquot;?(如何使报表页面方向更改为“rtl?)...
2024-08-25 Java开发问题
19

在 Eclipse 项目中使用西里尔文 .properties 文件
Use cyrillic .properties file in eclipse project(在 Eclipse 项目中使用西里尔文 .properties 文件)...
2024-08-25 Java开发问题
18

有没有办法在 Java 中检测 RTL 语言?
Is there any way to detect an RTL language in Java?(有没有办法在 Java 中检测 RTL 语言?)...
2024-08-25 Java开发问题
11

如何在 Java 中从 DB 加载资源包消息?
How to load resource bundle messages from DB in Java?(如何在 Java 中从 DB 加载资源包消息?)...
2024-08-25 Java开发问题
13

如何更改 Java 中的默认语言环境设置以使其保持一致?
How do I change the default locale settings in Java to make them consistent?(如何更改 Java 中的默认语言环境设置以使其保持一致?)...
2024-08-25 Java开发问题
13