How to access web table element in selenium web driver
There are 2 types of HTML table possible in the web.
- Static web table.
- Dynamic web table.
Static web table: Normally static tables are static in nature, means a number of column and row is fixed, it will never increase in runtime and data inside the each cell is also fixed.
Dynamic web table: It is just opposite of static web table, here row, column and cell data could be not fixed or we can say dynamic. It will change at runtime.
We need to follow few steps to retrieve data from web table
- We need to uniquely identify table object.
- Count number of rows present in the table.
- Count number of column present in each row.
- Get the value from each cell.
Step 1. We need to uniquely identify table object
Now we can start with the below table. We need to identify the web table uniquely.
Here we can see there an id for that highlighted table. The id is “ customers “. We know how to identify the web element using XPath or other selenium locators, you can refer my previous post also.
XPath for this table object
//*[@id="customers"]
Java code
driver.findelement(By.xapth(“//*[@id="customers"]”);
Step 2: Count Number of rows present in the table.
In HTML table row denoted as <tr>. So we need to count the number of <tr> present under the table.
We can directly count the number of rows is available on the table.
XPath for row count
//*[@id='customers']/child::*/child::tr
Above XPath will return 7 element
Java code
int rowCount = driver.findElements(By.xpath("//*[@id='customers']/child::*/child::tr")).size()
Step 3: Count Number of column present in each row.
Now we need to iterate each row and find out how many columns are there for that row.
Xpath for column count for first row (except header)
//*[@id='customers']/child::*/child::tr[1]/child::td
Above XPath will return 3 <td> tag
Java code
int columnCount = driver.findElements(By.xpath//[@id='customers']/child::*/child::tr[1]/child::td")).size()
Step 4: Get the value from each cell.
Java code
driver.findElement(By.xpath("//*[@id='customers']/child::*/child::tr["+row+"]/child::td["+column+"]")); element.getText()
Complete java code to iterate each row and column and print the value
public static void main(String argc[]){
System.setProperty("webdriver.chrome.driver","D:/workspace/TestProject/src/chromedriver.exe");
driver = new ChromeDriver() ;
driver.get("https://www.w3schools.com/html/html_tables.asp");
int rowCount = driver.findElements(By.xpath("//*[@id='customers']/child::*/child::tr")).size();
for (int i=1;i<=rowCount;i++){
int columnCount = driver.findElements(By.xpath("//*[@id='customers']/child::*/child::tr["+i+"]/child::td")).size();
for(int j=1;j<=columnCount;j++){
WebElement element = driver.findElement(By.xpath("//*[@id='customers']/child::*/child::tr["+i+"]/child::td["+j+"]"));
System.out.println(element.getText()+ " ");
}
System.out.println();
}
driver.quit();
}
13 comments
commentsGreat blog.you put Good stuff.All the topics were explained briefly.so quickly understand for me.I am waiting for your next fantastic blog.Thanks for sharing.Any coures related details learn...
Replyselenium training in bangalore|
Thanks for the useful information of software testing courses, give more updates on software testing development, First time I visit your blog really nice, I bookmark your blog here after a daily visit. Learn software testing course in Chennai
ReplyThanks for your informative post. Selenium is the future of IT world to improve the quality of products.
ReplySelenium Training institute in Chennai
Selenium Course in Chennai
Thank you so much for the great and very beneficial stuff that you have shared with the world.
ReplyBecome an Expert In Software Testing Training! The most trusted and trending Programming Language. Learn from experienced Trainers and get the knowledge to crack a coding interview, @Softgen Infotech Located in BTM Layout.
Thanks for sharing such a helpful, and understandable blog. I really enjoyed reading it.
ReplyRobots for kids
Robotic Online Classes
Robotics School Projects
Programming Courses Malaysia
Coding courses
Coding Academy
coding robots for kids
Coding classes for kids
Coding For Kids
youtube abone satın al /n trendyol indirim kodu
Replycami avizesi
cami avizeleri
avize cami
no deposit bonus forex 2021
takipçi satın al
takipçi satın al
takipçi satın al
takipcialdim.com/tiktok-takipci-satin-al/
instagram beğeni satın al
instagram beğeni satın al
btcturk
tiktok izlenme satın al
sms onay
youtube izlenme satın al
no deposit bonus forex 2021
tiktok jeton hilesi
tiktok beğeni satın al
binance
takipçi satın al
uc satın al
sms onay
sms onay
tiktok takipçi satın al
tiktok beğeni satın al
twitter takipçi satın al
trend topic satın al
youtube abone satın al
instagram beğeni satın al
tiktok beğeni satın al
twitter takipçi satın al
trend topic satın al
youtube abone satın al
takipcialdim.com/instagram-begeni-satin-al/
perde modelleri
instagram takipçi satın al
instagram takipçi satın al
takipçi satın al
instagram takipçi satın al
betboo
marsbahis
sultanbet
I'm truly grateful for this article. This is exactly what I've been looking for. Turkey visa for Mexico is a visa for Mexican people. With this visa all the Mexicans visit Turkey .
Replyseo fiyatları
Replysaç ekimi
dedektör
instagram takipçi satın al
ankara evden eve nakliyat
fantezi iç giyim
sosyal medya yönetimi
mobil ödeme bozdurma
kripto para nasıl alınır
bitcoin nasıl alınır
Replytiktok jeton hilesi
youtube abone satın al
gate io güvenilir mi
referans kimliği nedir
tiktok takipçi satın al
bitcoin nasıl alınır
mobil ödeme bozdurma
mobil ödeme bozdurma
mmorpg oyunlar
ReplyInstagram Takipçi Satın Al
Tiktok Jeton Hilesi
Tiktok jeton hilesi
sac ekimi antalya
İnstagram Takipçi Satın Al
İnstagram Takipçi Satin Al
Metin2 pvp serverlar
Takipçi
smm panel
Replysmm panel
iş ilanları
instagram takipçi satın al
hırdavatçı burada
BEYAZESYATEKNİKSERVİSİ.COM.TR
Servis
Jeton Hile İndir
Medespoir Tunis
Replynft nasıl alınır
Replylisans satın al
minecraft premium
özel ambulans
uc satın al
yurtdışı kargo
en son çıkan perde modelleri
en son çıkan perde modelleri