0

I have a scenario to click on 'X' icon from an overlay. When I check for the element using selenium.isElementPresent("//img[@src='close.jpg']"); it is returning true. But when I perform selenium.click("//img[contains(@src,'close.jpg')]"); action it throws an exception "Element is not currently visible and so may not be interacted with".

Tried with all types of xpath but couldn't resolve the problem.

2
  • 1
    Which browser are you using. I know this problem from IE. Does it work out in Firefox? Nov 28, 2012 at 13:02
  • I am facing the problem in Firefox.
    – deactivate
    Nov 29, 2012 at 4:52

1 Answer 1

3

first of all try using couple of methods:

public boolean isElementPresent(By selector)
   {
       return driver.findElements(selector).size()>0;
   }

    public boolean isElementVisible(By selector){
        return driver.findElement(selector).isDisplayed();
    }

then if your page not compeltely rendered try using some wait mechanisms:

Thread.sleep(1000);


driver.manage().timeouts().implicitlyWait(2,TimeUnit.SECONDS);

and fluentWait mechanism: .

  public WebElement fluentWait(final By locator){
            Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
                    .withTimeout(30, TimeUnit.SECONDS)
                    .pollingEvery(5, TimeUnit.SECONDS)
            .ignoring(org.openqa.selenium.NoSuchElementException.class);
            WebElement foo = wait.until(
                    new Function<WebDriver, WebElement>() {
                        public WebElement apply(WebDriver driver) {
                            return driver.findElement(locator);
                        }
                    }
            );
            return  foo;              }     ;
    fluentWait(By.xpath(..blablabla...)).click();

you can also make jsCode injection: 1) if jQuery is supported

String cssSelector="blablabla";
JavascriptExecutor js = (JavascriptExecutor) driver;
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("var x = $(\'"+cssSelector+"\');");
        stringBuilder.append("x.click();");
        js.executeScript(stringBuilder.toString());

2) locating needed element through DOM model: e.g.

String js="document.getElementsByTagName('div')[34].click();"
jsCodeExecution(js);
public void jsCodeExecution(String jsCode){
        JavascriptExecutor js = (JavascriptExecutor) driver;
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(jsCode);
        js.executeScript(stringBuilder.toString());
    }

try also debug step by step to see exactly where the problem is. Hope it helps you.

Moreover, I recommend you to verify your found css selectors and xpath verify in firepath (addon to firebug in firefox): enter image description here

2
  • Thanks for the solution. Using the jQuery solution I am able to resolve the issue but one more issue is that the cssSelector value is dynamic. I tried using xpath in that case but of no use and just to mention no error message is displayed. Please let me know if we can handle css which is dynamic.
    – deactivate
    Nov 29, 2012 at 6:43
  • well. Could you clarify what you mention saying cssSelector value is dynamic? As far as I can see from your question - css selector of your element is String cssSelector="img[src='close.jpg']" and this css selector rigorously defines your web element. Nov 29, 2012 at 10:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.