<span class="download-data-link"><a download="" target"_blank"="" style="cursor:pointer">Download file in csv format</a></span>
I need to click on the "Download file in csv format" but it always shows error that item is not clickable at that point click to see the screenshot :
I have tried all these
element=driver.findElement(By.xpath("//div[@class='download-data-link']/span"));
element.click();
WebDriverWait wait = new WebDriverWait(driver, 20);
WebElement link = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//a[contains(@href,'data:application/csv;')]")));
link.click();
element=driver.findElement(By.xpath("//tr[@class='alt']/span[@class='download-data-link']"));
element.click();
driver.findElement(By.cssSelector(a[class='download-data-link']span)).click();
driver.switchTo().frame("iframe-applicationname_ModalDialog_0");
element=driver.findElement(By.linkText("Download file in csv format"));
element.click();
driver.switchTo().defaultContent();
element = driver.findElement(By.className("downoad-data-link"));
element.click();
its my final year project ...
website : https://www.nseindia.com/products/content/equities/indices/historical_index_data.htm
You can use action class for same,
Actions action = new Actions(driver);
action.moveToElement(driver.findElement(By.xpath("//span[@class='download-data-link']//a"))).click().perform();
OR
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].click();",driver.findElement(By.xpath("//span[@class='download-data-link']//a")));
After clicking on GetData button , you need to scroll down and then you can interact with the WebElement, here is the full code for your requirement:
Code :
public class M{
static WebDriver driver;
static WebDriverWait wait;
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "F:\\Automation_Learning\\chromedriver.exe");
driver = new ChromeDriver();
wait =new WebDriverWait(driver, 10);
driver.manage().window().maximize();
driver.get("https://www.nseindia.com/products/content/equities/indices/historical_index_data.htm");
wait.until(ExpectedConditions.visibilityOf(driver.findElement(By.cssSelector("div[class='content']"))));
wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("input[class$='hasDatepicker'][id='fromDate']")));
driver.findElement(By.cssSelector("input[class$='hasDatepicker'][id='fromDate']")).click();
wait.until(ExpectedConditions.visibilityOf(driver.findElement(By.id("ui-datepicker-div"))));
driver.findElement(By.cssSelector(".ui-datepicker-month")).click();
selectDate(driver.findElement(By.cssSelector(".ui-datepicker-month")),"Jan" );
driver.findElement(By.cssSelector(".ui-datepicker-year")).click();
selectDate(driver.findElement(By.cssSelector(".ui-datepicker-year")), "2017");
driver.findElement(By.xpath("//table[@class='ui-datepicker-calendar']/descendant::td[contains(@class,'ui-datepicker-week-end')]/a[text()='1']")).click();
wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("input[class$='hasDatepicker'][id='toDate']")));
driver.findElement(By.cssSelector("input[class$='hasDatepicker'][id='toDate']")).click();
wait.until(ExpectedConditions.visibilityOf(driver.findElement(By.id("ui-datepicker-div"))));
driver.findElement(By.cssSelector(".ui-datepicker-month")).click();
selectDate(driver.findElement(By.cssSelector(".ui-datepicker-month")),"Dec");
driver.findElement(By.cssSelector(".ui-datepicker-year")).click();
selectDate(driver.findElement(By.cssSelector(".ui-datepicker-year")), "2017");
driver.findElement(By.xpath("//table[@class='ui-datepicker-calendar']/descendant::td[contains(@class,'ui-datepicker-week-end')]/a[text()='31']")).click();
wait.until(ExpectedConditions.elementToBeClickable(By.id("get")));
driver.findElement(By.id("get")).click();
wait.until(ExpectedConditions.visibilityOf(driver.findElement(By.cssSelector("div[class$='data-historic']"))));
Actions action = new Actions(driver);
action.keyDown(Keys.LEFT_CONTROL).sendKeys(Keys.END).keyUp(Keys.LEFT_CONTROL).build().perform();;
wait.until(ExpectedConditions.elementToBeClickable(By.linkText("Download file in csv format")));
driver.findElement(By.linkText("Download file in csv format")).click();
}
public static void selectDate(WebElement element , String textToBeSelected){
Select select = new Select(element);
select.selectByVisibleText(textToBeSelected);
}
}
To click on the link with text as Download file in csv format you can use either of the following Locator Strategies :
Update A
I am still not sure why you were stuck with the click()
on the link with text as Download file in csv format. I was able to click on the link just by inducing a waiter for the element to be clickable as follows :
Sample Code :
package demo;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class Q50035477_click_link {
public static void main(String[] args) {
System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.get("https://www.nseindia.com/products/content/equities/indices/historical_index_data.htm");
driver.findElement(By.cssSelector("input.textboxdata.hasDatepicker#fromDate")).sendKeys("23-04-2018");
driver.findElement(By.cssSelector("input.textboxdata.hasDatepicker#toDate")).sendKeys("25-04-2018");
driver.findElement(By.cssSelector("input.getdata-button")).click();
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.linkText("Download file in csv format"))).click();
}
}
Browser Client Snapshot :
Note : click()
method is robust, powerful and proven. You should try to utilize the click()
method in a proper way and in proper condition to avoid deviations. Of-coarse Actions class and JavascriptExecutor interface have their own usability and are used extensively.
Update B
Code block for larger data :
package demo;
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;
public class Q50035477_click_link {
public static void main(String[] args) {
System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.get("https://www.nseindia.com/products/content/equities/indices/historical_index_data.htm");
driver.findElement(By.cssSelector("input.textboxdata.hasDatepicker#fromDate")).sendKeys("01-01-2017");
driver.findElement(By.cssSelector("input.textboxdata.hasDatepicker#toDate")).sendKeys("31-12-2017");
driver.findElement(By.cssSelector("input.getdata-button")).click();
WebElement element = driver.findElement(By.linkText("Download file in csv format"));
((JavascriptExecutor)driver).executeScript("arguments[0].scrollIntoView(true);", element);
element.click();
}
}
Note : Browser snapshot remains the same.