java从网页抓取数据( html页面中显示的表数据 )

优采云 发布时间: 2022-03-18 06:05

  java从网页抓取数据(

html页面中显示的表数据

)

  Java 使用 SeleniumWebDriver 从网页中提取表格数据

  javahtmlseleniumxpathselenium-webdriver

  Java 使用 SeleniumWebDriver,java,html,selenium,xpath,selenium-webdriver,Java,Html,Selenium,Xpath,Selenium Webdriver 从网页中提取表数据,我使用 SeleniumWebDriver(在 Eclipse 中)自动化 Web 应用程序,但现在我需要 Capture在 html 页面中显示的表格数据。我尝试了给出的解决方案,其他几个 网站,但我们的网页显示表格的方式似乎有点不同尝试使用 div 类名获取值,如 String Text=driver.findElements(By.xpath("/ /div[@class='ag-row-ag-row-even-ag-row-level-0']//tr").get(0).getText() 但它不起作用,索引越界异常

  我正在使用 SeleniumWebDriver(在 Eclipse 中)来自动化 Web 应用程序,但现在需要捕获显示在 html 页面中的表数据。我尝试了给出的解决方案,其他几个网站,但我们的网页似乎显示的表格有点不同

  尝试使用 div 类名获取值,例如

  String Text=driver.findElements(By.xpath(“//div[@class='ag-row-ag-row-even-ag-row-level-0']//tr”)。get(0).getText()但是它不起作用,索引越界异常被抛出我不确定,但您的webElements数组可能是空的,这就是为什么会出现索引越界异常</p>

如果您试图从整个WW_SALES行中获取值,我认为find_元素应该去掉父div-class=“ag row ag row偶数ag-row-level-0”</p>

这只是我根据所附的描述和图片做出的假设</p> 据我所见,您似乎已经创建了一个自定义表。

从所附图像中的HTML摘录来看,结构类似于:</p>

... etc

... etc

</p>

  结果是一个空数组(有趣的是,你没有得到

  nosucheElement

  例外,也许在 html 树的某个地方有一些

  tr

  标记)

  现在,我不确定您要从该表中提取哪些数据,但您的最佳尝试是基于

  class

  属性获取所有行,并且对于每一行,基于

  class

  属性来获取所有列数据(或者你甚至可以使用

  col

  属性)

  编辑:要获取所有元素,您可以获取所有行,然后获取每行的所有列数据:

  //Get all the rows from the table

List rows = driver.findElements(By.xpath("//div[contains(@class, 'ag-row')));

//Initialize a new array list to store the text

List tableData = new ArrayList();

//For each row, get the column data and store into the tableData object

for (int i=0; i < rows.size(); i++) {

//Since you also have some span tags inside (and maybe something else)

//we first get the div columns

WebElement tableCell = rows.get(i).findElements(By.xpath("//div[contains(@class, 'ag-cell')]"));

tableData.add(tableCell.get(0).getText());

}

<p>//从表中获取所有行

List rows=driver.findElements(By.xpath(//div[contains(@class,'ag row'));

//初始化新数组列表以存储文本

List tableData=new ArrayList();

//对于每一行,获取列数据并存储到tableData对象中

对于(int i=0;i

0 个评论

要回复文章请先登录注册


官方客服QQ群

微信人工客服

QQ人工客服


线