Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/359.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Java中使用SeleniumWebDriver获取标记值_Java_Selenium_Selenium Webdriver - Fatal编程技术网

如何在Java中使用SeleniumWebDriver获取标记值

如何在Java中使用SeleniumWebDriver获取标记值,java,selenium,selenium-webdriver,Java,Selenium,Selenium Webdriver,我试着用这个代码做实验。我试图制作一个系统,从一个网站获取数据,并使用拉格朗日插值方法创建多项式。我正在用Java学习Selenium来实现这一点。看一看我的研究成果 package com.gustavo.seleniumTest; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; public clas

我试着用这个代码做实验。我试图制作一个系统,从一个网站获取数据,并使用拉格朗日插值方法创建多项式。我正在用Java学习Selenium来实现这一点。看一看我的研究成果

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

注意:我正在使用linux和Firefox。

在DOM中发布元素(标签之间的全部内容)


如果代码是标记内的文本,则代码可以工作,但是如果文本位于值参数内,例如,您需要获取属性(“值”),以便从元素中提取它。

您需要修改定位器,如果要获取所有元素,则需要使用
driver.findElements()
方法

请尝试以下XPath定位器,该定位器将标识该表的行数:

String xPath = "//table[@id='tblInterday']/tbody//tr";
您可以得到如下行的大小:

int rows = driver.findElements(By.xpath(xPath)).size();
您可以使用循环遍历整行,例如
for
循环,如下所示:

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

}
for(int i=1;i<rows;i++) {
    driver.findElements(By.xpath(xPath+"["+i+"]/td"));
}
for(WebElement element : driver.findElements(By.xpath(xPath+"["+i+"]/td"))) {
    System.out.print(element.getText()+"\t");
}
由于有许多行,您可以将行索引传递给上面的XPath,如下所示:

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

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

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

如果你想正常打印

下面是使用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();
        }
    }
}
import org.openqa.selenium.By;
导入org.openqa.selenium.WebDriver;
导入org.openqa.selenium.chrome.ChromeDriver;
公共类硒测试{
公共静态void main(字符串[]args){
System.setProperty(“webdriver.chrome.driver”,“C:\\NotBackedUp\\chromedriver.exe”);
WebDriver驱动程序=新的ChromeDriver();
驱动程序。获取(“http://cotacoes.economia.uol.com.br/acao/cotacoes-historicas.html?codigo=PETR4.SA&size=200&page=1&period=");
字符串xPath=“//表[@id='tblInterday']/tbody//tr”;
int rows=driver.findElements(By.xpath(xpath)).size();
for(int i=1;i System.out.print(e.getText()+“\t”);
System.out.println();
}
}
}
我希望它有助于……获取数据。。。您正试图准确提取哪些数据?创建/使用拉格朗日插值方法创建多项式是否属于该问题的一部分?