html table是由 table 元素以及一个或多个 tr、th 或 td 元素组成。如下:
HTML源码如下:
for selenium test table head:
Test Case ID Steps Expect Actual PASS/FAIL ENT#-12345 1.open baidu.com ,wait for the page load2.enter "selenium" in the input box" 3.click search button "Selenium - Web Browser Automation" link be the first of the search result Selenium - Web Browser Automation is appear the page,but is not the first link FAIL ENT#-12346 1.click the "Selenium - Web Browser Automation" link2.wait for page load open the official home page of selenium selenium home page is load FAIl ENT#-12347 1.click baidu snapshot of selenium web page 2. wait for the page load the snapshot web page can be show up the snapshot web page is show up PASS
获取table的base xpath,base xpath是指这个table的第n行第m列相同的部分,然后通过传入n,m获取返回值
Java代码:
public static String tableCell(WebDriver driver,int row, int column) { String text = null; //avoid get the head line of the table // row=row+1; String xpath="//*[@id='table138']/tbody/tr["+row+"]/td["+column+"]"; try{ WebElement table=driver.findElement(By.xpath(xpath)); //*[@id="table138"]/tbody/tr[1]/td[1]/strong text=table.getText(); }catch(NoSuchElementException e){ System.out.println("超出table边界值"); } return text; }
也可以通过 tr,td来写
public static String tableCell2(WebDriver driver,int row, int column) { String text = null; //avoid get the head line of the table //row=row+1; try{ ListrowCounts = driver.findElements(By.tagName("tr")); WebElement currentRow = rowCounts.get(row-1); List td = currentRow.findElements(By.tagName("td")); WebElement cell = td.get(column-1); text=cell.getText(); }catch(IndexOutOfBoundsException e){ System.out.println("超出table边界值"); } return text; }
Python代码,这里要将row/column转为str
def tablecell(driver,row ,column): row = row + 1 xpath = "//*[@id='table138']/tbody/tr["+str(row)+"]/td["+str(column)+"]"; table = driver.find_element_by_xpath(xpath) return table.text
示例:
JAVA代码:
import org.openqa.selenium.By;import org.openqa.selenium.NoSuchElementException;import org.openqa.selenium.WebDriver;import org.openqa.selenium.WebElement;import org.openqa.selenium.firefox.FirefoxDriver;public class getTableValue { public static void main(String[] args) { // TODO Auto-generated method stub WebDriver driver = new FirefoxDriver(); driver.manage().window().maximize(); driver.get("F:\\table.html"); System.out.println(tableCell(driver, 1, 2)); driver.quit(); } public static String tableCell(WebDriver driver,int row, int column) { String text = null; //avoid get the head line of the table row=row+1; String xpath="//*[@id='table138']/tbody/tr["+row+"]/td["+column+"]"; try{ WebElement table=driver.findElement(By.xpath(xpath)); //*[@id="table138"]/tbody/tr[1]/td[1]/strong text=table.getText(); }catch(NoSuchElementException e){ System.out.println("超出table边界值"); } return text; }}
输出为:
Python代码
def tablecell(driver,row ,column): row = row + 1 xpath = "//*[@id='table138']/tbody/tr["+str(row)+"]/td["+str(column)+"]"; table = driver.find_element_by_xpath(xpath) return table.textfrom selenium import webdriverdriver = webdriver.Firefox()driver.maximize_window()driver.get("F:\\table.html")print(tablecell(driver, 1, 2))driver.quit()
输出为: