selenium webdriver 表格的定位方法練習

來源:互聯網
上載者:User

標籤:搜狗搜尋   化妝品   name   pre   img   pos   url   java代碼   user   

 selenium webdriver 表格的定位方法html 資料準備
<html><body><div id="div1"><input name="divl1input"></input><a href="http://www.sogou.com/">搜狗搜尋</a><img alt="div1-img1 "src="http://www.sogou.com/images/logo/new/sogou.png"href = "http://www.sogou.com">搜狗圖片</img><input type ="button" value="查詢"></input></div><br></br><div name="div2"><input name="div2input"></input><a  href = "http://www.baidu.com">百度搜尋</a><img alt="div2-img2" src="http://www.baidu.com/img/bdlogo.png" href= "http://www.baidu.com">百度圖片</img><input type="button" value="查詢"></input></div><br></br><br></br><br></br><br></br><a href="http://www.sogou.com"> 搜狗搜尋</a><br></br><a href="http://www.baidu.com"> 百度搜尋</a><br></br><br></br><br></br><br></br><table width="400" border="1" id= "table"><tr><td align="left" >消費項目。。。。</td><td align="right"> one  month </td><td align="right">two  month</td></tr><tr><td align="left" >衣服</td><td align="right"> 1000 元 </td><td align="right">2000 元</td></tr><tr><td align="left" >化妝品</td><td align="right"> 10 元 </td><td align="right">20 元</td></tr><tr><td align="left" >零食</td><td align="right"> 120 元   </td><td align="right">100 元  </td></tr><tr><td align="left" >總計</td><td align="right">88880 元 </td><td align="right">10000  元</td></tr></table></body></html>

java代碼 :
package page_elements_positioning;import java.util.List;import org.openqa.selenium.By;import org.openqa.selenium.WebDriver;import org.openqa.selenium.WebElement;import org.openqa.selenium.firefox.FirefoxDriver;public class Table_Positiing {        public static void main(String[] args) {        String url ="file:///C:/Users/ty/Desktop/selenium_test.html";        System.setProperty("webdriver.firefox.bin", "D:\\softerWare\\firefox\\firefox\\firefox.exe");        WebDriver driver ;        driver = new FirefoxDriver();        driver.get(url);                    WebElement table =driver.findElement(By.id("table")); List<WebElement> rows =  table.findElements(By.tagName("tr")); for (WebElement row  : rows ){          List<WebElement> cols =row.findElements(By.tagName("td"));     for(WebElement col :cols ){         System.out.print(col.getText()+"\t");     }     System.out.println(""); }  System.out.println("-----------------------------------------------------------------------");  //注意" //*[@id=‘table‘]/tbody/td[1]/tr[1]"---- /tbody 是必須要加的    WebElement element =driver.findElement(By.xpath("//*[@id=‘table‘]/tbody/tr[2]/td[3]"));//---去掉 *  號 !        WebElement element1 =driver.findElement(By.xpath("//*[@id=‘table‘]/tbody/tr[3]/td[3]"));    System.out.println("//*[@id=‘table‘]/tbody/tr[2]/td[3]----"+element.getText());    System.out.println("//[@id=‘table‘]/tbody/tr[3]/td[3]---"+element1.getText());        }}

 

測試結果:

消費項目。。。。 one month two month 
衣服 1000 元 2000 元 
化妝品 10 元 20 元 
零食 120 元 100 元 
總計 88880 元 10000 元 
-----------------------------------------------------------------------
//*[@id=‘table‘]/tbody/tr[2]/td[3] -----2000 元
//[@id=‘table‘]/tbody/tr[3]/td[3]-------20 元

 

selenium webdriver 表格的定位方法練習

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.