无法访问模态元素以使用硒下载PDF

发布于 2025-01-27 08:51:53 字数 355 浏览 2 评论 0原文

我正在网上抓取一个网站,该网站打开了要在模态视图中访问的PDF。我想下载/访问此PDF。

它可以在模式下以PDF查看器的形式打开,我无法通过XPath访问下载按钮或任何其他元素。 通过XPATH访问任何元素时,它将返回一个空列表。

需要访问的模态对话框

模态视图的检查部分

I am web scraping a website that opens up the pdf I want to access in a modal view. I want to download/access this pdf.

It opens up as a pdf viewer in a modal and I can't access the download button or any other element through the Xpath.
On accessing any element through Xpath, it returns an empty list .

The modal dialog box that needs to be accessed

The inspect section of the modal view

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

御弟哥哥 2025-02-03 08:51:53

我注意到您关于模态视图的检查部分的屏幕截图。

该元素具有属性Original_url是PDF文件的原始URL。

并引用这个答案,您可以尝试通过配置webdrover.chromeoptions.chromeoptions() /代码>。

因此,在您的情况下,可能是这样:

from selenium import webdriver

profile = {
    'download.prompt_for_download': False,
    'download.default_directory': '/path/to/download/the/pdf',
    'download.directory_upgrade': True,
    'plugins.always_open_pdf_externally': True,
}
options = webdriver.ChromeOptions()
options.add_experimental_option('prefs', profile)
driver = webdriver.Chrome(options=options)
driver.get('your_url')

# Your code to handle the Captcha

# When you open the modal dialog box
pdf_url = driver.find_element("id", "plugin").get_attribute("original_url")
driver.get(pdf_url)

# Chrome will download the PDF automatically

I noticed that your screenshot about The inspect section of the modal view .

The element has an attribute original_url which is the original url of the pdf file.

And reference to this answer, you can try to directly download the pdf file by configurating webdriver.ChromeOptions().

So in your case it can be like this:

from selenium import webdriver

profile = {
    'download.prompt_for_download': False,
    'download.default_directory': '/path/to/download/the/pdf',
    'download.directory_upgrade': True,
    'plugins.always_open_pdf_externally': True,
}
options = webdriver.ChromeOptions()
options.add_experimental_option('prefs', profile)
driver = webdriver.Chrome(options=options)
driver.get('your_url')

# Your code to handle the Captcha

# When you open the modal dialog box
pdf_url = driver.find_element("id", "plugin").get_attribute("original_url")
driver.get(pdf_url)

# Chrome will download the PDF automatically
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文