How to click the save or print button inside Google Chrome print window using selenium when the HTML page has shadow-root?

63 views Asked by At

enter image description hereI want to scrape data using selenium and python. My chrome version is 123.0.6312.87 and web-driver is compatible with this chrome version. From this following page "https://web.bcpa.net/BcpaClient/#/Record-Search" , I want to scrape data. In this page when I am giving the address "2216 NW 6 PL FORT LAUDERDALE" using selenium it will give the details of the property. Now there is a Print button when I am clicking it using selenium it is redirecting me to a ne page "https://web.bcpa.net/BcpaClient/recinfoprint.html". Now in this HTML page there is dropdown option under class "md-select" from which I want to select "Save As PDF" which has the value "Save as PDF/local/". But this HTML page has shadow root. So selenium is not able to locate the position of class "md-select". Also I want to click the button Save in that html page bellow within the class "action-button" but the presence of shadow root is creating huge problem. I have tried to extract information from the "print-preview-app" which is located before shadow-root. But it is not working also.

Code:

import datetime as dt
import os
import time 
from datetime import datetime
import pandas as pd
import numpy as np
from datetime import timedelta
import sys
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.edge.options import Options
import os.path 
import json
import ssl
import io
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.support.ui import Select
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver import ActionChains
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains

try:
    # # Path to your Chrome WebDriver executable

    
    webdriver_path = "D:/Grass_Image_Classification/chromedriver-win64/chromedriver.exe"

    # Create a Chrome WebDriver instance
    service = Service(webdriver_path)
    driver = webdriver.Chrome(service=service)
    print('Successfully received the chromedriver path')

    driver.maximize_window()
    actions = ActionChains(driver)

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


    driver.implicitly_wait(10)
    text_input = driver.find_element(By.XPATH, '//input[@class="form-control"]').send_keys("2216 NW 6 PL FORT LAUDERDALE, FL 33311")
    driver.implicitly_wait(10)
    search_button = driver.find_element(By.XPATH, '//span[@class="input-group-addon"]/span[@class="glyphicon glyphicon-search"]').click()
    
    driver.implicitly_wait(10)
    printer_click = driver.find_element(By.XPATH, '//div[@class="col-sm-1  btn-printrecinfo"]').click()
    driver.implicitly_wait(15)
  
    # Switch to the new tab

    handles = driver.window_handles
   

    print(handles)
    print(handles[-1])
    driver.switch_to.window(handles[-1])
    print(driver.current_url)
    
    # shadow_root =  driver.find_element(By.ID,"sidebar").shadow_root
    shadow_root =  driver.find_element(By.CSS_SELECTOR,"body > print-preview-app").shadow_root
    # shadow_root =  driver.find_element(By.XPATH,"/html/body/print-preview-app").shadow_root
    # shadow_root =  driver.find_element(By.XPATH,'//*[@id="sidebar"]').shadow_root
    shadow_text = shadow_root.find_element(By.CSS_SELECTOR,"print-preview-settings-section > div > select").text
    print(shadow_text)
    time.sleep(10)
    
except Exception as e:
    print(e)
    sys.exit(1)

I want to select "Save As PDF" which has the value "Save as PDF/local/" within the class "md-select" and then I want to click the button Save in that html page bellow within the class "action-button".

Code stop working after applying webdriver shadow root method.

1

There are 1 answers

0
Yaroslavm On

You don't need to access in shadow-root on newly opened tab. Saving pdf file is much easier, using Chrome driver options. You can just pass preferences to chromedriver that will automatically save your pdf file to directory on 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)