Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

selenium webdriver: org.openqa.selenium.ElementNotVisibleException: Element is not currently visible and so may not be interacted with

I am trying to click on the span with the text- clone concept. Following is the html

<ul class="context-menu-list context-menu-root" style="width: 210px; top: 500px; left: 231px; z-index: 2;">
    <li class="context-menu-item">
    <li class="context-menu-item">
    <li class="context-menu-item disabled">
    <li class="context-menu-item">
    <li class="context-menu-item icon icon-evn-icon-clone-concept">
        <span>Clone concept</span>
    </li>
    <li class="context-menu-item">
    <li class="context-menu-item icon icon-delete disabled">
</ul>

the javascript code i use is:

driver.findElement(By.xpath("//span[text()='Clone concept']")).click();

I verified that this is the right for the element through firepath.

I also made sure that element is visible as per the link How to force Selenium WebDriver to click on element which is not currently visible?

Here is the computed css

font-family Verdana,?Arial,?Helvetica,?sans-serif
    .context-menu-list  Verdana,?Arial,?Helvetica,?sans-serif   
    jquery...enu.css (line 15)
    body    Arial,?Helvetica,?sans-serif    
    swa.css (line 3)
    font-size   11px
    .context-menu-list  11px    
    jquery...enu.css (line 15)
    list-style-type none
    .context-menu-list  none    
    jquery...enu.css (line 15)

Also tried the following code:

WebElement foo = driver.findElement(By.xpath("//span[text()='Clone concept']"));
Actions bar = new Actions(driver);
bar.click(foo).perform(); 

Exception: org.openqa.selenium.ElementNotVisibleException: Element is not currently visible and so may not be interacted with Command duration or timeout: 30.04 seconds Build info: version: '2.24.1', revision: '17205', time: '2012-06-19 16:53:24' System info: os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.7.0' Driver info: driver.version: RemoteWebDriver

Any help will be appreciated.

Another hack for those who get stuck here:
For the time being I have been able to move forward by splitting this huge test case into simpler test cases.

like image 749
user672365 Avatar asked Aug 22 '12 23:08

user672365


People also ask

How do I fix ElementNotVisibleException?

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 do you do if the WebElement is not visible?

Element not visible exception can be resolved by using Explicit wait. Explicit wait in selenium will wait until the element is visible. Once it's visible you can perform the necessary operation. WebDriverWait wait = new WebDriverWait(driver,30); WebElement e = wait.

How do you handle invisible hidden elements in Selenium?

Selenium by default cannot handle hidden elements and throws ElementNotVisibleException while working with them. Javascript Executor is used to handle hidden elements on the page. Selenium runs the Javascript commands with the executeScript method. The commands to be run are passed as arguments to the method.

How can we handle element which is not visible on the screen because of resolution issue?

Solution: You need to scroll to element using javascript or Actions class so that element is perfectly visible on screen. By using explicit wait and fluent wait as mentioned above.


2 Answers

Unfortunately Webdriver doesn't seem to be great at handling situations like that described in your questions. You have a couple of options though. Mock a click using Javascript:

JavascriptLibrary jsLib = new JavascriptLibrary(); 
jsLib.callEmbeddedSelenium(selenium,"triggerMouseEventAt", elementToClick,"click", "0,0");

or

((JavascriptExecutor) driver).executeScript("arguments[0].click();", elementToClick);

Or you can play around with using actions to click all of the elements in the menu chain. Unfortunately I have found this to be unreliable.

I have a script which detects whether an element is in a menu chain and if it is clicks on them in the required order to finally click on the one the user wanted if you want it I can post it somewhere but it isn't pretty or short.

like image 84
James Avatar answered Nov 03 '22 23:11

James


For the above query, here is the xpath:

//ui[@class='context-menu-list context-menu-root']/span[contains(text(),'Clone concept')]
like image 29
Punith Avatar answered Nov 03 '22 23:11

Punith