Tips for using Selenium to interact with the save or print button in the Google Chrome print window, even when the HTML page contains a shadow-root

https://i.stack.imgur.com/NFJat.pngMy goal is to use Selenium and Python for web scraping. I am currently using Chrome version 123.0.6312.87 with a compatible web driver. Specifically, I am interested in extracting data from the following page: "https://web.bcpa.net/BcpaClient/#/Record-Search". Upon entering the address "2216 NW 6 PL FORT LAUDERDALE" on this page using Selenium, detailed property information is displayed. On clicking the Print button through Selenium, it redirects me to a new page "https://web.bcpa.net/BcpaClient/recinfoprint.html". Within this HTML page, there is a dropdown menu under the class "md-select" that contains the option to select "Save As PDF" with the value "Save as PDF/local/". Unfortunately, due to the presence of shadow root elements, Selenium is unable to locate the position of the "md-select" class.

Here is the relevant code snippet:

(insert modified code here)

I aim to interact with the "Save As PDF" option within the "md-select" class and subsequently click the Save button located in the "action-button" class. However, the presence of shadow roots has posed significant challenges in achieving this functionality. Despite attempts to extract information from the "print-preview-app" located before the shadow root, the script encounters issues.

The execution of the code stops after implementing the WebDriver shadow root method.

Answer №1

When dealing with a newly opened tab, there's no need to access the shadow-root. Instead, saving a PDF file can be made much simpler by utilizing Chrome driver options. By passing preferences to chromedriver, your PDF file will automatically be saved to a designated directory during print actions.

import json
import sys
import time

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

from selenium.webdriver.common.action_chains import ActionChains

try:
    print_settings = {
        "recentDestinations": [{
            "id": "Save as PDF",
            "origin": "local",
            "account": "",
        }],
        "selectedDestinationId": "Save as PDF",
        "version": 2,
        "isHeaderFooterEnabled": False,
        "isLandscapeEnabled": True
    }

    prefs = {'printing.print_preview_sticky_settings.appState': json.dumps(print_settings),
             "download.prompt_for_download": False,
             "profile.default_content_setting_values.automatic_downloads": 1,
             "download.directory_upgrade": True,
             "savefile.default_directory": "/Users/a1/PycharmProjects/PythonProject", #this is path to dir where you want to save the file
             "safebrowsing.enabled": True}

    options = webdriver.ChromeOptions()
    options.add_experimental_option('prefs', prefs)
    options.add_argument('--kiosk-printing')
    service = Service()
    driver = webdriver.Chrome(options)

    driver.maximize_window()
    actions = ActionChains(driver)
    wait = WebDriverWait(driver, 20)

    driver.get("https://web.bcpa.net/BcpaClient/#/Record-Search")

    text_input = wait.until(EC.visibility_of_element_located((By.XPATH, '//input[@class="form-control"]'))).send_keys(
        "2216 NW 6 PL FORT LAUDERDALE, FL 33311")
    search_button = driver.find_element(By.XPATH,
                                        '//span[@class="input-group-addon"]/span[@class="glyphicon glyphicon-search"]').click()

    printer_click = wait.until(EC.visibility_of_element_located((By.XPATH, '//div[@class="col-sm-1  btn-printrecinfo"]'))).click()
    time.sleep(5)
except Exception as e:
    print(e)
    sys.exit(1)

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

Constant price updates through an API loop

I am transferring data from a spreadsheet to a dataframe that includes product details, which I need to update prices in an e-commerce via a put request through an API. However, I am facing the challenge of creating a loop to properly iterate through the e ...

What are some methods for increasing the speed of debugging in Python + Django + PyCharm on a Windows operating system

Enhancing Django Debugging with PyCharm. Every time I try to debug something, the process runs terribly slow. The start-up time for Django is excessively long. Let me clarify - I am a big fan of PyCharm for its comprehensive debugging features...and Pyt ...

I'm looking for a sample application that showcases the best MVC practices in a Python app running on Google App Engine. Can you

Despite my familiarity with the Python language, I have yet to create a substantial web application consisting of numerous classes. As I consider embarking on this endeavor, I find myself pondering the most effective way to organize the code similar to wh ...

Managing process control in Supervisord - halting an individual subprocess

