Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How I can avoid "Element is not currently visible and so may not be interacted with " Selenium Webdriver

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

like image 514
hks1233 Avatar asked May 17 '12 18:05

hks1233


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.

How do you interact with 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 does Selenium handle element not displayed?

We can verify if an element does not exist in Selenium webdriver. To achieve this, we shall use the method getPageSource which gets the entire page source. So we can obtain a complete page source and check if the text of the element exists.

What will be your approach if an element is not clickable?

The exception “Element is not clickable at point” might be thrown when the element is not under focus or the action is being performed on the incorrect WebElement. In such cases, you have to switch to the actual element and perform the click action.


2 Answers

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.

like image 156
Kesty Avatar answered Sep 18 '22 18:09

Kesty


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();
    
like image 22
alecxe Avatar answered Sep 21 '22 18:09

alecxe