0

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");
  • Does this answer your question? [Selenium: Saving pdf which is opened in new browser without url](https://stackoverflow.com/questions/45240294/selenium-saving-pdf-which-is-opened-in-new-browser-without-url) – Darkproduct Aug 24 '22 at 11:07

2 Answers2

1

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.

Jonatan Kruszewski
  • 1,027
  • 3
  • 23
0

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

K J
  • 8,045
  • 3
  • 14
  • 36