ajay gawade
ajay gawade

Reputation: 1

how to click on download icon using selenium when pdf is opened in new tab

pdf opened in chrome browser new tab, now want to click on download icon

WebElement root=DriverManager.getDriver().findElement(By.id("viewer"));
    WebElement shadowdom1=getShadowDOM(root,DriverManager.getDriver());
    WebElement toolbar=shadowdom1.findElement(By.tagName("viewer-toolbar"));
    WebElement shadowdom2=getShadowDOM(toolbar,DriverManager.getDriver());
    WebElement downloads=shadowdom2.findElement(By.tagName("viewer-download-controls"));
    WebElement shadowdom3=getShadowDOM(downloads,DriverManager.getDriver());
    WebElement crIconbutton =shadowdom3.findElement(By.tagName("cr-icon-button"));
    WebElement shadowdom4=getShadowDOM(crIconbutton,DriverManager.getDriver());
    WebElement downloadIcon =shadowdom4.findElement(By.tagName("iron-icon"));
    Assert.assertTrue(downloadIcon.isDisplayed(),"No download option available");

Upvotes: 0

Views: 1823

Answers (2)

Zihad Milon
Zihad Milon

Reputation: 11

Give a look to the following:

from selenium import webdriver

import time

options = webdriver.ChromeOptions() ;

prefs = {"download.default_directory" : "C:\Tutorial\down"};

options.add_experimental_option("prefs",prefs);

driver = webdriver.Chrome(executable_path='./chromedriver',chrome_options=options);
    driver.get('https://www.browserstack.com/test-on-the-right-mobile-devices');

    downloadcsv= driver.find_element_by_css_selector('.icon-csv');

    gotit= driver.find_element_by_id('accept-cookie-notification');

    gotit.click();    

    downloadcsv.click();

    time.sleep(5)

    driver.close()

except:

     print("Invalid URL")

Using this way you can download and take web driver with your version.

Upvotes: 1

K J
K J

Reputation: 11811

In principle the pdf and its viewer in a browser is an independent windowed application/pdf thus the browser can interact with the HTML carrier but not the PDF viewer.

There are many workarounds such as bypass web security and press the print keys, but the focus would need when driven external to be firmly focused on the application.

Perhaps the best description and solution is here http://thetestmate.com/save-as-pdf-in-chrome-testmate-selenium/

Since the print pane is a desktop window in all the browsers, Save as PDF can not be automated using Selenium. People try to use different chrome options, AutoIt or Robot class to handle such scenarios but that does not always work.

Here using sendkeys in my non default setup I am reminded I have only one print method

enter image description here

One reliable way to enforce PDF download is bypass any user icon by setting always_open_pdf_externally to cUrl the file direct, and that is described here https://stackoverflow.com/a/73386059/10802527

Upvotes: 0

Related Questions