Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

One possible method to use Selenium and Java to click the download icon of a PDF that has been opened in a new tab is:

  1. Switch to the new tab using the window handles API:
String currentHandle = driver.getWindowHandle();
for (String handle : driver.getWindowHandles()) {
  if (!handle.equals(currentHandle)) {
    driver.switchTo().window(handle);
    break;
  }
}
  1. Locate the download icon using a CSS selector or XPath expression, either by inspecting the page source or using a testing tool like Selenium IDE:
WebElement downloadIcon = driver.findElement(By.cssSelector("a[title='Download PDF']"));
  1. Click the download icon using the click() method:
downloadIcon.click();
  1. Optionally, wait for the PDF file to download using a custom timeout or a wait method like implicit or explicit waits:
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.tagName("body")));

Note that the exact details of the code will depend on the specific website and PDF viewer being used, and may require additional steps such as handling pop-up dialogs or specifying a download folder.