Monday 29 December 2014

A better way of handling hidden elements in Selenium



We have already discussed how to interact with the hidden elements in the webpage in this blog.

There could still be challenges in identifying and performing specific actions on the hidden elements, especially when using Selenium WebDriver as the test automation tool, which can interact only with elements that are in visible state on the webpage.

Here are the couple of other solutions to interact with hidden webelements in a better way.

1. When we need to perform, just a click operation on the any webelement irrespective of the element having the unique html id assigned or not. We can use the below method.


a.        In case of ID exist for the element, we can use .getElementById javascript method to perform the click operation

String Script = "javascript:document.getElementById('testobject').click();";
((JavascriptExecutor) webdriver).executeScript(Script);

b.       In case there is no id or name assigned to the webelement, still we can interact as given below.

Identify the element by any of the standard webdrive By class.

WebDriver element = webdriver.findElement(By.xpath(“<locator>”));

Now the argument[0] will always represent the webelement holding by “element” object in the above statement.

JavascriptExecutor Executor = (JavascriptExecutor)webdriver);
Executor.executeScript(“arguments[0].click();”, element);

In fact we can even perform complex actions using javascript support like scrolling the current webpage view. Actually Selenium 2 or webdriver implicitly scrolls the page to the respective element to be clicked or to perform the action on it.

We can still perform the scroll actions explicitly using the below statement whenever necessary.

Executor.executeScript("arguments[0].scrollIntoView(true);", element);


2.
 We can even perform the action on the specified elements by making it to visible during run time of the automation script.



Having the firebug addon in firefox as the tool to identify and debug the object locator effectively we can even change the any attribute value of the html element temporarily on any webpage.





Generally in web application, elements visibility is controlled majorly using the CSS (which are attached to webpages) attributes like style=”display: none;” as shown above.

We can change the style attribute or even add new attributes which can cause a temporary change in the application look and feel which helps to execute selenium automation scripts uninterruptedly.

Below, we have changed the style property as style=”visibility : visible; which has made the drop down or combo box as visible for selenium WebDriver interactions.




This can be performed during runtime of the automation test script using the below command. Again to remind you, we can use the javascript to change the attribute values dynamically.

Executor.executeScript(“arguments[0].setAttribute(‘style’, ‘visibility: visible;’);”, element);

With the above methods we can handle the hidden elements in an effective manner.




ByAutomationMentorwww.automationmentor.in
We provide hands-on training on automation tools and frameworks

