简体   繁体   English

如何在 Java 中使用 Selenium Webdriver 获取标签值

[英]How to use Selenium Webdriver in Java to get tag values

I tried to use this code to make my experiment.我尝试使用此代码进行实验。 I have tried to make a system to get the data from a website and use the lagrange interpolation method to create a polynomial.我试图制作一个系统来从网站获取数据并使用拉格朗日插值法创建多项式。 I am studying Selenium with Java to do this.我正在用 Java 学习 Selenium 来做到这一点。 Take a look at what I've developed.看看我开发的东西。

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);
}

Note: I'm using linux and Firefox.注意:我使用的是 linux 和 Firefox。

You need to modify your locator and if you want to fetch all the elements then you need to use driver.findElements() method.您需要修改定位器,如果要获取所有元素,则需要使用driver.findElements()方法。

Try the below XPath locator which will identify number of rows that table has:试试下面的 XPath 定位器,它将识别表具有的行数:

String xPath = "//table[@id='tblInterday']/tbody//tr";

and you can get the size of the rows like this:你可以像这样获得行的大小:

int rows = driver.findElements(By.xpath(xPath)).size();

and you can loop through whole rows using loops for example for loop like below:您可以使用循环遍历整行,例如for循环如下:

for(int i=1;i<rows;i++) {

}

The below XPath will identity number of columns in each row based on the row index number:下面的 XPath 将根据行索引号标识每行中的列数:

String xPath = "//table[@id='tblInterday']/tbody//tr[row index number]/td";

As there are many rows, you can pass the row index to the above XPath like below:由于有很多行,您可以将行索引传递给上面的 XPath,如下所示:

for(int i=1;i<rows;i++) {
    driver.findElements(By.xpath(xPath+"["+i+"]/td"));
}

As we are using the driver.findElements() method above, it will hold all the columns elements, we can loop through and print each of them like below:当我们使用上面的driver.findElements()方法时,它将保存所有列元素,我们可以循环遍历并打印每个元素,如下所示:

for(WebElement element : driver.findElements(By.xpath(xPath+"["+i+"]/td"))) {
    System.out.print(element.getText()+"\t");
}

Replace代替

driver.findElements(By.xpath(xPath+"["+i+"]/td")).forEach(e -> System.out.print(e.getText()+"\t"));

with

for(WebElement element : driver.findElements(By.xpath(xPath+"["+i+"]/td"))) {
        System.out.print(element.getText()+"\t");
    }

If you want to print normally.如果要正常打印。

Below is the whole code using Java 8 :以下是使用 Java 8 的整个代码:

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class SeleniumTest {

    public static void main(String[] args) {
        System.setProperty("webdriver.chrome.driver", "C:\\NotBackedUp\\chromedriver.exe");
        WebDriver driver = new ChromeDriver();

        driver.get("http://cotacoes.economia.uol.com.br/acao/cotacoes-historicas.html?codigo=PETR4.SA&size=200&page=1&period=");
        String xPath = "//table[@id='tblInterday']/tbody//tr";
        int rows = driver.findElements(By.xpath(xPath)).size();
        for(int i=1;i<rows;i++) {
            driver.findElements(By.xpath(xPath+"["+i+"]/td")).forEach(e -> System.out.print(e.getText()+"\t"));
            System.out.println();
        }
    }
}

I hope it helps...我希望它有帮助...

Post the element in the DOM(the whole thing between the tags).在 DOM 中发布元素(标签之间的整个内容)。

Your code will work if it's a text inside the tag but if the text is inside a value parameter for example, you will need to getProperty("value") in order to extract it from the element.如果它是标签内的文本,您的代码将起作用,但例如,如果文本在值参数内,您将需要 getProperty("value") 以便从元素中提取它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM