webdriver程序的输出格式不正确(表格格式) import java.util.List; 导入java.util.concurrent.TimeUnit; 导入org.openqa.selenium.By; 导入org.openqa.seleniu
webdriver程序的输出格式不正确(表格格式)webdriver程序的输出格式不正确(表格格式) import java.util.List; 导入java.util.concurrent.TimeUnit; 导入org.openqa.selenium.By; 导入org.openqa.seleniu,java,selenium-webdriver,Java,Selenium Webdriver,webdriver程序的输出格式不正确(表格格式) import java.util.List; 导入java.util.concurrent.TimeUnit; 导入org.openqa.selenium.By; 导入org.openqa.selenium.WebDriver; 导入org.openqa.selenium.WebElement; 导入org.openqa.selenium.firefox.FirefoxDriver; 公共类WebTable_动态{ 公共静态void main(
import java.util.List;
导入java.util.concurrent.TimeUnit;
导入org.openqa.selenium.By;
导入org.openqa.selenium.WebDriver;
导入org.openqa.selenium.WebElement;
导入org.openqa.selenium.firefox.FirefoxDriver;
公共类WebTable_动态{
公共静态void main(字符串[]args){
WebDriver=newfirefoxdriver();
driver.manage().timeouts().implicitlyWait(20,TimeUnit.SECONDS);
驱动程序。获取(“http://www.w3schools.com/tags/tag_table.asp");
WebElement=driver.findElement(By.xpath(//table[@class='reference nottranslate']);
列表行=element.findElements(按.tagName(“tr”));
对于(int rowNum=1;rownumr)当您从getText()
获得单元格值后,用空字符串/空格替换
文本。如果您想在输出中保留换行符,您将需要更多的格式。感谢您的回复……我尝试使用以下方法:string cellValue=cells.get(colNum).getText().replaceAll(
,“”);System.out.print(cellValue);……但是没有结果:(从getText()
输出的是什么?它是否有任何换行符,例如\r
或\n
?
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class WebTable_Dynamic {
public static void main(String[] args) {
WebDriver driver= new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.get("http://www.w3schools.com/tags/tag_table.asp");
WebElement element=driver.findElement(By.xpath("//table[@class='reference notranslate']"));
List<WebElement> rows=element.findElements(By.tagName("tr"));
for(int rowNum=1;rowNum<rows.size();rowNum++){
List<WebElement> cells=rows.get(rowNum).findElements(By.tagName("td"));
for(int colNum=0;colNum<cells.size();colNum++){
System.out.print(cells.get(colNum).getText());
System.out.print("----");
}
System.out.println();
}
driver.close();
}
}