27 comments:

  1. hi can you please tell how to handle a javascript alert/popup which has hidden objects..ex:dice.com,search with a job,and in search results page select create an alert link which pops up a javascript frame.

    ReplyDelete
  2. Thanks a lot for sharing a informative article.
    It was very much helpful..
    Best Selenium training in Chennai

    ReplyDelete
  3. Anybody suggest me i want to hide the webelement in selenium.

    ReplyDelete
  4. woow very informative blog! thanks a ton!

    ReplyDelete
  5. Hi,
    I Can able to select option using above mentioned method for all browsers except SAFARI.
    Currently Im using Safari, 11.0, macOS 10.13
    Is there any other way to select option from dropdown for "Safari" when we have style=”display : none;"

    ReplyDelete
  6. Hi,
    I Can able to select option using above mentioned method for all browsers except SAFARI.
    Currently Im using Safari, 11.0, macOS 10.13
    Is there any other way to select option from dropdown for "Safari" when we have style=”display : none;"

    ReplyDelete
  7. WebDriver element = webdriver.findElement(By.xpath(“”));
    Doesn't WebDriver above should have been WebElement? If we are trying to store an Web Element????

    ReplyDelete
  8. Nice Blog..Well explained..Thanks for sharing.I refer this blog to my friends.keep update best selenium training institute in chennai
    Training with placement in chennai

    ReplyDelete
  9. Thank you for all the comments

    ReplyDelete
  10. Rash from Grover's Disease can be extraordinarily irritated. Natural Treatment decrease itching ordinarily start with high-power steroid creams, for example, triamcinolone or clobetasol, beside with antihistamines. Natural Remedies for Grover's Disease provides the relief from skin disorders. It helps to reducing rashes from skin that occur from Grover’s Disease.

    ReplyDelete
  11. Natural Remedy for Achalasia includes some most effective herbs and natural remedies to treat this disease. Herbal Supplement can help relieve the symptoms.

    ReplyDelete
  12. Herbs Solutions By Nature Herbal Supplement for Health and Skin Diseases. Natural Herbal Treatment really does help you recover from your health condition.

    ReplyDelete

  13. A hydrocele is a fluid-filled sac that surrounds a testicle and produces enlargement in the scrotum. The majority of hydrocele cases are caused by birth, and newborn kids are at a greater risk of developing hydrocele. It mostly affects males rather than women. They may strike men of any age. So Organic Herbal Supplements are the best option to cure from that disease.

    ReplyDelete
  14.  Herbal Supplement for Health and Skin Diseas to help improve the appearanceHome Remedies for Granuloma Annulare, grape seed extract is one of my favorite ingredients. In medical science, grape seed extractNatural Remedies for Granuloma Annulare is Vitamin E oil.— Herbal Supplement for Granuloma Annulare which may help explainHerbal Remedies for Granuloma Annulare for soothing your skin and reducing inflammation.

    ReplyDelete
  15. Herbal Supplement for Actinic Keratosis is highly recommended to all people suffering from these diseases as they offer positive results at an affordable price. Herbal Remedies for Actinic Keratosis, It immediately reduced redness.The extract of olive leaf has been used for thousands of years to treat various skin conditions. Herbal Remedies for Actinic Keratosis, It immediately reduced redness.

    ReplyDelete
  16. Natural Remedies for Burning Mouth Syndrome Options (2013). According to his experience,
    Changing your diet can have a tremendous impact on your burning mouth syndrome symptoms.One quick remedy is taking Breneton; it’s an Herbal Supplement for Burning Mouth Syndrome that’s extremely effective at treating and reducing burning mouth syndrome symptoms. how much pain and discomfort you’re experiencing and other things associated with burning mouth syndrome. Note your symptoms before taking Herbal Supplements.

    ReplyDelete
  17. One of several ways you can treat granuloma annulare is using a potent and effective Herbal Remedies for Granuloma Annulare. Herbal supplements for granuloma annulare include milk thistle, borage oil, dandelion root, red clover extract, burdock root, white willow bark, and St. John’s wort, among others. It would help if you chose a 100% natural herbal supplement that contains only pure herbs without fillers or additives.

    ReplyDelete
  18. Bronchiectasis Natural Treatment: Although bronchiectasis is not curable, you can manage it with proper treatment. The disease causes the bronchial tubes to enlarge, making it difficult to breathe.One of nature’s most bountiful and powerful Herbal Supplement for Bronchiectasis, honey is a natural antibiotic and anti-inflammatory.Using ACV as a Natural Treatment for Bronchiectasis may help relieve chest tightness and pain associated with breathing.

    ReplyDelete
  19. Emphysema is a disease that makes breathing very difficult. It damages your lungs, so they cannot get as much oxygen in and out of your body. It can be excruciating to live when you have emphysema. Hysmeton is an Herbal Supplement for Emphysema to help with symptoms of emphysema and asthma.There are many different symptoms of emphysema. It is often a silent killer because people with severe emphysema may not have any symptoms.

    ReplyDelete
  20. After being diagnosed with the problem, Natural Treatment for Abdominal Adhesions should be started right away. Sedeton, a herbal supplement, is the best treatment for it.But if you are afeared from surgical treatment then must try Herbal Supplement for Abdominal Adhesions.. It's also been shown to help regulate bowel motions and ease stomach pains and cramps, which are signs of partial intestinal obstruction caused by abdominal adhesions. Follow the instructions on the package for the most effective Alternative Treatment for Abdominal Adhesions.

    ReplyDelete