Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.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
我无法使用Selenium with java从以下网站中获取表的内容_Java_Selenium_Selenium Webdriver_Web_Web Scraping - Fatal编程技术网

我无法使用Selenium with java从以下网站中获取表的内容

我无法使用Selenium with java从以下网站中获取表的内容,java,selenium,selenium-webdriver,web,web-scraping,Java,Selenium,Selenium Webdriver,Web,Web Scraping,从以下站点 页面加载后,点击“策略测试仪->交易列表”选项卡 在那里,您可以看到一个表,它正在动态变化,是否有任何方法可以删除该表内容。是的,您可以通过滚动直到到达表的末尾来删除该表内容: 通过在此处应用递归,您可以实现以下目标: (我假设您已进入“战略测试人员->交易列表”选项卡): 代码如下: package com.demo.core; import java.util.List; import java.util.Scanner; import org.openqa.selenium

从以下站点 页面加载后,点击“策略测试仪->交易列表”选项卡


在那里,您可以看到一个表,它正在动态变化,是否有任何方法可以删除该表内容。

是的,您可以通过滚动直到到达表的末尾来删除该表内容:
通过在此处应用递归,您可以实现以下目标:

(我假设您已进入“战略测试人员->交易列表”选项卡):
代码如下:

package com.demo.core;

import java.util.List;
import java.util.Scanner;

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

public class TableParser {

    public static void main(String[] args) {
        System.setProperty("webdriver.chrome.driver", "J:\\STADIUM\\selenium-demo\\src\\main\\resources\\drivers\\chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        driver.navigate().to("https://www.tradingview.com/chart/EhIMW8kQ/");
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter any integer to continue : ");
        int x = sc.nextInt();

        // I have used Scanner here just to hold the execution until I reach "Strategy Tester -> List of Trades" tab manually
        List<WebElement> rows = driver.findElements(By.cssSelector("div.report-content.trades .report-data .table-wrap table tbody"));
        WebElement tableView = driver.findElement(By.className("report-content"));
        int count = 0;

        printTableDataRecursively(rows, count, driver, tableView);
    }

    /** It will print the table data using recursion
     * @param rows First list of rows
     * @param count
     * @param driver
     * @param tableView scrollable table view element
     */
    public static void printTableDataRecursively(List<WebElement> rows, int count, WebDriver driver, WebElement tableView) {
        boolean bottomOfTableReached = checkIfBottomOfViewReached(driver, tableView); // checking if end of table is reached
        for (WebElement row : rows) {
            count++;
            System.out.println(row.getAttribute("textContent")); 
            if (count == rows.size() && !bottomOfTableReached) {
                count = 0;
                scrollToElement(driver, row); // scrolling to last row element from list of rows
                rows = driver.findElements(By.cssSelector("div.report-content.trades .report-data .table-wrap table tbody")); // getting new list of rows
                rows.remove(0); // removing first row element because it was the last row from previous list of rows
                printTableDataRecursively(rows, count, driver,tableView);
            }
        }
    }

     /** It will check if scroll has reached to bottom of an HTML element that is scrollable.
     * @param driver
     * @param element 
     * @return true (if bottom reached) otherwise false
     */
    public static boolean checkIfBottomOfViewReached(WebDriver driver, WebElement element) {
         return  (boolean) ((JavascriptExecutor)driver).executeScript("if (arguments[0].scrollHeight == arguments[0].offsetHeight + arguments[0].scrollTop) { return true; } else { return false; }", element);
     }

     /** It will scroll to the given WebElement.
     * @param driver
     * @param element
     */
    public static void scrollToElement(WebDriver driver, WebElement element) {
            ((JavascriptExecutor)driver).executeScript("arguments[0].scrollIntoView();", element);
        }

}
package com.demo.core;
导入java.util.List;
导入java.util.Scanner;
导入org.openqa.selenium.By;
导入org.openqa.selenium.JavascriptExecutor;
导入org.openqa.selenium.WebDriver;
导入org.openqa.selenium.WebElement;
导入org.openqa.selenium.chrome.ChromeDriver;
公共类表解析器{
公共静态void main(字符串[]args){
System.setProperty(“webdriver.chrome.driver”,“J:\\STADIUM\\selenium demo\\src\\main\\resources\\drivers\\chromedriver.exe”);
WebDriver驱动程序=新的ChromeDriver();
驱动程序。导航()。到(“https://www.tradingview.com/chart/EhIMW8kQ/");
扫描仪sc=新的扫描仪(System.in);
System.out.println(“输入任何整数以继续:”);
int x=sc.nextInt();
//我在这里使用Scanner只是为了保持执行,直到我手动到达“StrategyTester->List of Trade”选项卡
列表行=driver.findElements(By.cssSelector(“div.report-content.trades.report data.table wrap table tbody”);
WebElement tableView=driver.findElement(By.className(“报告内容”);
整数计数=0;
递归打印TableData(行、计数、驱动程序、tableView);
}
/**它将使用递归打印表数据
*@param rows第一行列表
*@param计数
*@param驱动程序
*@param tableView可滚动表视图元素
*/
公共静态void printTableDataRecursive(列表行、int计数、WebDriver驱动程序、WebElement tableView){
boolean BottomOfTableReach=CheckIfBottomOfViewReach(驱动程序,tableView);//检查是否到达表的末尾
for(WebElement行:行){
计数++;
System.out.println(row.getAttribute(“textContent”);
if(count==rows.size()&&!bottomoftable已到达){
计数=0;
scrollToElement(驱动程序,行);//滚动到行列表中的最后一行元素
rows=driver.findElements(By.cssSelector(“div.report-content.trades.report data.table wrap table tbody”);//获取新的行列表
rows.remove(0);//删除第一行元素,因为它是上一行列表中的最后一行
递归打印TableData(行、计数、驱动程序、tableView);
}
}
}
/**它将检查scroll是否已到达可滚动的HTML元素的底部。
*@param驱动程序
*@param元素
*@返回true(如果到达底部),否则返回false
*/
公共静态布尔checkifbottomofviewer(WebDriver驱动程序、WebElement元素){
return(boolean)((JavascriptExecutor)driver.executeScript(“if(arguments[0]。scrollHeight==arguments[0]。offsetHeight+arguments[0]。scrollTop){return true;}else{return false;}”,元素);
}
/**它将滚动到给定的WebElement。
*@param驱动程序
*@param元素
*/
公共静态无效scrollToElement(WebDriver驱动程序、WebElement元素){
((JavascriptExecutor)driver.executeScript(“参数[0].ScrollingToView();”,元素);
}
}
我总共得到291行(最后3或4行包含重复),我认为您可以自己解决这个问题

我的观点是,您必须滚动并获取行数据,直到到达表的末尾。

只需运行这个程序,并在控制台中查看输出


希望这能帮助您实现目标。

您可以放弃数据,但数据会在一段时间后发生变化。你必须定期删除数据以获取最新数据。我已经尝试过了。但是我只得到了前6行。请添加您尝试的代码块。您好@dangi13,我用python测试了您的代码,在表中动态添加了一个新数据时出现了一个错误,有没有解决这个问题的建议?