Last active
August 11, 2021 03:27
-
-
Save sarangjaiswal/1e89a7b18105728172c0 to your computer and use it in GitHub Desktop.
Find number of rows & columns in a webtable
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.File; | |
import java.util.List; | |
import java.util.concurrent.TimeUnit; | |
import org.junit.Test; | |
import org.openqa.selenium.By; | |
import org.openqa.selenium.WebElement; | |
import org.openqa.selenium.firefox.FirefoxBinary; | |
import org.openqa.selenium.firefox.FirefoxDriver; | |
import org.openqa.selenium.firefox.FirefoxProfile; | |
import org.openqa.selenium.firefox.internal.ProfilesIni; | |
public class LearningWebTable { | |
@Test | |
public void testWebTable(){ | |
ProfilesIni profile = new ProfilesIni(); | |
FirefoxProfile p = profile.getProfile("default"); | |
FirefoxBinary binary = new FirefoxBinary(new File("C:\\Users\\username\\AppData\\Local\\Mozilla Firefox\\firefox.exe")); | |
FirefoxDriver driver = new FirefoxDriver(binary, p); | |
driver.manage().timeouts().implicitlyWait(10000, TimeUnit.SECONDS); | |
driver.get("https://www.google.com/finance"); | |
driver.findElement(By.xpath(".//*[@id='pHeader1']/td[1]/b/a")).click(); | |
//Row Count in WebTable | |
WebElement table = driver.findElement(By.xpath("//table[@class='gf-table']")); | |
List<WebElement> row = table.findElements(By.tagName("tr")); | |
System.out.println("Total Number of Rows = " + row.size()); | |
//Column Count in WebTable | |
List<WebElement> column = row.get(10).findElements(By.tagName("td")); | |
System.out.println("Total Number of Column = " + column.size()); | |
System.out.println("=================================================================="); | |
//Print content of Rows | |
for (int i=0;i<=row.size()-1 ;i++){ | |
System.out.println(row.get(i).getText()); | |
} | |
System.out.println("=================================================================="); | |
// Print content of 2nd Column | |
List<WebElement> firstcol = driver.findElements(By.xpath("//table[@class='gf-table']/tbody/tr/td[2]")); | |
for (int j=0; j<=row.size();j++){ | |
System.out.println(firstcol.get(j).getText()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
row.get(10).findElements(By.tagName("td"));
why did you take row.get(10)? Is it row.get(1)?