Ryusei Nakamura
Ryusei Nakamura

Reputation: 71

how to acess pdf link using selenium webdriver

The below code functions as follows:

  1. ask user to enter name of book.
  2. It opens Firefox browser.
  3. loads google.com
  4. enter book name using sendkeys()
  5. clicks on search button
  6. it shows all link

below is the code.

import java.util.*;
    import java.util.concurrent.TimeUnit;    
    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.firefox.FirefoxDriver;
    public class downloaddemo {
        String text ;
        String text1 ;
        WebDriver driver;
        void getTextU()
        {
            Scanner s = new Scanner(System.in);
            System.out.println("Enter Text");
            text = s.nextLine();
            text1 = text + " pdf " ; 
            System.out.println(" Searching for " + text1  +  " .......... ");
        }

        public void invokeBrowser()
        {
            System.setProperty("webdriver.gecko.driver","C:\\geckodriver-v0.19.0-win64\\geckodriver.exe");
            driver = new FirefoxDriver();
            driver.manage().deleteAllCookies();
            driver.manage().window().maximize();
            driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
            driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS );
            driver.get("https://www.google.com");
            search();

        }
        public void search()
        {
            driver.findElement(By.xpath("//input[@id='lst-ib' and @class='gsfi']")).click(); 

            System.out.println(text1);
            driver.findElement(By.id("lst-ib")).sendKeys(text1);

            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            driver.findElement(By.xpath("//input[@value='Google Search' or  @aria-label='Google Search']")).click();
            //driver.findElement(By.xpath("//span[@class='_ogd b w xsm'] and //a[@href='']")).click();
            /*List<WebElement> list = driver.findElements(By.xpath("//span[@class='_ogd b w xsm']//a[@href]"));
            for (WebElement e : list) {
                String link = e.getAttribute("href");
                System.out.println(e.getTagName() + "=" + link + " , " + e.getText());
            }*/

        }

        public static void main(String[] args) {
            // TODO Auto-generated method stub
            downloaddemo d  = new downloaddemo();
            d.getTextU();
            d.invokeBrowser();
        }

    }

it shows output as follows:

The problem is how can I load any one of the link which is pdf, the problem is that each anchor tag has different href value .
or
how can I download all pdf .

Upvotes: 0

Views: 1069

Answers (1)

Zakaria Shahed
Zakaria Shahed

Reputation: 2697

Using this code I take all the pdf link

    public class testing_solution
    {
           public static WebDriver driver;

            public static void main(String[] args) throws InterruptedException {
                    driver =new ChromeDriver();
                    driver.get("https://www.google.com/search?q=selenium+pdf&rlz=1C1CHZL_enBD739BD739&oq=selenium+pdf&aqs=chrome..69i57j69i60l2j0l3.3815j0j7&sourceid=chrome&ie=UTF-8");
                    List<WebElement> list = driver.findElements(By.xpath(".//*[@class='f kv _SWb']/cite"));

             for (WebElement e : list) {
                 if(e.getText().endsWith(".pdf")){

                     System.out.println(e.getText());
                 }

        }}

First I take the link of the link then check if the link is pdf then I print all the link.

And I get Following Output

enter image description here

Hope it will help you....

Upvotes: 1

Related Questions