[英]How to get the only PDF url from web page?
I am trying to get some DOM elements using Selenium and I am doing all of this using Java but I am getting this Error when trying it out:我正在尝试使用 Selenium 获取一些 DOM 元素,我正在使用 Java 完成所有这些操作,但在尝试时出现此错误:
Exception in thread "main" org.openqa.selenium.StaleElementReferenceException: stale element reference: element is not attached to the page document
I am still a newbie in all this but the code I am using to retrieve the DOM element is:我仍然是这一切的新手,但我用来检索 DOM 元素的代码是:
driver.get("https://www.qp.alberta.ca/570.cfm?frm_isbn=9780779808571&search_by=link");
String pagePdfUrl = driver.findElement(By.xpath("//img[@alt='View PDF']//..//parent::a")).getAttribute("href");
I believe the error is that it cannot find the XPath given although this xpath exists.我相信错误是它找不到给定的 XPath 尽管这个 xpath 存在。 Any help would be appreciated.
任何帮助,将不胜感激。
Thank you.谢谢你。
There is a href
attribute is having pdf URL but that URL
opens the pdf within webpage.有一个
href
属性具有 pdf URL 但URL
在网页中打开 pdf。
So I extracted the pdf URL
from href
attribute and fetched the pdf name from that then concatenated with https://www.qp.alberta.ca/documents/Acts/
URL.因此,我从
href
属性中提取了 pdf URL
,并从中提取了 pdf 名称,然后将其与https://www.qp.alberta.ca/documents/Acts/
连接起来。
You can write the code like below to get the pdf URL.您可以编写如下代码来获取 pdf URL。
PDF
URL :PDF
URL 的代码: driver = new ChromeDriver();
/*I hard coded below URL. You need parameterize based on your requirement.*/
driver.get("https://www.qp.alberta.ca/570.cfm?frm_isbn=9780779808571&search_by=link");
String pagePdfUrl = driver.findElement(By.xpath("//img[@alt='View PDF']//..//parent::a")).getAttribute("href");
System.out.println("Page PDF URL: " + pagePdfUrl);
String pdfName = StringUtils.substringBetween(pagePdfUrl, "page=", ".cfm&");
driver.get("https://www.qp.alberta.ca/documents/Acts/" + pdfName + ".pdf");
PDF
:PDF
: Required ChromOptions:必需的 ChromOptions:
ChromeOptions options = new ChromeOptions();
HashMap<String, Object> chromeOptionsMap = new HashMap<String, Object>();
chromeOptionsMap.put("plugins.plugins_disabled", new String[] { "Chrome PDF Viewer" });
chromeOptionsMap.put("plugins.always_open_pdf_externally", true);
chromeOptionsMap.put("download.default_directory", "C:\\Users\\Downloads\\test\\");
options.setExperimentalOption("prefs", chromeOptionsMap);
options.addArguments("--headless");
Accessing PDF:接入PDF:
driver = new ChromeDriver(options);
driver.get("https://www.qp.alberta.ca/570.cfm?frm_isbn=9780779808571&search_by=link");
String pagePdfUrl = driver.findElement(By.xpath("//img[@alt='View PDF']//..//parent::a")).getAttribute("href");
System.out.println("Page PDF URL: " + pagePdfUrl);
String pdfName = StringUtils.substringBetween(pagePdfUrl, "page=", ".cfm&");
System.out.println("Only PDF URL: "+"https://www.qp.alberta.ca/documents/Acts/" + pdfName + ".pdf");
driver.get("https://www.qp.alberta.ca/documents/Acts/" + pdfName + ".pdf");
OutPut : OutPut :
Page PDF URL: https://www.qp.alberta.ca/1266.cfm?page=2017ch18_unpr.cfm&leg_type=Acts&isbncln=9780779808571
Only PDF URL: https://www.qp.alberta.ca/documents/Acts/2017ch18_unpr.pdf
Import for StringUtils
:为
StringUtils
导入:
import org.apache.commons.lang3.StringUtils;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.