1

I did not face any error as no element found however my test case is passed in console but when i checked in download folder it shows some temp file instead of actual image file. It will be very useful if someone solve this issue.

driver.get("https://demoqa.com/elements");

driver.manage().window().maximize();

driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
            
            // Here normal 'findElement' is not working, hence used the javascript executor
            
            WebElement leftmenu = driver.findElement(By.xpath("(//li[@id='item-7']//span)[1]"));
            JavascriptExecutor executor = (JavascriptExecutor)driver;
            executor.executeScript("arguments[0].click();", leftmenu);  //clicking the left menu 
            
            Thread.sleep(5000);
            
            driver.findElement(By.xpath("//a[@download='sampleFile.jpeg']")).click();  // download button
Prophet
  • 32,350
  • 22
  • 54
  • 79

2 Answers2

0

You are finishing the test run and closing the browser immediately after clicking the download button.
Try adding a simple sleep after clicking it.
Also, you should not use hardcoded pauses like this

Thread.sleep(5000);

Explicit wait should be used instead.
Also please try to wait until the element is visible, as I wrote here. I think this will let you clicking it with regular driver .click() method.
Try this:

driver.get("https://demoqa.com/elements");

driver.manage().window().maximize();
WebDriverWait wait = new WebDriverWait(driver, globalDelay);
            
// Here normal 'findElement' is not working, hence used the javascript executor
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("(//li[@id='item-7']//span)[1]"))).click();            
//WebElement leftmenu = driver.findElement(By.xpath("(//li[@id='item-7']//span)[1]"));
//JavascriptExecutor executor = (JavascriptExecutor)driver;
//executor.executeScript("arguments[0].click();", leftmenu);  //clicking the left menu 

wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//a[@download='sampleFile.jpeg']"))).click();            

Thread.sleep(5000);


Prophet
  • 32,350
  • 22
  • 54
  • 79
0

To click() on the element Download you can use either of the following Locator Strategies:

  • linkText:

    driver.findElement(By.linkText("Download")).click();
    
  • cssSelector:

    driver.findElement(By.cssSelector("a#downloadButton[download^='sampleFile']")).click();
    
  • xpath:

    driver.findElement(By.xpath("//a[@id='downloadButton' and starts-with(@download, 'sampleFile')]")).click();
    

Ideally, to click() on the element you need to induce WebDriverWait for the elementToBeClickable() and you can use either of the following Locator Strategies:

  • linkText:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.linkText("Download"))).click();
    
  • cssSelector:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("a#downloadButton[download^='sampleFile']"))).click();
    
  • xpath:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//a[@id='downloadButton' and starts-with(@download, 'sampleFile')]"))).click();
    

PS: Once you click on Download don't close the web browser immediately and wait for sometime for the download process to be completed.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352