Selenium Webdriver Table Positioning method Practice

Source: Internet
Author: User
Tags gettext tagname xpath

Selenium Webdriver table Positioning method HTML Data preparation
<HTML><Body><DivID= "Div1"><inputname= "Divl1input"></input><ahref= "http://www.sogou.com/">Sogou Search</a><imgalt= "DIV1-IMG1"src= "Http://www.sogou.com/images/logo/new/sogou.png"href= "Http://www.sogou.com">Sogou Pictures</img><inputtype= "button"value= "Query"></input></Div><BR></BR><Divname= "Div2"><inputname= "Div2input"></input><ahref= "Http://www.baidu.com">Baidu Search</a><imgalt= "Div2-img2"src= "Http://www.baidu.com/img/bdlogo.png"href= "Http://www.baidu.com">Baidu Pictures</img><inputtype= "button"value= "Query"></input></Div><BR></BR><BR></BR><BR></BR><BR></BR><ahref= "Http://www.sogou.com">Sogou Search</a><BR></BR><ahref= "Http://www.baidu.com">Baidu Search</a><BR></BR><BR></BR><BR></BR><BR></BR><Tablewidth= "The "Border= "1"ID= "Table"><TR><TDAlign= "Left" >Consumer projects ....</TD><TDAlign= "Right">One month</TD><TDAlign= "Right">Month</TD></TR><TR><TDAlign= "Left" >Clothes</TD><TDAlign= "Right">1000 USD</TD><TDAlign= "Right">2000 USD</TD></TR><TR><TDAlign= "Left" >Cosmetics</TD><TDAlign= "Right">10 USD</TD><TDAlign= "Right">20 USD</TD></TR><TR><TDAlign= "Left" >Snacks</TD><TDAlign= "Right">120 USD</TD><TDAlign= "Right">100 USD</TD></TR><TR><TDAlign= "Left" >Total</TD><TDAlign= "Right">88880 USD</TD><TDAlign= "Right">10000 USD</TD></TR></Table></Body></HTML>

Java code:
 Packagepage_elements_positioning;Importjava.util.List;Importorg.openqa.selenium.By;ImportOrg.openqa.selenium.WebDriver;Importorg.openqa.selenium.WebElement;ImportOrg.openqa.selenium.firefox.FirefoxDriver; Public classtable_positiing { Public Static voidMain (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=NewFirefoxdriver ();                    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 ("-----------------------------------------------------------------------"); //Note "//*[@id = ' table ']/tbody/td[1]/tr[1] "----/tbody must be added.webelement element =driver.findelement (By.xpath ("//*[@id = ' table ']/tbody/tr[2]/td[3]"));//---Remove the * number! 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 ()); }}

Test results:

Consumer projects .... One month and one month
Clothes 1000 yuan 2000 yuan
Cosmetics 10 Yuan 20 yuan
Snacks 120 Yuan 100 yuan
Total $88880 $10000
-----------------------------------------------------------------------
*[@id = ' table ']/tbody/tr[2]/td[3]-----$ 2000
[@id = ' table ']/tbody/tr[3]/td[3]-------$ 20

Selenium Webdriver Table Positioning method Practice

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.