<span class="download-data-link"><a download="" target"_blank"="" style="cursor:pointer">Download file in csv format</a></span>
我需要点击“以csv格式下载文件”,但它始终显示该项无法点击的错误点击查看屏幕截图:
我尝试了所有这些
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();
这是我的最后一年项目......
网站:https://www.nseindia.com/products/content/equities/indices/historical_index_data.htm
答案 0 :(得分:0)
要点击带有以csv格式下载文件的文字的链接,您可以使用以下任一定位器策略:
cssSelector :
driver.findElement(By.cssSelector("span.download-data-link>a")).click();
xpath :
driver.findElement(By.xpath("//span[@class='download-data-link']/a")).click;
我仍然不确定您为何会因为以csv格式下载文件而在文本链接上遇到click()
。我能够点击链接,只需通过诱导服务员点击元素,如下所示:
示例代码:
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();
}
}
浏览器客户端快照:
注意:click()
方法强大,强大和经过验证。您应该尝试以适当的方式和适当的条件使用click()
方法以避免偏差。粗略的动作类和 JavascriptExecutor 接口具有自己的可用性并被广泛使用。
更大数据的代码块:
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();
}
}
注意:浏览器快照保持不变。
答案 1 :(得分:0)
点击 GetData 按钮后,您需要向下滚动,然后才能与WebElement交互,以下是满足您要求的完整代码:
代码:
公共等级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);
}
}
答案 2 :(得分:0)
您可以将动作类用于相同的
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")));