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.
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);
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
We provide hands-on training on automation tools and frameworks