Selenium Webdriver——處理Table

來源:互聯網
上載者:User

標籤:

 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+"]";        WebElement table=driver.findElement(By.xpath(xpath)); //*[@id="table138"]/tbody/tr[1]/td[1]/strong        text=table.getText();        return text;     }

 

樣本:

JAVA代碼:

 

Python代碼

Selenium Webdriver——處理Table

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.