Download PDF file opening as URL in different tab in selenium
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
add a comment |
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
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
add a comment |
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
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
selenium pdf selenium-webdriver
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
add a comment |
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
add a comment |
1 Answer
1
active
oldest
votes
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.
New contributor
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
New contributor
add a comment |
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.
New contributor
add a comment |
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.
New contributor
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.
New contributor
New contributor
answered 20 hours ago
Arindam Sarkar
1
1
New contributor
New contributor
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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