Download PDF file opening as URL in different tab in selenium












0














Scenario:



I have an application, say A: When we log in to A, we have one link say B clicking on which will open a new browser.
In browser B we have one Link that will open a pdf. Once click on that the PDF is opening as URL in the 2nd tab where we had opened the page A.



Problem:



I have tried switching using iteration through window handle but it is not finding that. I have also tried adding below to find all the window handle where you have below locator.
if(driver.findElement(By.xpath("//*[@id="plugin"]")))



but as PDF URL has opened in a 2nd tab, I am not able to get the window handle. I think as per my code below, if I get Window handle then I will use robot class and save the PDF.



Note : I am using xframium framework so, had to define
WebDriver driver = getCustumWebDriver();
I can only use IE/Chrome and no other browsers



Any suggestions on how to solve?



Code:



public String getWindowUrl(String saveDir, SoftAssert softAssert, Element element)
{



boolean success = false;
String newWindowUrl = null;

try {
WebDriver driver = getCustumWebDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
//current window handle
String beforWindowHandle = driver.getWindowHandle();
_wait(2000);
//Click on the element to which the pdf link is opened
_click(element);
waitForPageLoad();
_wait(30000);

Set<String> allWindowhandles = driver.getWindowHandles();
for(String handle1 : allWindowhandles)
{
if(!handle1.equals(beforWindowHandle))
{
driver.switchTo().window(handle1);
_wait(2000);
newWindowUrl = driver.getCurrentUrl();
docName = newWindowUrl.replaceAll("[^0-9]+", "");
docName = saveDir.concat(docName).concat(".pdf");
Robot rb = new Robot();

rb.keyPress(KeyEvent.VK_CONTROL);
rb.keyPress(KeyEvent.VK_S);
_wait(3000);
rb.keyPress(KeyEvent.VK_CONTROL);
rb.keyPress(KeyEvent.VK_C);
rb.keyRelease(KeyEvent.VK_C);
rb.keyRelease(KeyEvent.VK_CONTROL);

_wait(1000);

rb.keyPress(KeyEvent.VK_HOME);
rb.keyRelease(KeyEvent.VK_HOME);
_wait(3000);

StringSelection stringSelection = new StringSelection(saveDir);
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
clipboard.setContents(stringSelection, stringSelection);

rb.keyPress(KeyEvent.VK_CONTROL);
rb.keyPress(KeyEvent.VK_V);
rb.keyRelease(KeyEvent.VK_V);
rb.keyRelease(KeyEvent.VK_CONTROL);
rb.delay(3000);
// _wait(3000);
rb.keyPress(KeyEvent.VK_ENTER);
rb.keyRelease(KeyEvent.VK_ENTER);
_wait(2000);
}

}
success = true;
driver.close();


}



Scenario 2 :
I can force the application to open a new browser window just for PDF but I am not able to get to that specific window where new PDF URL will open.
I will try the below and give my observation:



**for(String handle1 : allWindowhandles)
{
// change focus to new tab
driver.switchTo().window(handle1);
if(!(driver.findElement(By.id("Element that will be present on browser window 1 - A ")).isDisplayed()||
driver.findElement(By.id("Element that will be present on browser window 2 - B ")).isDisplayed()))
{
rb.keyPress(KeyEvent.VK_CONTROL);
rb.keyPress(KeyEvent.VK_S);
_wait(3000);
rb.keyPress(KeyEvent.VK_CONTROL);
rb.keyPress(KeyEvent.VK_C);
rb.keyRelease(KeyEvent.VK_C);
rb.keyRelease(KeyEvent.VK_CONTROL);

_wait(1000);

rb.keyPress(KeyEvent.VK_HOME);
rb.keyRelease(KeyEvent.VK_HOME);
_wait(3000);

StringSelection stringSelection = new StringSelection(saveDir);
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
clipboard.setContents(stringSelection, stringSelection);

rb.keyPress(KeyEvent.VK_CONTROL);
rb.keyPress(KeyEvent.VK_V);
rb.keyRelease(KeyEvent.VK_V);
rb.keyRelease(KeyEvent.VK_CONTROL);
rb.delay(3000);
// _wait(3000);
rb.keyPress(KeyEvent.VK_ENTER);
}**









share|improve this question




















  • 1




    stackoverflow.com/questions/51217622/…
    – Infern0
    20 hours ago










  • New browser or new tab? If it is new tab i can help, also can you access B without going to A?
    – Neagu V
    20 hours ago
















0














Scenario:



I have an application, say A: When we log in to A, we have one link say B clicking on which will open a new browser.
In browser B we have one Link that will open a pdf. Once click on that the PDF is opening as URL in the 2nd tab where we had opened the page A.



Problem:



I have tried switching using iteration through window handle but it is not finding that. I have also tried adding below to find all the window handle where you have below locator.
if(driver.findElement(By.xpath("//*[@id="plugin"]")))



but as PDF URL has opened in a 2nd tab, I am not able to get the window handle. I think as per my code below, if I get Window handle then I will use robot class and save the PDF.



Note : I am using xframium framework so, had to define
WebDriver driver = getCustumWebDriver();
I can only use IE/Chrome and no other browsers



Any suggestions on how to solve?



Code:



public String getWindowUrl(String saveDir, SoftAssert softAssert, Element element)
{



boolean success = false;
String newWindowUrl = null;

try {
WebDriver driver = getCustumWebDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
//current window handle
String beforWindowHandle = driver.getWindowHandle();
_wait(2000);
//Click on the element to which the pdf link is opened
_click(element);
waitForPageLoad();
_wait(30000);

Set<String> allWindowhandles = driver.getWindowHandles();
for(String handle1 : allWindowhandles)
{
if(!handle1.equals(beforWindowHandle))
{
driver.switchTo().window(handle1);
_wait(2000);
newWindowUrl = driver.getCurrentUrl();
docName = newWindowUrl.replaceAll("[^0-9]+", "");
docName = saveDir.concat(docName).concat(".pdf");
Robot rb = new Robot();

rb.keyPress(KeyEvent.VK_CONTROL);
rb.keyPress(KeyEvent.VK_S);
_wait(3000);
rb.keyPress(KeyEvent.VK_CONTROL);
rb.keyPress(KeyEvent.VK_C);
rb.keyRelease(KeyEvent.VK_C);
rb.keyRelease(KeyEvent.VK_CONTROL);

_wait(1000);

rb.keyPress(KeyEvent.VK_HOME);
rb.keyRelease(KeyEvent.VK_HOME);
_wait(3000);

StringSelection stringSelection = new StringSelection(saveDir);
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
clipboard.setContents(stringSelection, stringSelection);

rb.keyPress(KeyEvent.VK_CONTROL);
rb.keyPress(KeyEvent.VK_V);
rb.keyRelease(KeyEvent.VK_V);
rb.keyRelease(KeyEvent.VK_CONTROL);
rb.delay(3000);
// _wait(3000);
rb.keyPress(KeyEvent.VK_ENTER);
rb.keyRelease(KeyEvent.VK_ENTER);
_wait(2000);
}

}
success = true;
driver.close();


}



Scenario 2 :
I can force the application to open a new browser window just for PDF but I am not able to get to that specific window where new PDF URL will open.
I will try the below and give my observation:



**for(String handle1 : allWindowhandles)
{
// change focus to new tab
driver.switchTo().window(handle1);
if(!(driver.findElement(By.id("Element that will be present on browser window 1 - A ")).isDisplayed()||
driver.findElement(By.id("Element that will be present on browser window 2 - B ")).isDisplayed()))
{
rb.keyPress(KeyEvent.VK_CONTROL);
rb.keyPress(KeyEvent.VK_S);
_wait(3000);
rb.keyPress(KeyEvent.VK_CONTROL);
rb.keyPress(KeyEvent.VK_C);
rb.keyRelease(KeyEvent.VK_C);
rb.keyRelease(KeyEvent.VK_CONTROL);

_wait(1000);

rb.keyPress(KeyEvent.VK_HOME);
rb.keyRelease(KeyEvent.VK_HOME);
_wait(3000);

StringSelection stringSelection = new StringSelection(saveDir);
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
clipboard.setContents(stringSelection, stringSelection);

rb.keyPress(KeyEvent.VK_CONTROL);
rb.keyPress(KeyEvent.VK_V);
rb.keyRelease(KeyEvent.VK_V);
rb.keyRelease(KeyEvent.VK_CONTROL);
rb.delay(3000);
// _wait(3000);
rb.keyPress(KeyEvent.VK_ENTER);
}**









share|improve this question




















  • 1




    stackoverflow.com/questions/51217622/…
    – Infern0
    20 hours ago










  • New browser or new tab? If it is new tab i can help, also can you access B without going to A?
    – Neagu V
    20 hours ago














0












0








0







Scenario:



I have an application, say A: When we log in to A, we have one link say B clicking on which will open a new browser.
In browser B we have one Link that will open a pdf. Once click on that the PDF is opening as URL in the 2nd tab where we had opened the page A.



Problem:



I have tried switching using iteration through window handle but it is not finding that. I have also tried adding below to find all the window handle where you have below locator.
if(driver.findElement(By.xpath("//*[@id="plugin"]")))



but as PDF URL has opened in a 2nd tab, I am not able to get the window handle. I think as per my code below, if I get Window handle then I will use robot class and save the PDF.



Note : I am using xframium framework so, had to define
WebDriver driver = getCustumWebDriver();
I can only use IE/Chrome and no other browsers



Any suggestions on how to solve?



Code:



public String getWindowUrl(String saveDir, SoftAssert softAssert, Element element)
{



boolean success = false;
String newWindowUrl = null;

try {
WebDriver driver = getCustumWebDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
//current window handle
String beforWindowHandle = driver.getWindowHandle();
_wait(2000);
//Click on the element to which the pdf link is opened
_click(element);
waitForPageLoad();
_wait(30000);

Set<String> allWindowhandles = driver.getWindowHandles();
for(String handle1 : allWindowhandles)
{
if(!handle1.equals(beforWindowHandle))
{
driver.switchTo().window(handle1);
_wait(2000);
newWindowUrl = driver.getCurrentUrl();
docName = newWindowUrl.replaceAll("[^0-9]+", "");
docName = saveDir.concat(docName).concat(".pdf");
Robot rb = new Robot();

rb.keyPress(KeyEvent.VK_CONTROL);
rb.keyPress(KeyEvent.VK_S);
_wait(3000);
rb.keyPress(KeyEvent.VK_CONTROL);
rb.keyPress(KeyEvent.VK_C);
rb.keyRelease(KeyEvent.VK_C);
rb.keyRelease(KeyEvent.VK_CONTROL);

_wait(1000);

rb.keyPress(KeyEvent.VK_HOME);
rb.keyRelease(KeyEvent.VK_HOME);
_wait(3000);

StringSelection stringSelection = new StringSelection(saveDir);
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
clipboard.setContents(stringSelection, stringSelection);

rb.keyPress(KeyEvent.VK_CONTROL);
rb.keyPress(KeyEvent.VK_V);
rb.keyRelease(KeyEvent.VK_V);
rb.keyRelease(KeyEvent.VK_CONTROL);
rb.delay(3000);
// _wait(3000);
rb.keyPress(KeyEvent.VK_ENTER);
rb.keyRelease(KeyEvent.VK_ENTER);
_wait(2000);
}

}
success = true;
driver.close();


}



Scenario 2 :
I can force the application to open a new browser window just for PDF but I am not able to get to that specific window where new PDF URL will open.
I will try the below and give my observation:



**for(String handle1 : allWindowhandles)
{
// change focus to new tab
driver.switchTo().window(handle1);
if(!(driver.findElement(By.id("Element that will be present on browser window 1 - A ")).isDisplayed()||
driver.findElement(By.id("Element that will be present on browser window 2 - B ")).isDisplayed()))
{
rb.keyPress(KeyEvent.VK_CONTROL);
rb.keyPress(KeyEvent.VK_S);
_wait(3000);
rb.keyPress(KeyEvent.VK_CONTROL);
rb.keyPress(KeyEvent.VK_C);
rb.keyRelease(KeyEvent.VK_C);
rb.keyRelease(KeyEvent.VK_CONTROL);

_wait(1000);

rb.keyPress(KeyEvent.VK_HOME);
rb.keyRelease(KeyEvent.VK_HOME);
_wait(3000);

StringSelection stringSelection = new StringSelection(saveDir);
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
clipboard.setContents(stringSelection, stringSelection);

rb.keyPress(KeyEvent.VK_CONTROL);
rb.keyPress(KeyEvent.VK_V);
rb.keyRelease(KeyEvent.VK_V);
rb.keyRelease(KeyEvent.VK_CONTROL);
rb.delay(3000);
// _wait(3000);
rb.keyPress(KeyEvent.VK_ENTER);
}**









share|improve this question















Scenario:



I have an application, say A: When we log in to A, we have one link say B clicking on which will open a new browser.
In browser B we have one Link that will open a pdf. Once click on that the PDF is opening as URL in the 2nd tab where we had opened the page A.



Problem:



I have tried switching using iteration through window handle but it is not finding that. I have also tried adding below to find all the window handle where you have below locator.
if(driver.findElement(By.xpath("//*[@id="plugin"]")))



but as PDF URL has opened in a 2nd tab, I am not able to get the window handle. I think as per my code below, if I get Window handle then I will use robot class and save the PDF.



Note : I am using xframium framework so, had to define
WebDriver driver = getCustumWebDriver();
I can only use IE/Chrome and no other browsers



Any suggestions on how to solve?



Code:



public String getWindowUrl(String saveDir, SoftAssert softAssert, Element element)
{



boolean success = false;
String newWindowUrl = null;

try {
WebDriver driver = getCustumWebDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
//current window handle
String beforWindowHandle = driver.getWindowHandle();
_wait(2000);
//Click on the element to which the pdf link is opened
_click(element);
waitForPageLoad();
_wait(30000);

Set<String> allWindowhandles = driver.getWindowHandles();
for(String handle1 : allWindowhandles)
{
if(!handle1.equals(beforWindowHandle))
{
driver.switchTo().window(handle1);
_wait(2000);
newWindowUrl = driver.getCurrentUrl();
docName = newWindowUrl.replaceAll("[^0-9]+", "");
docName = saveDir.concat(docName).concat(".pdf");
Robot rb = new Robot();

rb.keyPress(KeyEvent.VK_CONTROL);
rb.keyPress(KeyEvent.VK_S);
_wait(3000);
rb.keyPress(KeyEvent.VK_CONTROL);
rb.keyPress(KeyEvent.VK_C);
rb.keyRelease(KeyEvent.VK_C);
rb.keyRelease(KeyEvent.VK_CONTROL);

_wait(1000);

rb.keyPress(KeyEvent.VK_HOME);
rb.keyRelease(KeyEvent.VK_HOME);
_wait(3000);

StringSelection stringSelection = new StringSelection(saveDir);
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
clipboard.setContents(stringSelection, stringSelection);

rb.keyPress(KeyEvent.VK_CONTROL);
rb.keyPress(KeyEvent.VK_V);
rb.keyRelease(KeyEvent.VK_V);
rb.keyRelease(KeyEvent.VK_CONTROL);
rb.delay(3000);
// _wait(3000);
rb.keyPress(KeyEvent.VK_ENTER);
rb.keyRelease(KeyEvent.VK_ENTER);
_wait(2000);
}

}
success = true;
driver.close();


}



Scenario 2 :
I can force the application to open a new browser window just for PDF but I am not able to get to that specific window where new PDF URL will open.
I will try the below and give my observation:



**for(String handle1 : allWindowhandles)
{
// change focus to new tab
driver.switchTo().window(handle1);
if(!(driver.findElement(By.id("Element that will be present on browser window 1 - A ")).isDisplayed()||
driver.findElement(By.id("Element that will be present on browser window 2 - B ")).isDisplayed()))
{
rb.keyPress(KeyEvent.VK_CONTROL);
rb.keyPress(KeyEvent.VK_S);
_wait(3000);
rb.keyPress(KeyEvent.VK_CONTROL);
rb.keyPress(KeyEvent.VK_C);
rb.keyRelease(KeyEvent.VK_C);
rb.keyRelease(KeyEvent.VK_CONTROL);

_wait(1000);

rb.keyPress(KeyEvent.VK_HOME);
rb.keyRelease(KeyEvent.VK_HOME);
_wait(3000);

StringSelection stringSelection = new StringSelection(saveDir);
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
clipboard.setContents(stringSelection, stringSelection);

rb.keyPress(KeyEvent.VK_CONTROL);
rb.keyPress(KeyEvent.VK_V);
rb.keyRelease(KeyEvent.VK_V);
rb.keyRelease(KeyEvent.VK_CONTROL);
rb.delay(3000);
// _wait(3000);
rb.keyPress(KeyEvent.VK_ENTER);
}**






selenium pdf selenium-webdriver






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 20 hours ago

























asked 21 hours ago









Ken Beasly

33




33








  • 1




    stackoverflow.com/questions/51217622/…
    – Infern0
    20 hours ago










  • New browser or new tab? If it is new tab i can help, also can you access B without going to A?
    – Neagu V
    20 hours ago














  • 1




    stackoverflow.com/questions/51217622/…
    – Infern0
    20 hours ago










  • New browser or new tab? If it is new tab i can help, also can you access B without going to A?
    – Neagu V
    20 hours ago








1




1




stackoverflow.com/questions/51217622/…
– Infern0
20 hours ago




stackoverflow.com/questions/51217622/…
– Infern0
20 hours ago












New browser or new tab? If it is new tab i can help, also can you access B without going to A?
– Neagu V
20 hours ago




New browser or new tab? If it is new tab i can help, also can you access B without going to A?
– Neagu V
20 hours ago












1 Answer
1






active

oldest

votes


















0














As per my understanding,Developer of that application that you are referring has configured the pdf launching in new tab.so we cannot open it in same tab because it has already configured in application label.






share|improve this answer








New contributor




Arindam Sarkar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.


















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53942820%2fdownload-pdf-file-opening-as-url-in-different-tab-in-selenium%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    As per my understanding,Developer of that application that you are referring has configured the pdf launching in new tab.so we cannot open it in same tab because it has already configured in application label.






    share|improve this answer








    New contributor




    Arindam Sarkar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.























      0














      As per my understanding,Developer of that application that you are referring has configured the pdf launching in new tab.so we cannot open it in same tab because it has already configured in application label.






      share|improve this answer








      New contributor




      Arindam Sarkar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





















        0












        0








        0






        As per my understanding,Developer of that application that you are referring has configured the pdf launching in new tab.so we cannot open it in same tab because it has already configured in application label.






        share|improve this answer








        New contributor




        Arindam Sarkar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.









        As per my understanding,Developer of that application that you are referring has configured the pdf launching in new tab.so we cannot open it in same tab because it has already configured in application label.







        share|improve this answer








        New contributor




        Arindam Sarkar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.









        share|improve this answer



        share|improve this answer






        New contributor




        Arindam Sarkar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.









        answered 20 hours ago









        Arindam Sarkar

        1




        1




        New contributor




        Arindam Sarkar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.





        New contributor





        Arindam Sarkar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.






        Arindam Sarkar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53942820%2fdownload-pdf-file-opening-as-url-in-different-tab-in-selenium%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Angular Downloading a file using contenturl with Basic Authentication

            Olmecas

            Can't read property showImagePicker of undefined in react native iOS