Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java webdriver: Element not visible exception

I'm having the following problem. I have a dropdown that is hidden so when I make the Select and run the test i get the following error:

 org.openqa.selenium.ElementNotVisibleException: element not visible: Element is not currently visible and may not be manipulated
  (Session info: chrome=30.0.1599.101)

This is my select:

Select s = new Select(dropDown);
s.selectByVisibleText("CHARGEBACK");

Is there a walk around it to manipulate hidden elements?. I found the following code in one of the posts:

 JavascriptExecutor jse = (JavascriptExecutor) driver;
 jse.executeScript("arguments[0].scrollIntoView(true);", element);

This is the html code:

 <div class="ui-helper-hidden">
<select id="formLevel:levels_input" name="formLevel:levels_input">
<option value="541fac58-5ea8-44ef-9664-e7e48b6c6a3c">Seleccione un Registro</option>
<option value="dafc799c-4d5e-4b02-a882-74cb6ad98902">SECURITY</option>
<option value="e5416086-2036-4cd0-b23e-865747aa3f53">CALL CENTER</option>
<option value="7ea4b4ea-4f06-4d27-9541-1b0cf3f2aa22">CHARGEBACK</option>
<option value="0f915120-7b8f-4a33-b063-5d20a834b655">PREVENÇÃO A FRAUDE</option>
<option value="a8ef13e8-f4a5-43b8-a668-b769f6988565">ANALISE DE CREDITO</option>
<option value="83b65a26-d4cd-43d3-b3fa-2f7894ca454a">SUPORTE A CONTA</option>
<option value="163d0db9-590c-47a7-a271-218b2d27d8d9">REGULARIZAÇÃO FINANCEIRA</option>

And it doesn't work in this case. Any help would be appreciated.

like image 742
elcharrua Avatar asked Oct 28 '13 14:10

elcharrua


People also ask

How do you handle an element not visible exception?

First Solution: Try to write unique XPATH that matches with a single element only. Second Solution: Use Explicit wait feature of Selenium and wait till the element is not visible. Once it is visible then you can perform your operations.

What is element not visible exception?

Class ElementNotVisibleExceptionThrown to indicate that although an element is present on the DOM, it is not visible, and so is not able to be interacted with.

Why is there no element exception in Selenium?

There are two main reasons for a NoSuchElementException : The Selenium locator strategy you adopted does not identify any HTML element in the DOM. There's a few ways this can occur: The HTML element is not yet present in the web page, just when the desired element is rendered asynchronously as a result of an AJAX call.

What is exception thrown by Webdriver wait if element not found?

NoSuchElementException Happens when the locators are unable to find or access elements on the web page or application. Ideally, the exception occurs due to the use of incorrect element locators in the findElement(By, by) method. To handle this exception, use the wait command.


1 Answers

Since WebDriver tries to simulate real users, it cannot interact with elements which are invisible/hidden. To solve your issue, I think you would need to click on div first which will make the drop down visible and select option from the dropdown. I would recommend such an approach as opposed to pure Javascript way since it would simulate a real user. Give following a shot,

WebDriverWait wait = new WebDriverWait(driver, 300);
WebElement triggerDropDown = driver.findElement(By
                .className("ui-helper-hidden"));
triggerDropDown.click();
WebElement selectElement = wait.until(ExpectedConditions
                  .visibilityOfElementLocated(By.id("formLevel:levels_input")));
Select select = new Select(selectElement);
select.selectByVisibleText("SECURITY");

Edit updated the class name of triggerDropDown

like image 192
nilesh Avatar answered Sep 19 '22 02:09

nilesh