ElementNotVisibleException: Element is not currently visible | Selenium Forum
M
Posted on 06/08/2016
Below is code. Open make my trip. Click on Departure calendar and try to select 17 as date ...
Last statement throwing error.
Please help



Webdriver driver=new FirefoxDriver();


driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.manage().timeouts().pageLoadTimeout(65, TimeUnit.SECONDS);
driver.get("http://makemytrip.com");


driver.findElement(By.xpath("//*[@id='start_date_sec']")).click();

driver.findElement(By.xpath("//a[text()='17']")).click();

LAST command not working


Error:Exception in thread "main" org.openqa.selenium.ElementNotVisibleException: Element is not currently visible

M
Replied on 06/08/2016

Works Now. Check below code

package module15;

import java.util.List;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class delete_later {

public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub


WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

driver.get("http://makemytrip.com");


driver.findElement(By.xpath("//*[@id='start_date_sec']")).click();
WebElement dayElement = driver.findElement(By.xpath("//a[text()='31']"));
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].click();", dayElement);

}

}


M
Replied on 07/08/2016

Hi , Thanks man.
Please can you explain me purpose of last two lines? why we need to add them ?


M
Replied on 07/08/2016

even this code not working:(...


M
Replied on 07/08/2016

It is very fast and selects the date , check the date after selection it will show 31 Aug 16.

JavaScript is language inside the browser to interact with HTML DOM.
Selenium WebDriver provides a JavascriptExecutor interface that can be used to execute
arbitrary JavaScript code within the context of the browser.

Instead of driver.getTitle() we can call uisng JavascriptExecutor

JavascriptExecutor js = (JavascriptExecutor)driver;
string sText = js.executeScript("return document.title;").toString();


M
Replied on 07/08/2016

Hey , if I paste your whole code its working for 31 only , If I change to say 21 not working.
Do I need to change anything for 21??


js.executeScript("arguments[0].click();", dayElement);
what is argument[0] indicates? will this be always fixed?
Secondly do you have any link from where I can go through all Java script command beneficial for webdriver


M
Replied on 07/08/2016

You can observe, that there are two tables displayed on UI i.e August and September.

Suppose if you want to click day 25 , the xpath is same for both august and september month,therefore
Selenium throws "ElementNotVisibleException".

So i used findElements method and clicked on the ( August25)
[b:1qdqg84m]driver.findElements(By.xpath("//a[text()='25'] [@class='ui-state-default']")).get(0).click();[/b:1qdqg84m]

If you want to click on September 25 then
[b:1qdqg84m]driver.findElements(By.xpath("//a[text()='25'] [@class='ui-state-default']")).get(1).click();[/b:1qdqg84m]

So i used below code the fix it. Responsive image

package module15;

import java.util.List;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class delete_later {

public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub


WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

driver.get("http://makemytrip.com");

driver.findElement(By.xpath("//*[@id='start_date_sec']")).click();

int date=driver.findElements(By.xpath("//a[text()='25'] [@class='ui-state-default']")).size();
System.out.println(date);
driver.findElements(By.xpath("//a[text()='25'] [@class='ui-state-default']")).get(0).click();
//driver.findElements(By.xpath("//a[text()='25'] [@class='ui-state-default']")).get(1).click();


}

}


M
Replied on 07/08/2016

Hi ,

Suppose if you want to click day 25 , the xpath is same for both august and september month,therefore
Selenium throws "ElementNotVisibleException".

It should not throw exception , this is weird, It should find 1st element.

Second thing , below code works , but again below code is customized , need common xpath for any date wthether it is 21 , 27 or 31?
is it possible?


M
Replied on 08/08/2016

use this. it works perfectly. if you have any question paste it below.

[code:v1himrss]package com.soapuitutorial.propertie;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Select_date {
static WebDriver driver;

public static void main(String[] args) throws ParseException {
driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.manage().window().maximize();
driver.manage().timeouts().pageLoadTimeout(20, TimeUnit.SECONDS);
driver.get("https://www.makemytrip.com");
selectDate("25/07/2016");
}



public static void selectDate(String date) throws ParseException {

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

SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy");
Date dateToBeSelected = df.parse(date);
Date currentDate = new Date();
String monthYearDisplayed = driver.findElement(
By.xpath("//*[@id='ui-datepicker-div']/div[1]/div/div"))
.getText();
System.out.println("month year displayed " + monthYearDisplayed);
String month = new SimpleDateFormat("MMMM").format(dateToBeSelected);
String year = new SimpleDateFormat("yyyy").format(dateToBeSelected);
String day = new SimpleDateFormat("d").format(dateToBeSelected);
System.out.println("day is " + day);
String monthYearToBeSelected = month + " " + year;
System.out
.println("month year to be selected " + monthYearToBeSelected);

while (true) {
if (monthYearToBeSelected.equals(monthYearDisplayed)) {

driver.findElement(By.xpath("//td/a[text()=" + day + "]"))
.click();
System.out.println("found and selected");
break;

} else {
if (dateToBeSelected.after(currentDate)) {

driver.findElement(
By.xpath(".//*[@id='ui-datepicker-div']/div[2]/div/a"))
.click();
} else {
driver.findElement(
By.xpath(".//*[@id='ui-datepicker-div']/div[1]/div/a"))
.click();
}
}
monthYearDisplayed = driver.findElement(
By.xpath("//*[@id='ui-datepicker-div']/div[1]/div/div"))
.getText();
}
}

}
[/code:v1himrss]


M
Replied on 08/08/2016

Thanks team its working .

Only one concern

By.xpath("//td/a[text()=" + day + "]"))

why after putting //td its working ? is there any logic which we need to keep in our mind going forward?


M
Replied on 08/08/2016

partial xpath have to be specific. that is why we put td in front to make it specific.

PS: please try to post your question Mon to Sat 10AM to 5PM. Sunday is day off.