Handling Web Tables, Frames, and Dynamic Elements in Selenium

Handling Web Tables, Frames, and Dynamic Elements in Selenium 



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

#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 group of elements that are logically stored in a row and column format. It is used to organize similar information in a web page.
Below is an example of Html table:
Handling web tables in selenium
Below tags are generally defined in an html tables:
1.’table’ tag defines html table.
2.’tbody’ tag defines container for rows and columns.
3.’tr’ defines rows in an html table.
4.’td’/’th’ define 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 number of rows and columns are always constant, let’s say our html table will always have 5 rows and 3 columns.
1for(int numberOfRows=1; numberOfRows<=5; numberOfRows++)
2{
3for(int numberOfCol=1; numberOfCol <=3; numberOfCol++)
4{
5System.out.println(driver.findElement(By.xpath(“//div[@id='main']/table[1]/tbody/tr[“+numberOfRows+”]/th[“+numberOfCol+”]”)));
6}
7}

Except 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:
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:
1WebElement htmltable=driver.findElement(By.xpath("//*[@id='main']/table[1]/tbody"));
2List<WebElement> rows=htmltable.findElements(By.tagName("tr"));
3
4for(int rnum=0;rnum<rows.size();rnum++)
5{
6List<WebElement> columns=rows.get(rnum).findElements(By.tagName("th"));
7System.out.println("Number of columns:"+columns.size());
8
9for(int cnum=0;cnum<columns.size();cnum++)
10{
11System.out.println(columns.get(cnum).getText());
12}
13}
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 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 frame. 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 tag name as “iframe”.
List<WebElement> frameList=driver.findElements(By.tagName(“iframe”));
System.out.println(frameList.size());
In above exampleframeList will have all the list of frames andframeList.size() will give the number of frames.
Handling an element inside 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 1If 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 2If 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.

Handling Dynamic Elements in Selenium WebDriver


                      Handling Dynamic Elements in Selenium WebDriver

Dynamic elements are those elements which have identifiers that are dynamically generated. Dynamic identifiers are normally used for buttons, text-fields and buttons. Let us take an example of a button whose ID is in following format…
In this test scenario, we can observe that element ID is not static. There is a number combined with text that auto increments on user action. So, we can expect that on every script execution there will be a new ID for the element.
There are multiple approaches which can be used to handle dynamic elements but there is no definite one. An approach might work in one scenario and might not work in another. It all depends on the code, element type, locator and test script requirements.
1. Absolute XPath
Xpath Position or Absolute Xpath are most frequently used to resolve the dynamic element issues. Only problem with using XPath locators is that they are very fragile. They are most prone to breakage in case of change in web page. This factor could get worse exponentially as the test suite size and complexity increases. Below is an example of Absolute XPath and XPath Position
2. Identify Element by starting Text
If the dynamic elements have a definite pattern to them, then we can also use JavaScript functions like “starts-with” or “contains” in our element locators to separate the dynamic part of locator from static part.
For example, in case of dynamic submit button Id example which we discussed earlier, we can apply ‘starts-with’ function to access this locator irrespective of its dynamic part.
3. Identify Element by containing Text
Similarly, in some scenarios where dynamic element is surrounded by a static value, we can use ‘contains’ function. For example we have following element locators…
As we can see ‘usefield’ part of element is static, so we can apply ‘contains’ function to access this element locator as shown below…
4. Identify Element by Index
If there are multiple elements present on page with same locator then we can use following Java code in our selenium WebDriver script to interact with element of particular index.
5. Identify Element with reference of a closest stable element
We can use the DOM structure to find the closest stable element first and then this stable element can be used as a reference element to find the required element.
DOM structure could be found using Firefox extension like Firebug and FirePath. But in complex and large applications this approach is difficult to use because of large DOM structure.
6. Identify Element by stable preceding Text
For web elements like text field and text areas we can identify them by using the stable text labels nearby. This approach might not be possible in all scenarios but it does resolve the dynamic element issues where possible. Example of this approach is shown below.

Handling Dynamic Elements in Selenium WebDriver

                       Handling Dynamic Elements in Selenium WebDriver Dynamic elements are those elements which have identifiers that a...