Posts

Showing posts from February, 2017

Selenium WebDriver - Handle Dynamic Web Table

In this selenium webdriver post, we will learn how to handle dynamic web table. There might occur some scenarios where every time a page loads with different number of rows and columns or different columns for each row.

For that, we have to put two for loops,
1. first loop will iterate all the rows and
2. second loop inside it will iterate the columns of each row


publicclassDynamicTable{ WebDriver driver =new FirefoxDriver();@BeforeTestpublicvoidsetup()throws Exception { driver.manage().window().maximize(); driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS); driver.get("https://www.techlistic.com/p/sample-table.html");}@AfterTestpublicvoidtearDown()throws Exception { driver.quit();}@Testpublicvoidtest_Dynamic_Webtable(){// Locate table WebElement mytable = driver.findElement(By.xpath(".//*[@id='post']/div[1]/table/tbody"));// Locate rows of table and save locators of each row in a list List rows_table = mytable.findElements(By.…

Selenium WebDriver - Extract/Read Table Data

Following Script will extract data from table:


publicclasstable{ WebDriver driver =new FirefoxDriver();@BeforeTestpublicvoidsetup()throws Exception { driver.manage().window().maximize(); driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS); driver.get("https://www.techlistic.com/p/sample-table.html");}@AfterTestpublicvoidtearDown()throws Exception { driver.quit();}@Testpublicvoidprint_data(){//Get number of rows In table.int Row_count = driver.findElements(By.xpath("//*[@id='post']/div[1]/table/tbody/tr")).size(); System.out.println("Number Of Rows = "+Row_count);//Get number of columns In table.int Col_count = driver.findElements(By.xpath("//*[@id='post']/div[1]/table/tbody/tr[1]/td")).size(); System.out.println("Number Of Columns = "+Col_count);//divided xpath In three parts to pass Row_count and Col_count values. String first_part ="//*[@id='post']/div[1]/table/tbody/tr["; S…