Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

238
Views
¿Cómo obtener la única URL de PDF de la página web?

Estoy tratando de obtener algunos elementos DOM usando Selenium y estoy haciendo todo esto usando Java, pero recibo este error cuando lo pruebo:

 Exception in thread "main" org.openqa.selenium.StaleElementReferenceException: stale element reference: element is not attached to the page document

Todavía soy un novato en todo esto, pero el código que estoy usando para recuperar el elemento DOM es:

 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");

Creo que el error es que no puede encontrar el XPath dado aunque este xpath existe. Cualquier ayuda sería apreciada.

Gracias.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

  • Hay un atributo href que tiene una URL de pdf, pero esa URL abre el pdf dentro de la página web.

  • Así que extraje la URL del pdf del atributo href y obtuve el nombre del pdf que luego concatené con https://www.qp.alberta.ca/documents/Acts/ URL.

Puede escribir el código como se muestra a continuación para obtener la URL del pdf.

Código para obtener la URL del PDF :

 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");

Código para descargar PDF :

ChromOptions requeridas:

 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");

Acceso a 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");

Salida :

 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

Importar para StringUtils :

 import org.apache.commons.lang3.StringUtils;
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!