Java+Selenium3实战1:利用List对表格列数据进行校验

本文介绍了一种使用Selenium WebDriver抓取网页表格特定列数据并与预期结果进行比对的方法。通过实例演示了如何利用Java编程语言实现这一过程,包括初始化WebDriver、定位元素、获取文本及比较数据等关键步骤。

1、实战问题

Web界面存在1个table,想通过抓取table指定列的数据跟预期结果做对比。table如下:



2、解决思路

1)利用driver.findElements抓取对象,存放在List<String> list_element中

2)将预期结果存放在List<String> list中

3)对2个List<String>进行比对

3、源代码

package com.example.tests;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class TestCompare {

	WebDriver driver;

	@Before
	public void setUp() throws Exception {

		System.setProperty("webdriver.chrome.driver", ".\\Tools\\chromedriver.exe");

		driver = new ChromeDriver();

		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);

		driver.manage().window().maximize();

	}

	@Test
	public void test() {

		driver.get("http://www.cnblogs.com/Ming8006/p/5727542.html#top");

		List<WebElement> element_all = driver
				.findElements(By.xpath("//*[@id='cnblogs_post_body']//table//tbody//td[@class='xl66']"));

		List<String> list = new ArrayList<String>();
		List<String> list_element = new ArrayList<String>();
		list.add("If   this element is a text entry element, this will clear the value.");
		list.add("Click   this element.");
		list.add("Find   the first WebElement  using the given method.");
		list.add("Find   all elements within the current context using the given mechanism.");
		list.add("Get   the value of a the given attribute of the element.");
		list.add("Get   the value of a given CSS property.");
		list.add("Where   on the page is the top left-hand corner of the rendered element?");
		list.add("What   is the width and height of the rendered element?");
		list.add(" Get   the tag name of this element.");
		list.add(
				"Is   this element displayed or not? This method avoids the problem of having to   parse an element's \"style\" attribute.");
		list.add(
				"Is the element currently enabled or not? This will generally return true for everything but disabled input elements.");
		list.add("Determine   whether or not this element is selected or not.");
		list.add("Use   this method to simulate typing into an element, which may set its value.");
		list.add(
				"If   this current element is a form, or an element within a form, then this will   be submitted to the remote server.");

		for (int i = 0; i < element_all.size(); i++) {
			list_element.add(element_all.get(i).getText());
			System.out.println(list.get(i));
			System.out.println(list_element.get(i));
		}

		System.out.println(list_element.equals(list));
		Assert.assertEquals(list_element, list);

	}

	@After
	public void tearDown() throws Exception {

		driver.quit();

	}

}

4、总结

采用最笨的方法实现,学习知识点:WebDriver.findElements(By.xpath)、List<String>、Assert.assertEquals。


--------------------------

若有不同的方法思路,欢迎留言交流


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值