opneScript has some really powerful inbuilt methods for you to use. We will try to cover those methods. I am starting with one such method called getElementsByTag. This method can be very helpful in scenario where you have to find the count of any element or you are searching for some element and ….
As the name suggests it will return the DOM Elements which has the HTML tag which you will be passing as parameter. Return type is List<DOMElements>; which means a list of DOM Elements.
Now we will see the example of this method on our very own google.com 😉
As you would have noticed that google has so many links in its page. Our job is to find out many links are displayed and if there is any link called ‘GMail’ then just click it.
We will call getElementsByTag method on the document to get all the links displayed in the page. I hope you all will be very familiar with basic of HTML tags such as <img> for image <a> for links <input type=”text”> for textbox in form and so on…
Here i would also recommend to use debug tool to understand the HTML structrure of a page. For Firefox there is a plugin called firebug, chrome has inbuilt developer tools…
Now coming back to the problem.. We have to find the number of links in the page so we will pass parameter as a;which is HTML tag for link. Also this method will return list of DOMElement so we will store them in List<DOMElement>
java.util.List<DOMElement> lt = web.document(“/web:window[@index=’0′ or @title=’Google’]/web:document[@index=’0′]”)
.getElementsByTagName(“a”);
Now lt has all the DOMElement which has HTML tag =<a>
int ltLength = lt.size();
for(int i=0;i<ltLength;i++)
{
String strAttr = lt.get(i).getAttribute(“text”);
if(strAttr.equalsIgnoreCase(“gmail”))
{
lt.get(i).click();
}
}
That’s all, SIMPLE, isn’t it?? Do let me know if you have any questions..
Cheers,
Pratik