We have implemented Supervisord to manage workers initiated by our Gearman job server. When it comes to removing a job from the queue, we currently execute the following commands: $ sudo killall supervisord This kills all Supervisord subprocesses to prev ...

Failed to retrieve the article outcome from a search on Google

Hi, I'm currently trying to use BeautifulSoup to scrape the content of a link and extract article dates from spans with the class "f". import requests import json from bs4 import BeautifulSoup headers = {'User-Agent': 'Mozilla/5.0 (Mac ...

What is the best way to execute a single GET request concurrently for a set number of times?

Is there a way to send a GET request multiple times in parallel, for example 100 times using JMeter or Python? I attempted to use bzm parallel executor for this task but it did not work as expected. ...

The preference for the download directory in Selenium's Firefox profile is failing to be applied

Here is the code snippet I am working with: profile = webdriver.FirefoxProfile() profile.set_preference("browser.download.folderList", 2) profile.set_preference("browser.download.manager.showWhenStarting", False) profile.set ...

Creating a JSON schema in JSL python library that defines a mapping of strings to strings

Imagine a scenario where my JSON document contains various properties, one of which holds a collection of HTTP headers represented as a simple map of key-value pairs. { "property": "value", "headers": { "Content-Type": "text/css", "Last-Modifi ...

Designate a specific Python version to be used when executing commands in the Windows command line

https://i.stack.imgur.com/cqGZM.png After installing both Python2 and Python3 on my computer, I noticed that when running a Python script, my computer defaults to using Python2. I attempted to uninstall Python2, but then my computer was unable to run any ...

Struggling to find element using Java in Selenium WebDriver - need assistance

I am facing a challenge where I need to select and delete a dynamically generated row in a table using Java in Selenium WebDriver. The issue is that the ID of the row changes every time a new row is added, so I'm unsure how to access it programmatical ...

Execute a Selenium WebDriver test on a server running the Linux operating system

I am attempting to execute a test using selenium webdriver on a Linux server with Chrome and no display. Below is my Java code: System.setProperty("webdriver.chrome.driver","/home/exploit/Bureau/chromedriver"); WebDriver driver = new ChromeDriver(); dri ...

Can value_counts() be applied to two columns simultaneously?

I am working with a pandas dataframe that contains texts, each of which can be categorized into multiple categories and belong to one genre. The categories are represented in the dataframe using one-hot encoding. For example: df = pd.DataFrame({'text ...

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 ...

Executing Browser in the Background with Protractor/Selenium

When using protractor for testing, I often find that there is no need to actually view the browser in action. The tests can be time-consuming, and what truly matters is the final outcome. Is there a way to run these tests in the background or hide the br ...

Can I consolidate identical terms into a single column in a pandas dataframe?

Within this extensive pandas dataframe, there are several terms: type name exp ------------------- feline tiger True feline cat False rodent rabbit True canine dog False feline puma True feline bobcat False Are there ways to consolid ...

Ensuring Proper Alignment in PyQT5 Layouts

In my PyQt5 GUI, I have the following code: import sys from PyQt5.QtWidgets import QApplication, QWidget, QCalendarWidget, QMainWindow, QGridLayout, QLayout, QTableWidget, QHeaderView, QLabel, QHBoxLayout, QVBoxLayout, QLineEdit, QComboBox from PyQt5.QtCo ...

Collecting Payment Information using Python's Selenium Automation

This question is a continuation of my previous inquiry Click here for details on dynamic elements I am currently working with Python and Selenium to navigate to a webpage and input Credit Card information. Unlike my previous question where the element ha ...

The Java Selenium scrolling code fails to function in a newly opened tab

Currently working on Java Selenium and encountering an issue with the scroll code I added not opening in a new tab. How can I ensure that the codes I wrote will function properly in the new tab? My goal is to have the page scroll down in the new tab and t ...

Discovering text using Selenium is easier than you might think

Hello everyone, I am seeking assistance on how to locate a specific text using Selenium. For instance, the text I want to find is: Test I currently have this code to retrieve the text: wait.until(EC.element_to_be_clickable((By.XPATH, '//*[text()=&quo ...

Transform the HTTP text response into a pandas dataframe

Is there a way to utilize Python Pandas' pre-built or in-built parser to convert the text below into a pandas dataframe? Although I can create a custom parsing function, I am interested in finding out if there is a faster and ready-made solution avail ...