0

I am trying to validate content of pdf by copying the content to clipboard and transferring to String. Below is my code

ArrayList<String> tabs = new ArrayList<String> (driver.getWindowHandles());
            for (String string : tabs) {
                System.out.println("tab id:: "+string);
            }   

            driver.switchTo().window(tabs.get(1));  //pdf opens in new tab so switching to new tab

            pause(5000);

                String selectAll = Keys.chord(Keys.CONTROL,Keys.chord("a"));
                String copy = Keys.chord(Keys.CONTROL,Keys.chord("c"));
         driver.switchTo().frame(driver.findElement(By.xpath("//html/body//div/div/iframe")));
                driver.findElement(By.xpath("//*[@id='plugin']")).sendKeys(selectAll);  //Select all works on the opened pdf

                pause(3000);

                driver.findElement(By.xpath("//*[@id='plugin']")).sendKeys(copy);

                pause(5000);    
                Clipboard clipboard2 = Toolkit.getDefaultToolkit().getSystemClipboard();
                Transferable contents = clipboard2.getContents(null);
                String x = (String) contents.getTransferData(DataFlavor.stringFlavor);
                System.out.println(x);  //getting worng out put here instead of pdf content

Selecting the content works , but i get some random output like this

tab id:: CDwindow-FC2070E3D2902357E08E5D6720AF9766
tab id:: CDwindow-D06392BD9261B14C083395D57AE805FA
Clipboard clipboard2 = Toolkit.getDefaultToolkit().getSystemClipboard();

Here is the page source for Embedded pdfenter image description here

Not sure what wrong i am doing. Can any one please help. Thank you.

Chomredriver 2.39 Chrome version 66

mk_
  • 164
  • 1
  • 14
  • please check https://stackoverflow.com/questions/3563147/can-selenium-verify-text-inside-a-pdf-loaded-by-the-browser – Vinit Mehta Jun 06 '18 at 14:50

1 Answers1

1

You might be getting wrong output because the control might not be on the same page even after switching to the iframe. You need to do a Click operation on PDF before sending copy command.

     driver.switchTo().frame(driver.findElement(By.xpath("//html/body//div/div/iframe")));

driver.findElement(By.xpath("//*[@id='plugin']")).click();

( or )

WebElement element = driver.findElement(By.xpath("//*[@id='plugin']"));

(JavascriptExecutor) driver.executeScript("element.click()");

element.sendKeys(selectAll);

element.sendKeys(copy);

  • Thank you for your input, i am now trying different approach to solve this problem. We are now directly reading the pdf encoded source driver.findElement(By.xpath("//*[@id='plugin']")).getAttribute("src"); and converting the pdf source to string and verifying. – mk_ Jun 08 '18 at 10:44