第十篇: Java-Selenium之处理动态Web表

程序-练习Web表

  1. 打开浏览器
  2. 打开网址: http://toolsqa.com/automation-practice-table/
  3. 获取单元格"Dubai"值并打印
  4. 点击第一行最后一列的"details"链接
  5. 用动态法获取单元格"Dubai"值
  6. 打印出"Clock Tower Hotel"行所有的值
  7. 关闭浏览器
package automationFramework;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class DynamicWebTables {
	public static void main(String[] args) throws InterruptedException {
		System.setProperty("webdriver.gecko.driver", "F:\\\\workspace\\ToolsQA\\OnlineStore\\geckodriver-v0.26.0-win64\\geckodriver.exe");
		WebDriver driver = new FirefoxDriver();
		driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
		//打开网址: http://toolsqa.com/automation-practice-table/
		String bdURL = "http://toolsqa.com/automation-practice-table";
		driver.navigate().to(bdURL);
		
		//获取单元格"Dubai"值并打印
		String sCellValue = driver.findElement(By.xpath(".//*[@id='content']/table/tbody/tr[1]/td[2]")).getText();
		System.out.println(sCellValue);
		
		//点击第一行最后一列的"details"链接
		driver.findElement(By.xpath(".//*[@id='content']/table/tbody/tr[1]/td[6]/a")).click();
		Thread.sleep(5000);
		
		String sRowValue = "Clock Tower Hotel";
		//用动态法获取单元格"Dubai"值
		String sRow = "1";
		String sCol = "2";
		String Dy_sCellValue=driver.findElement(By.xpath(".//*[@id='content']/table/tbody/tr["+sRow+"]/td["+sCol+"]")).getText();
		System.out.println(Dy_sCellValue);
		
		//打印出"Clock Tower Hotel"行所有的值
		for(int i=1;i<=5;i++) {
			String sValue=null;
			sValue=driver.findElement(By.xpath(".//*[@id='content']/table/tbody/tr[" + i + "]/th")).getText();
			if(sValue.equalsIgnoreCase(sRowValue)) {
				for(int j=1;j<=5;j++) {
					System.out.println(driver.findElement(By.xpath(".//*[@id='content']/table/tbody/tr[" +i+"]/td["+j+"]")).getText());
				}
				break;
			}
		}
		driver.quit();
	}
}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值