How to make a robot take data from a table in an HTML website

Asked

Viewed 2,247 times

0

I’m trying to create a database with stock histories extracted from a website. Below is an example of what I’m trying to do.

package com.gustavo.seleniumTest;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class seleniumTest {

public static void main(String[] args) {
    System.setProperty("webdriver.gecko.driver", "/home/gustavo/geckodriver");
    WebDriver driver = new FirefoxDriver();

    String valor;
    driver.get("http://cotacoes.economia.uol.com.br/acao/cotacoes-historicas.html?codigo=PETR4.SA&size=200&page=1&period=");
    valor = driver.findElement(By.xpath(".//*[@class='odd']")).getText();
    System.out.println(valor);
}

}

I was able to create a robot with Selenium to insert values into a text box and press a button, but I’m having difficulty extracting values from a table or tag (HTML) and add to a variable or array.

  • Knows the Jsoup? This lib allows you to extract data from HTML

1 answer

1

Well whenever I needed to do something of the kind with tables, I used a full scan using the xpath below (I already put the table id you passed on the link):

int lineNumber = 1;
int columnIndex = 1;

String valueXpath = ".//table/tbody[@id='tblInterday']/tr[" + lineNumber + "]/td[" + columnIndex + "]"; // linha 1 x coluna 1
WebElement element = driver.findElement(By.xpath(valueXpath));
String elementValue = element.getText().trim();

The above Xpath will give you the element that contains the value of the cross row x column, what you need to do now is create the code that scans the entire table, ie all rows and columns and store each value of this.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.