Skip to content

Iptv Assist

Learn More from us

Menu
  • HOW TO
  • Firestick
  • Chromecast
  • PC Apps
  • Lg Smart TV
  • IPTV Services
  • Automation Testing
  • Smart TV
  • Software Testing Tools
  • Contact Us
Menu

Handling Web Tables, Frames, and Dynamic Elements in Selenium Script – Selenium Tutorial #18

Posted on March 26, 2023

Best Iptv Service Provider 2023 With 40k+ Channels And 100k+ VOD . 24/7 Suppport . Paypal Supported

In last Selenium WebDriver tutorial, we learned various commonly and routinely used Selenium WebDriver commands including important topics like handling iframe and exceptions in Selenium scripts.

Best Iptv Service Provider 2023 With 40k+ Channels And 150k+ VOD . Hurry Up

Moving ahead in our comprehensive series of tutorials on Selenium, in this tutorial we would discuss about handling Web tables, iframe and dynamic elements which are an essential part of any web project.

Recommended IPTV Service Providers

  1. IPTVGREAT – Rating 4.8/5 ( 600+ Reviews )
  2. IPTVRESALE – Rating 5/5 ( 200+ Reviews )
  3. IPTVGANG – Rating 4.7/5 ( 1200+ Reviews )
  4. IPTVUNLOCK – Rating 5/5 ( 65 Reviews )
  5. IPTVFOLLOW -Rating 5/5 ( 48 Reviews )
  6. IPTVTOPS – Rating 5/5 ( 43 Reviews )

 

This tutorial consists of 3 different topics and their handling mechanisms in selenium script.

Handling Web Tables, Frames, and Dynamic Elements in Selenium Script

  1. Web Tables/HTML tables
  2. Frames
  3. Dynamic elements

What You Will Learn:

  • #1) Web Tables/HTML Tables
  • #2) Frames
  • #3) Dynamic Elements
  • Conclusion

#1) Web Tables/HTML Tables

In this module, we will learn about the web tables or HTML tables in a web page, tags available in HTML and how to handle web tables dynamically.

Web tables are basically a group of elements that are logically stored in a row and column format. It is used to organize similar information on a web page.

Below is an example of Html table:

Handling web tables in selenium

Below is the snippet of HTML structure of an HTML table:

Handling web tables in selenium 1

Below tags are generally defined in html tables:

1.’table’ tag defines HTML table.
2.’tbody’ tag defines a container for rows and columns.
3.’tr’ defines rows in an HTML table.
4.’td’/’th’ define the column of an HTML table.

Find the details of a web table:

There are many ways we can handle a web table.

Approach #1:

Below is the xpath of one of the cell in html table. Let’s say “firstname”

//div[@id=’main’]/table[1]/tbody/tr[1]/th[1]

tr[1] defines first row and th[1] defines first column.

If a number of rows and columns are always constant, let’s say our HTML table will always have 5 rows and 3 columns.

for(int numberOfRows=1; numberOfRows<=5; numberOfRows++)
{
for(int numberOfCol=1; numberOfCol <=3; numberOfCol++)
{
System.out.println(driver.findElement(By.xpath
(“//div[@id='main']/table[1]/tbody/tr
[“+numberOfRows+”]/th[“+numberOfCol+”]”)));
}
} 

 

Except for row and column number, each component of XPath remains the same. So you can iterate using “for loop” for each row and column as mentioned above.

Approach #2:

The first approach is best suitable for the table which doesn’t change its dimensions and always remains the same. Above approach will not be a perfect solution for dynamically changing web tables.

Let’s take above HTML table as an example:

WebElement htmltable=driver.findElement(By.xpath("//*[@id='main']/table[1]/tbody"));
List<WebElement> rows=htmltable.findElements(By.tagName("tr"));

for(int rnum=0;rnum<rows.size();rnum++)
{
List<WebElement> columns=rows.get(rnum).findElements(By.tagName("th"));
System.out.println("Number of columns:"+columns.size());

for(int cnum=0;cnum<columns.size();cnum++)
{
System.out.println(columns.get(cnum).getText());
}
} 

