Ask Your Question
1

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

asked 2023-03-07 11:00:00 +0000

bukephalos gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2022-04-09 16:00:00 +0000

devzero gravatar image

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.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-03-07 11:00:00 +0000

Seen: 8 times

Last updated: Apr 09 '22