15

Am using selenium webdriver 2.210 + JAVA for testing.I have a sample code for selecting all mails in gmail.But the code throws an "Element is not currently visible and so may not be interacted with" error when i tries to put a 5sec delay after getting URL through webdriver.Is it possible to make this code working with delay?

    driver.get("https://mail.google.com/mail/u/0/?shva=1#all");
        delay(5);  ////*......Working fine without this...........*////
    driver.switchTo().frame(driver.findElement(By.id("canvas_frame")));
driver.findElement(By.xpath("//div[@class = 'T-Jo-auh']")).click();

Thanks in advance

hks1233
  • 196
  • 1
  • 2
  • 8
  • 2
    You need to try to understand (and tell us) why this happens. Is the first(!) `//div[@class = 'T-Jo-auh']` element on page really invisible after some time? What do you see when you find it with Firebug? You could try `List list = driver.findElements("//div[@class = 'T-Jo-auh']");` and then look in the list for the first visible element by testing with [`isDisplayed()`](http://selenium.googlecode.com/svn/trunk/docs/api/java/org/openqa/selenium/WebElement.html#isDisplayed%28%29) – Petr Janeček May 17 '12 at 18:57

11 Answers11

20

Are you sure you're looking at the right element? I had a similar problem and it turned out there were two similar elements on the page, one visible and the other not. The FindElement function was returning the one that wasn't visible.

I solved this by using FindElements instead of FindElement and then using Linq to extract the one that was visible.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Kesty
  • 610
  • 9
  • 19
6

Here is a summary of things you can do to tackle the problem (examples in Protractor/Javascript):

  • maximize the browser window (on Chrome+Mac, currently you have to do it differently):

    browser.driver.manage().window().maximize();
    
  • verify that there are no other elements matching the locator. You can get this error if there is an another element matching the locator that is actually invisible.

  • wait for the element to be clickable:

    var EC = protractor.ExpectedConditions,
        elm = element(by.id("myid"));
    
    browser.wait(EC.elementToBeClickable(elm), 5000);
    
  • scroll into view of the element:

    var elm = element(by.id("myid"));
    browser.executeScript("arguments[0].scrollIntoView();", elm);
    
  • click via javascript:

    var elm = element(by.id("myid"));
    browser.executeScript("arguments[0].click();", elm);
    
  • move to element and click via "browser actions":

    var elm = element(by.id("myid"));
    browser.actions()
        .mouseMove(elm)
        .click()
        .perform();
    
Community
  • 1
  • 1
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
3

Alternatively, you can use the JavascriptExecutor class provided in Selenium. After you do that, you can execute any JavaScript to manipulate the DOM on a Web page.

Ref:: http://selenium.googlecode.com/svn/trunk/docs/api/java/org/openqa/selenium/JavascriptExecutor.html

Sandy
  • 5
  • 2
armeo
  • 199
  • 1
  • 1
  • 7
1

I'm not sure, but every watir-webdriver element has the - (Object) wait_until_present(timeout = 30) method.

if this step is optional, you should check for visibility:

element = driver.findElement(By.xpath("//div[@class = 'T-Jo-auh']"));

if (element.isDisplayed()) {
  element.click();
}

Please note, I'm not a java guru, and the code above was not tested. Give it a try.

  • The `wait_until_present()` is not present in WebDriver, but can be done in [a similar way](http://seleniumhq.org/docs/04_webdriver_advanced.html#explicit-and-implicit-waits). The problem here is that the element IS present, just not visible (and in that case, WebDriver refuses to interact with it). Also, your `visible()` method is called `isDisplayed()` – Petr Janeček May 17 '12 at 19:05
  • Thanks for the clarification Slanec! Making the element visible with JS would be a cheat? :) – GoobeMaster May 17 '12 at 19:23
1

Selenium will not interact with WebElements that are hidden or that are not displayed to the user. In this case, it's not unusual for user clicks to interact with a div element or something similar which in turn triggers the actual button, which is hidden for visual purposes. I'd suggest running through the steps in the selenium IDE in firefox on your page. See if multiple events are triggered when you perform the click on your "hidden" element. In the case that multiple events are in fact triggered, follow suit in your WebDriver code.

AndyPerfect
  • 1,180
  • 1
  • 10
  • 25
0

Try to downgrade selenium webdriver to 2.20.0.

I got similar error with ruby gem version 2.21.0 and 2.21.2.

In my case web driver always return button.visible? = false in case of button is added to the page via .Ajax call.

My tests work previously for a longer time and nothing related has changed. So I assume this is a bug in the current version of webdriver.

Bjørn-Roger Kringsjå
  • 9,849
  • 6
  • 36
  • 64
Grimmo
  • 1,485
  • 14
  • 13
0

If you application uses jQuery, you may do the clicks using Javascript. I created this simple helper for clicking elements that WebDriver refuses to find:

public static void jqClick(String selector, JavascriptExecutor driver) {
    driver.executeScript("$('" + selector + "').click()");
}

As the "driver", you can use, for instance a org.openqa.selenium.firefox.FirefoxDriver.

This was the only solution that worked for me.

raimohanska
  • 3,265
  • 17
  • 28
0

In my case (PhantomJSDriver called from Selenium WebDriver in c#) I had to set the Window Size to be large enough for the element to be visible:

driver.Manage().Window.Size = new Size(1000, 800);

I discovered that workaround reading through the issues here: https://github.com/ariya/phantomjs/issues/11637

Matthew Lock
  • 13,144
  • 12
  • 92
  • 130
0
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("document.getElementById('id').setAttribute('visibility', 'true');");

driver.findElement(By.id("id")).click();

by changing the visibility of the element you can perform your action. you can either click with selenium or with JavascriptExecutor

pradeep
  • 29
  • 4
0

PhantomJS users should watch for the maximize-window answer. There is a good chance that a javascripted dialog box is thought to be outside the viewport even when a screenshot shows it to be fully visible. Since it is clearly visible some scroll-into-view action does not change anything, and all the other actions are useless as well. (Same for other browsers based on the WebKit engine, e.g. on MacOS Safari and old Chrome)

Guido U. Draheim
  • 3,038
  • 1
  • 20
  • 19
0

Its weird but i have to set the browser size explicitly in Java.

driver.manage().window().setSize(new Dimension(1000, 800));

Vaibs
  • 2,018
  • 22
  • 29