Step 1: First get the entire HTML table and store this in a variable ‘htmltable’ of type web element.

Step 2: Get all the rows with tag name ‘tr’ and store all the elements in a list of web elements. Now all the elements with tag ‘tr’ are stored in ‘rows’ list.

Step 3: Loop through each row and get the list of elements with tag ‘th’. ‘rows.get(0)’ will give first row and ‘findElements(By.tagName(“th”))’ will give list of columns for the row.

Step 4: Iterate using ‘columns.getsize()’ and get the details of each cell.

Note: Above approach will be best suitable if the table dimensions changes dynamically.

This concludes the topic how to handle web tables in selenium. Next, we will learn about handling an element inside a frame.

#2) Frames

In this section, we will learn about the frames in a web page and how to identify the frames. Also, we will find out how we can handle a frame in Selenium WebDriver.

Many developers like to place elements inside a frame. The frame is just like a container where few elements can be grouped.

Identification of a frame:

Different ways to know if the element is present inside a frame or not

#1. Right-click on the element. Check if “This Frame” option is available. If This frame option is available, it means that the element is inside a frame.

#2. View page source of the web page and check if any tag is available for ‘iframe’.

Handling iframes in selenium

Verify Number of frames in a webpage:

All the frames are having the tag name as “iframe”.

List<WebElement> frameList=driver.findElements(By.tagName(“iframe”));
System.out.println(frameList.size());

In above example: frameList will have all the list of frames and frameList.size() will give the number of frames.

Handling an element inside the frame:

If an element is inside a frame then control has to switch to frame first and then start operating on the elements.

Step 1: To switch inside a frame:

driver.switchTo().frame(1); //pass frame number as parameter.
or
driver.switchTo().frame(“frame Name”); //pass frame name as parameter.
or
driver.switchTo().frame(“xpath of the frame”);

Step 2: After switching inside a frame selenium will be able to operate on elements.

