Selenium Webdriver——处理Table
html table是由 table 元素以及一个或多个 tr、th 或 td 元素组成。如下:
HTML源码如下:
<html> <head> <meta http-equiv="Content-Language" content="zh-cn"> <meta http-equiv="Content-Type" content="text/html; charset=gb2312"> <title>for selenium test </title> </script> </head> <body> <div align="center"> <h4 align="left"> table head:</h4> <table border="2" width="90%" id="table138" bordercolorlight="#CCCCCC" cellspacing="0" cellpadding="0" bordercolordark="#CCCCCC" style="border-collapse: collapse"> <tr align="center"> <td height="26" width="10%" align="center" bgcolor="#CCEEAA" ><Strong>Test Case ID</Strong></td> <td height="26" width="35%" align="center" bgcolor="#EEEEAA" ><Strong>Steps</Strong></td> <td height="26" width="30%" align="center" bgcolor="#00EEEE" ><Strong>Expect</Strong></td> <td height="26" align="center" bgcolor="#EE00EE" ><Strong>Actual</Strong></td> <td height="26" align="center" bgcolor="#00EE00" ><Strong>PASS/FAIL</Strong></td> </tr> <tr align="center"> <td height="26" align="center" bgcolor="#CCEEAA">ENT#-12345</td> <td height="26" align="left" bgcolor="#EEEEAA" >1.open baidu.com ,wait for the page load</br>2.enter "selenium" in the input box" </br>3.click search button </td> <td height="26" align="left" bgcolor="#00EEEE" >"Selenium - Web Browser Automation" link be the first of the search result</td> <td height="26" align="left" bgcolor="#EE00EE" >Selenium - Web Browser Automation is appear the page,but is not the first link</td> <td height="26" align="center" bgcolor="#00EE00" >FAIL</td> </tr> </tr> <tr align="center"> <td height="26" align="center" bgcolor="#CCEEAA">ENT#-12346</td> <td height="26" align="left" bgcolor="#EEEEAA" >1.click the "Selenium - Web Browser Automation" link</br>2.wait for page load</td> <td height="26" align="left" bgcolor="#00EEEE" >open the official home page of selenium</td> <td height="26" align="left" bgcolor="#EE00EE" >selenium home page is load </td> <td height="26" align="center" bgcolor="#00EE00" >FAIl</td> </tr> </tr> <tr align="center"> <td height="26" align="center" bgcolor="#CCEEAA">ENT#-12347</td> <td height="26" align="left" bgcolor="#EEEEAA" >1.click baidu snapshot of selenium web page </br>2. wait for the page load</td> <td height="26" align="left" bgcolor="#00EEEE" >the snapshot web page can be show up</td> <td height="26" align="left" bgcolor="#EE00EE" >the snapshot web page is show up</td> <td height="26" align="center" bgcolor="#00EE00" >PASS</td> </tr> </table> </div> </body> </html>
获取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{ List<WebElement> rowCounts = driver.findElements(By.tagName("tr")); WebElement currentRow = rowCounts.get(row-1); List<WebElement> 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.text from selenium import webdriver driver = webdriver.Firefox() driver.maximize_window() driver.get("F:\\table.html") print(tablecell(driver, 1, 2)) driver.quit()
输出为:
分类:
Selenium
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!