如何在Java Selenium中刮取选定的表列并将其写入CVS
我的目标是使用JavaSelenium来刮取数据。我能够加载selenium驱动程序,连接到网站并获取第一列,然后转到下一个分页按钮,直到其变为禁用并将其写入控制台。以下是我迄今为止所做的:如何在Java Selenium中刮取选定的表列并将其写入CVS,java,selenium,arraylist,io,export-to-csv,Java,Selenium,Arraylist,Io,Export To Csv,我的目标是使用JavaSelenium来刮取数据。我能够加载selenium驱动程序,连接到网站并获取第一列,然后转到下一个分页按钮,直到其变为禁用并将其写入控制台。以下是我迄今为止所做的: public static WebDriver driver; public static void main(String[] args) throws Exception { System.setProperty("webdriver.chrome.driver", &qu
public static WebDriver driver;
public static void main(String[] args) throws Exception {
System.setProperty("webdriver.chrome.driver", "E:\\eclipse-workspace\\package-name\\src\\working\\selenium\\driver\\chromedriver.exe");
System.setProperty("webdriver.chrome.silentOutput", "true");
driver = new ChromeDriver();
driver.get("https://datatables.net/examples/basic_init/zero_configuration.html");
driver.manage().window().maximize();
compareDispalyedRowCountToActualRowCount();
}
public static void compareDispalyedRowCountToActualRowCount() throws Exception {
try {
Thread.sleep(5000);
List<WebElement> namesElements = driver.findElements(By.cssSelector("#example>tbody>tr>td:nth-child(1)"));
System.out.println("size of names elements : " + namesElements.size());
List<String> names = new ArrayList<String>();
//Adding column1 elements to the list
for (WebElement nameEle : namesElements) {
names.add(nameEle.getText());
}
//Displaying the list elements on console
for (WebElement s : namesElements) {
System.out.println(s.getText());
}
//locating next button
String nextButtonClass = driver.findElement(By.id("example_next")).getAttribute("class");
//traversing through the table until the last button and adding names to the list defined about
while (!nextButtonClass.contains("disabled")) {
driver.findElement(By.id("example_next")).click();
Thread.sleep(1000);
namesElements = driver.findElements(By.cssSelector("#example>tbody>tr>td:nth-child(1)"));
for (WebElement nameEle : namesElements) {
names.add(nameEle.getText());
}
nextButtonClass = driver.findElement(By.id("example_next")).getAttribute("class");
}
//printing the whole list elements
for (String name : names) {
System.out.println(name);
}
//counting the size of the list
int actualCount = names.size();
System.out.println("Total number of names :" + actualCount);
//locating displayed count
String displayedCountString = driver.findElement(By.id("example_info")).getText().split(" ")[5];
int displayedCount = Integer.parseInt(displayedCountString);
System.out.println("Total Number of Displayed Names count:" + displayedCount);
Thread.sleep(1000);
// Actual count calculated Vs Dispalyed Count
if (actualCount == displayedCount) {
System.out.println("Actual row count = Displayed row Count");
} else {
System.out.println("Actual row count != Displayed row Count");
throw new Exception("Actual row count != Displayed row Count");
}
} catch (Exception e) {
e.printStackTrace();
}
}
公共静态WebDriver;
公共静态void main(字符串[]args)引发异常{
System.setProperty(“webdriver.chrome.driver”,“E:\\eclipse workspace\\package name\\src\\working\\selenium\\driver\\chromedriver.exe”);
System.setProperty(“webdriver.chrome.silentOutput”,“true”);
驱动程序=新的ChromeDriver();
驱动程序。获取(“https://datatables.net/examples/basic_init/zero_configuration.html");
driver.manage().window().maximize();
将SpalyedRowCount与实际RowCount()进行比较;
}
public static void comparedSpayedRowCountToActualRowCount()引发异常{
试一试{
睡眠(5000);
List namesElements=driver.findElements(By.cssSelector(#example>tbody>tr>td:nth child(1)”);
System.out.println(“名称元素的大小:+namesElements.size());
列表名称=新的ArrayList();
//将column1元素添加到列表中
for(WebElement-namele:namesElements){
add(namele.getText());
}
//在控制台上显示列表元素
for(WebElement s:namesElements){
System.out.println(s.getText());
}
//查找下一步按钮
字符串nextButtonClass=driver.findElement(By.id(“example_next”)).getAttribute(“class”);
//遍历表直到最后一个按钮,并将名称添加到关于
而(!nextButtonClass.contains(“disabled”)){
driver.findElement(By.id(“example_next”))。单击();
睡眠(1000);
namesElements=driver.findElements(By.cssSelector(#example>tbody>tr>td:nth child(1)”);
for(WebElement-namele:namesElements){
add(namele.getText());
}
nextButtonClass=driver.findElement(By.id(“example_next”)).getAttribute(“class”);
}
//打印整个列表元素
for(字符串名称:名称){
System.out.println(名称);
}
//计算列表的大小
int actualCount=names.size();
System.out.println(“名称总数:+实际数量”);
//查找显示的计数
String displayedCountString=driver.findElement(By.id(“示例信息”)).getText().split(“”[5];
int displayedCount=Integer.parseInt(displayedCountString);
System.out.println(“显示名称总数计数:“+displayedCount”);
睡眠(1000);
//计算的实际计数与显示的计数
如果(实际计数==显示计数){
System.out.println(“实际行数=显示行数”);
}否则{
System.out.println(“实际行数!=显示行数”);
抛出新异常(“实际行数!=显示的行数”);
}
}捕获(例外e){
e、 printStackTrace();
}
}
我想:
我这样试过,但没有跑:
for(WebElement trElement : tr_collection){
int col_num=1;
List<WebElement> td_collection = trElement.findElements(
By.xpath("//*[@id=\"example\"]/tbody/tr[rown_num]/td[col_num]")
);
for(WebElement tdElement : td_collection){
rows += tdElement.getText()+"\t";
col_num++;
}
rows = rows + "\n";
row_num++;
}
for(WebElement-treelement:tr_集合){
int col_num=1;
列表td_collection=treelement.findElements(
By.xpath(“//*[@id=\'example\”]/tbody/tr[rown\u num]/td[col\u num]”)
);
for(WebElement-tdElement:td_集合){
行+=tdElement.getText()+“\t”;
col_num++;
}
行=行+“\n”;
行数++;
}
刮削:
通常,当我想要收集列表元素时,我将通过Xpath而不是CssSelector来选择。如何通过Xpath访问元素的结构通常更清晰,并且取决于指定元素的一个或两个整数值
因此,对于要查找名称的示例,可以通过Xpath查找元素,即列表Xpath中的下一个元素,并找到不同的值:
名字“Airi Satou”位于以下Xpath中:
/*[@id=“example”]/tbody/tr[1]/td[1]
Airi的立场如下:
/*[@id=“example”]/tbody/tr[1]/td[2]
您可以看到,跨行的“td”标记上的每一条信息的Xpath都不同
列表中的下一个名字是“Angela Ramos”:
/*[@id=“example”]/tbody/tr[2]/td[1]
安吉拉的位置如下:
/*[@id=“example”]/tbody/tr[2]/td[2]
您可以看到,列中的差异由“tr”标记控制
通过迭代'tr'和'td'的值,可以得到整个表
至于写入CSV,有一些可靠的Java库可用于写入CSV。我认为下面是一个简单的例子:
更新:
@User169看起来您正在为表中的每一行收集元素列表。您希望逐个收集XPath,迭代最初找到的WebElement列表。尝试此操作,然后添加到其中,这样它将获得文本并将其保存到数组中
for (int num_row = 1; num_row < total_rows; num_row++){
for (int num_col = 1; num_col < total_col; num_col++){
webElement info = driver.findElement(By.xpath("//*[@id=\"example\"]/tbody/tr[" + row_num + ']/td[' + col_num + "]");
}
}
for(int num_row=1;num_row
我还没有测试过它,所以它可能需要一些小改动。:facepalm:新手错误,我是新手。不用担心,只要有时间读一读降价书——它在很多产品中都有使用(GitLab、GitHub、Jira等)。@TomM请查看我问题的更新部分,并更正我的问题code@TomM您提到了aske的测试用例