driver.findElement(//*[@id=’username’]).sendKeys(“username”);
driver.findElement(//*[@id=’pass’]).sendKeys(“password”);

Here, we have learned how to handle an element inside frame and next we will cover about the different ways to handle dynamic element.

#3) Dynamic Elements

In this section we will learn different ways to handle dynamic element and construct generic Xpath.

In few scenarios, element attributes change dynamically. It can be ‘id’, ’name’ etc.

Example: let’s say ‘id’ of a username field is ‘username_123’ and the XPath will be

//*[@id=’username_123′] but when you open the page again the ‘id’ of ‘username’ field might have changed and the new value may be ‘username_234’.

In this case, the test will fail because the selenium could not find the XPath you have passed earlier as the id of the field has changed to some other value.

There are many approaches depending upon the type of problem:

Problem Type 1: If part of the attribute value changes.

Example: As in the above example, id value changes but few fields remains constant.
‘username_123’ changed to ‘username_234’ but ‘username’ always remained constant.

You can construct xpath as below:

driver.findElement(By.xpath(“//*[contains(@id,’username’)]”)).sendKeys(“username”);
driver.findElement(By.xpath(“//*[starts-with(@id,’user’)]”)).sendKeys(“username”);

‘contains’ is a java method which checks if id contains the substring username.
starts-with() checks if any attribute starts with “user”.

Problem Type 2: If entire value of the attribute changes dynamically.

Again, in this case, there could be different approaches:

Handling Dynamic elements in selenium
For example: if id of ‘login’ field changes dynamically and there is no constant value to use contains method.

Solution: Use of sendKeys.
Selenium provides different API to use function keys. For example tab key, enter keys, F5 etc.

Step 1: Enter password
driver.findElement(By.id(“password”)).sendKeys(“password”));

Step 2: Use key functions to navigate to element.
driver.findElement(By.id(“password”)).sendKeys(Keys.ENTER));
or
driver.findElement(By.id(“password”)).sendKeys(Keys.TAB));

Conclusion

Web tables, frames and dynamic elements are essential part of any web project. It is always desirable to write effective code to handle web tables and dynamic elements.

Understanding the construction of generic XPath which is very helpful while handling dynamic elements. In case of a frame, your script has to switch the frame and then operate on the element.

Next tutorial #19: In next Selenium tutorial we will learn about types of exceptions and how to handle exceptions in java in Selenium scripts.

Please post your queries related to Web tables, frames and handling dynamic element if you have any.

Related

Best Iptv Service Provider 2023 With 40k+ Channels And 150k+ VOD . Hurry Up

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent Posts

  • IPTV List: Best iptv lista 2023
  • IPTV Premium: Best Premium IPTV Service Provider List And Benefits
  • Nikon IPTV Review: Over 10,000 Live Channels for $12/Month
  • Iptvwings. Com Review: +18000 Live IPTV Channels ,+70000 Movies, +40000 TV show For $15/1 month
  • IPTVUNO Review: More Than 16000 Live TV channels, 55,000 Movies & VOD For $15/Month

Recent Comments

  1. IPTV List: Play lista iptv 2022 - Iptv Assist on Best IPTV Player in 2023 for Watching Live TV
  2. Cola IPTV – Over 18,000 Live Channels for $12/Month - Iptv Assist on FileLinked – How to Install on Firestick/Fire TV and Android Devices
  3. Cola IPTV – Over 18,000 Live Channels for $12/Month - Iptv Assist on 50+ Best IPTV Service Providers for Streaming Live TV 2023
  4. XoomsTV IPTV – Over 11,000 Channels & VOD for Under $13/Month on Best VPN for IPTV in 2023 and How to Install on Firestick/Android
  5. Voodoo Streams IPTV Review – Over 12,000 Channels for $11/Month - Iptv Assist on Dynasty TV IPTV Review – Over 6,000 Channels for $10/Month

Archives

  • March 2023

Categories

  • Activate
  • Agile Testing
  • Alternatives
  • Android
  • APK
  • Apple TV
  • Automation Testing
  • Basics of Software Testing
  • Best Apps
  • Breakfast Hours
  • Bug Defect tracking
  • Career in Software Testing
  • Chromebook
  • Chromecast
  • Cross Platform
  • Database Testing
  • Delete Account
  • Discord
  • Error Code
  • Firestick
  • Gaming
  • General
  • Google TV
  • Hisense Smart TV
  • HOW TO
  • Interview Questions
  • iPhone
  • IPTV
  • IPTV Apps
  • Iptv Service SP
  • IPTV Services
  • JVC Smart TV
  • Kodi
  • Lg Smart TV
  • Manual Testing
  • MI TV
  • Mobile Testing
  • Mod APK
  • newestiptv.com
  • News
  • Nintendo Switch
  • Panasonic Smart TV
  • PC Apps
  • Performance Testing
  • Philips Smart TV
  • PS4
  • PS5
  • Python
  • QA Certifications
  • QA Leadership
  • QA Team Skills
  • Quality Assurance
  • Reddit
  • Reviews
  • Roku
  • Samsung Smart TV
  • Screenshot
  • Selenium Tutorials
  • Sharp Smart TV
  • Skyworth Smart TV
  • Smart TV
  • Soft Skills For Testers
  • Software Testing Templates
  • Software Testing Tools
  • Software Testing Training
  • Sony Smart TV
  • Sports
  • Streaming Apps
  • Streaming Devices
  • Tech News
  • Test Management Tools
  • Test Strategy
  • Testing Best Practices
  • Testing Concepts
  • Testing Methodologies
  • Testing News
  • Testing Skill Improvement
  • Testing Tips and Resources
  • Toshiba Smart TV
  • Tutorials
  • Twitch
  • Types of Testing
  • Uncategorized
  • Vizio Smart TV
  • VPN
  • Web Testing
  • What is
  • Xbox
©2023 Iptv Assist | Design: Newspaperly WordPress Theme