Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/314.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 can';使用selenium webdriver无法在yahoo finance中找到股票价格_Java_Selenium Webdriver_Jexcelapi - Fatal编程技术网

Java can';使用selenium webdriver无法在yahoo finance中找到股票价格

Java can';使用selenium webdriver无法在yahoo finance中找到股票价格,java,selenium-webdriver,jexcelapi,Java,Selenium Webdriver,Jexcelapi,在过去的两天里,我一直在试图找到一种方法,从雅虎金融(yahoo finance)获取股票数据。我以前使用过SeleniumWebDriver,我发现它非常有用,但是我似乎无法让它定位股票的价格。在finance.yahoo.com中,html相当复杂,但我认为您可以隔离标记,因此我的代码如下所示: public void writeSheet() throws WriteException, Exception{ int r = 0; for(String s : listOf

在过去的两天里,我一直在试图找到一种方法,从雅虎金融(yahoo finance)获取股票数据。我以前使用过SeleniumWebDriver,我发现它非常有用,但是我似乎无法让它定位股票的价格。在finance.yahoo.com中,html相当复杂,但我认为您可以隔离标记,因此我的代码如下所示:

public void writeSheet() throws WriteException, Exception{
    int r = 0;
    for(String s : listOfFunds){
        Label stockLabel = new Label(0,r,s);
        Number stockPrice = new Number(1,r,0.00);
        driver.get("http://finance.yahoo.com/q?s=" + s + "%2C+&q1=q");
        Thread.sleep(200);
        WebElement price = driver.findElement(By.tagName("span"));
        stockPrice.setValue(Double.parseDouble(price.findElement(By.id("yfs_184_" + s.toLowerCase())).getText()));
        id=\"yfs_184_" + s.toLowerCase() + "\""));
        sourceSheet.addCell(stockLabel);
        sourceSheet.addCell(stockPrice);
        r++;
    }
    sourceBook.write();
    sourceBook.close();
}
<span id="yfs_184_" + insert symbol to lowercase here + ">stock price</span>
我只是想检查一下,看我是否遗漏了任何愚蠢的东西,或者我是否在按标签名搜索。我要查找的HTML行如下所示:

public void writeSheet() throws WriteException, Exception{
    int r = 0;
    for(String s : listOfFunds){
        Label stockLabel = new Label(0,r,s);
        Number stockPrice = new Number(1,r,0.00);
        driver.get("http://finance.yahoo.com/q?s=" + s + "%2C+&q1=q");
        Thread.sleep(200);
        WebElement price = driver.findElement(By.tagName("span"));
        stockPrice.setValue(Double.parseDouble(price.findElement(By.id("yfs_184_" + s.toLowerCase())).getText()));
        id=\"yfs_184_" + s.toLowerCase() + "\""));
        sourceSheet.addCell(stockLabel);
        sourceSheet.addCell(stockPrice);
        r++;
    }
    sourceBook.write();
    sourceBook.close();
}
<span id="yfs_184_" + insert symbol to lowercase here + ">stock price</span>

我试过这个,效果很好。

WebElement stockpriceText = driver.findElement(By.cssSelector("span[id=\"yfs_l84_ctsh\"]"));
String price = stockpriceText.getText();
输出:51.59

我认为你的定位策略有点错误。您正在尝试首先在以下内容中查找跨度:

WebElement price = driver.findElement(By.tagName("span"));
这可能是问题所在。WebDriver将在页面上找到第一个
元素。它可能是DOM中的其他跨度。
另外,在执行此操作时:

price.findElement(By.id("yfs_184_" + s.toLowerCase())).getText();

当你试图在跨度元素中找到另一个元素时,它就不起作用了。

你得到的错误是什么?@ JDave1984:如果它对你有用的话,请考虑接受或支持这个答案。我回家后会试试这个。我曾尝试将所有元素加载到一个列表中,但没有成功。我甚至没有想到CSS选择器。我会让你知道结果如何。谢谢我试过了。。。我得到了无接触的例外。不知道我做错了什么是的,它不起作用。该css在任何雅虎财经页面的源代码中都没有显示。我没有正确加载页面吗?你是用你以前的方法还是用我的方法?css和css选择器是不同的东西。您不应该查看页面css源中的定位器
span[id=\“yfs\u l84\u ctsh\”