Py学习  »  Python

无法单击隐藏在#shadow root(open)中的pdf下载按钮,在python中使用JavaScript定位器使用Selenium

Adam • 4 年前 • 717 次点击  

我想写一个webscraper来自动下载文档。按钮隐藏在多个阴影根中。我怀疑问题在于我的webdriver当前加载的页面。我很确定我需要使用JavaScript来浏览 shadow-roots ,但我不确定。

我一直在犯这样的错误: JavascriptException: Message: javascript error: Cannot read properties of null (reading 'shadowRoot')

以下是我的完整代码:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
options = webdriver.ChromeOptions() 
options.add_argument("start-maximized")
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('useAutomationExtension', False)
from bs4 import BeautifulSoup
import requests
import time
import pandas as pd
import numpy as np



driver = webdriver.Chrome(options=options)
url = "http://oris.co.palm-beach.fl.us/or_web1/or_sch_1.asp"
driver.get(url)
driver.find_element_by_xpath('//*[@id="window1"]/table/tbody/tr/td/table/tbody/tr[1]/td[3]/a').click()

doc_type = driver.find_element_by_xpath('//*[@id="window2"]/table/tbody/tr/td/table/tbody/tr[3]/td/table/tbody/tr[1]/td/form/font[2]/input')
doc_type.send_keys('D')

consideration_input = driver.find_element_by_name('consideration')
consideration_input.send_keys('5,000,000')

from_date = driver.find_element_by_xpath('//*[@id="window2"]/table/tbody/tr/td/table/tbody/tr[3]/td/table/tbody/tr[1]/td/form/table/tbody/tr/td[1]/font[3]/input')

from_date.clear()
from_date.send_keys('11/04/2021')

to_date = driver.find_element_by_xpath('//*[@id="window2"]/table/tbody/tr/td/table/tbody/tr[3]/td/table/tbody/tr[1]/td/form/table/tbody/tr/td[1]/font[5]/input')

to_date.clear()
to_date.send_keys('11/05/2021')

submit_button = driver.find_element_by_xpath('//*[@id="window2"]/table/tbody/tr/td/table/tbody/tr[3]/td/table/tbody/tr[1]/td/form/input[2]')
submit_button.click()

url_list = driver.find_elements_by_class_name("list_2[href]")
url_list[0].click()

driver.switch_to.frame('FrmDes')

image_button = driver.find_element_by_xpath("/html/body/table[2]/tbody/tr/td[2]/map[4]/area")
image_button.click()

driver.switch_to.default_content()
driver.switch_to.frame('FrmImg')
view_pdf = driver.find_element_by_xpath('/html/body/table[2]/tbody/tr[1]/td[1]/form/input[12]')
view_pdf.click()

try:
    driver.page_source
except Exception as e:
    print(e)

element = driver.execute_script(
    "return document.querySelector('pdf-viewer').shadowRoot.querySelector('viewer-toolbar').shadowRoot.querySelector('viewer-download-controls')"
)
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/129992