Steps to click on the download icon while a PDF is displayed in a new tab through Selenium

Need help with automating the process of downloading a PDF opened in Chrome browser as a new tab

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");

Answer №1

Check out 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")

By using this method, you can download and use web driver with your specified version.

Answer №2

When it comes to viewing a PDF in a browser, the PDF and its viewer are essentially separate entities from the browser itself. This means that while the browser can interact with the HTML content surrounding the PDF, it cannot directly interact with the PDF viewer.

There are various workarounds available, such as bypassing web security measures and utilizing keyboard shortcuts like print keys. However, when trying to automate tasks external to the browser, the focus needs to be firmly on the application at hand.

For a detailed explanation and possible solution, you can refer to this link

Due to the fact that the print pane functions as a desktop window across all browsers, automating Save as PDF using Selenium is not straightforward. Some attempt to utilize chrome options, AutoIt, or Robot class to address this issue, but success is not guaranteed.

In my non-default setup, I am limited to using the sendkeys method for printing.

https://i.stack.imgur.com/3cOde.png

One effective way to ensure a PDF download is by bypassing any user interactions and setting always_open_pdf_externally to fetch the file directly using cUrl. More information on this approach can be found here

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Using Selenium in Java to interact with popup elements

Attempting to retrieve and interact with pop-up/alert elements using selenium in Java has been a bit challenging for me. Below is the code snippet I have been working on: import org.openqa.selenium.By; import org.openqa.selenium.JavascriptExecutor; import ...

Retrieve text using Selenium WebDriver in C#

Looking at the HTML element on my page: <li id="city" class="anketa_list-item"> <div class="anketa_item-city">From</div> London </li> I managed to locate this element: driver.FindElement(By.Id("city")) When I use: driver. ...

Utilize Selenium C# to locate and update information within a text file

I have a situation where I need to upload a text file in the UI, but this text file requires updating every time before the test is run. To achieve this functionality, I am looking to capture it within my Selenium C# script. The data format of the text fi ...

Why does JavaScript function flawlessly in FireFox, yet fails completely in other web browsers?

When it comes to browsing, FireFox is my go-to browser, especially for testing out my website Avoru. However, I recently encountered an issue when checking the functionality of my code on other major browsers like Google Chrome, Opera, and Safari. It seems ...

Unable to configure Luminati proxy with Selenium using Python 3

Currently, I am in the process of configuring a proxy for the Firefox driver using Selenium on Python. To set up the proxy, I followed the guidelines provided in this instruction: https://github.com/luminati-io/api/blob/master/python/3.x/simple.py userna ...

What is the process for executing double clicks and right clicks using WebDriver?

In my current project, I am exploring the use of Selenium 2 for automation purposes. However, I am encountering some challenges that I need help with: What is the best way to perform a double click on a web element using Selenium? How can I execute ...

Unable to fetch any links through the driver.find_elements method using the href attribute

As a newcomer to Python and Selenium WebDriver, my goal is to verify all links on my webpage by checking their HTTP status codes to identify any broken links. The snippet of code I am currently using looks like this... from selenium import webdriver from ...

Run a Firefox Browser within a Docker Container for Selenium testing purposes

Currently, I am utilizing a Java Application to manage an automated GUI test on a FF-Browser using the Selenium WebDriver Library. This application fetches test cases from a database and runs them based on specified code logic. For example, when the appli ...

Searching for an element using Python's Selenium library by referencing the value of its sibling element

This is an example of HTML code structure: <div> <span>Title</span> <input value="a"> </div> <div> <span>Price</span> <input value=""> </div> The task at hand is to ch ...

Selenium is having trouble getting Firefox to open maximized and switch tabs

My challenge is initiating the Firefox browser through Selenium (3.141.0) on Python 3.9, where it always starts minimized despite adding the function .maximize_window() in the code after opening the browser. The issue arises when the browser is started thi ...

Where is the appropriate location to input parameters in Selenium testing?

What is the best approach for passing parameters? The code snippet below has been modified to focus on relevant pieces... I currently pass in login parameters directly within the [TestMethod], rather than within the LoginPage class object. Is this the co ...

The WebDriver appears to be using a null Session ID from Selenium, possibly due to calling quit() before continuing with another test. This

When trying to run a test in Selenium with Junit, I encountered an issue. The test executes but I receive the following failure message - 1) testTripPlannerJUnit(com.example.tests.TripPlannerJUnit) org.openqa.selenium.NoSuchSessionException: Session ID ...

When executing in the local development environment, Selenium tests encounter errors and reach the error page with a connection issue (about:neterror?e=connection). However, the same tests fail

Encountering a perplexing issue with a Selenium test that fails in Travis CI but runs smoothly in the local development environment. The error message indicates that Firefox is unable to establish a connection to the server at localhost. To troubleshoot th ...

Is it impossible to determine the actual filenames, or only what the array generated instead?

I have been working on creating an automated uploader that will efficiently sort through a directory and upload the next file in line during each run. My initial step was to generate a text file that would contain the names of the files. static String fil ...

While Xpath can be successfully located within Elements and Console, it encounters difficulty when attempting to be located during application execution

I'm currently enhancing my skills with Selenium Webdriver along with ChromeDriver, focusing on XPath. Below is the code snippet that I attempted to run: WebDriver driver; String baseURL = "http://youtube.com"; System.setProperty("webdr ...

Development of Chrome Extensions, JavaScript dilemma

Hey there, I'm new to JavaScript and I've been diving into the world of creating Chrome extensions. I'm trying to set up a content script and browser action, but I'm struggling to get it up and running. I know I'm probably making a ...

Deactivate the Shockwave Flash plugin by utilizing Selenium and Python programming

Having issues with the Shockwave Flash plugin crashing during a crawling project. Is there an easy method to disable it from the start? Your assistance is greatly appreciated! ...

Encountering a TimeoutException while utilizing Selenium in Python

Utilizing WebDriverWait in my script involves two different instances with the same syntax. The first time I use it is to check for any typos within a defined class, while the second time is under a function nested within the same class. Surprisingly, an e ...

When using jQuery with a large selectbox, you may encounter the error message: "Uncaught RangeError: Maximum

My select box works fine in IE and Mozilla, but throws an uncaught rangeError in Chrome when choosing the "Others" option to display a second select box with over 10k options. How can I diagnose and resolve this issue? <!DOCTYPE html> ...

Python - ExceptionalInputRequirement

I'm diving into the world of web scraping, but I keep running into roadblocks whenever I attempt to access a URL. Here's the code I'm working with: from selenium import webdriver from selenium.webdriver.common.keys import Keys driver = w ...