How to copy new tab url java selenium?
'package TestCases;
public class Learn_TC3 extends SuperTestScript
{
@Test
public void LoginTC1() throws Exception
{
//all the required data
String USRID = ExcelLibrary.readData("Sheet1", 0, 0);
String PSW = ExcelLibrary.readData("Sheet1", 0, 1);
//create page objects
LearnPage Lp = new LearnPage();
Tabswitch Ts = new Tabswitch();
//invoke the methods
Lp.ClickonMaterialsButton();
Thread.sleep(3000);
Lp.ClickonPDF1();//By clicking on pdf. Pdf opens in new tab
String CurrentUrl = driver.getCurrentUrl();// to Fetch new url
ExcelLibrary.writeData("Sheet1", 0, 4, CurrentUrl);//write url to excel sheet?
Ts.switchToPreviousTabAndClose(); //Closing new tab
}
}'
I have tried to copy URL which is opened in new tab but copying old tab urls. How to copy new tab url java selenium?
CodePudding user response:
After clicking on Lp.ClickonPDF1()
that opens a new tab you have to switch Selenium driver to the opened tab in order to perform actions there.
So your code can be something like this:
Lp.ClickonMaterialsButton();
Thread.sleep(3000);
Lp.ClickonPDF1();//By clicking on pdf. Pdf opens in new tab
Thread.sleep(500);
List<String> tabs = new ArrayList<>(driver.getWindowHandles());
driver.switchTo().window(tabs.get(tabs.size()-1));
String CurrentUrl = driver.getCurrentUrl();// to Fetch new url
ExcelLibrary.writeData("Sheet1", 0, 4, CurrentUrl);//write url to excel sheet?
Ts.switchToPreviousTabAndClose(); //Closing new tab
Not sure how can I use the driver
object in your infrastructure, so I used it as regular, with no relation to any page object instance.
CodePudding user response:
Below are methods that I created for a test project (using cucumber-java and selenium-java) that is similar to your approach, where it was required to:
- Click on a link for a pdf document on a webpage, which opened in a new tab
- List the opened tabs
- Move to the new tab
- Get the url for the pdf document and verify (assert) that it was correct
- Close the pdf tab
- Return to the webpage
Feel free to use what you need
@Then("^on \"([^\"]*)\" I can view the Pdf Document of \"([^\"]*)\"$")
public void on_I_can_view_the_Pdf_Document_of(String relativeUrl, String document) throws Throwable {
viewPDFDocument(relativeUrl, document);
}
private void viewPdfDocument(String relativeUrl, String document) throws Exception {
clickOnLink("", document);
verifyPdfUrl();
}
public void clickOnLink(String linkXpath, String label) throws Exception {
LOG.info("Clicking link '{}' with xpath '{}'", label, linkXpath);
retryIfAssertionFails(() -> {
String xpath = linkXpath "//a/descendant-or-self::*[contains(normalize-space(text()), '" label "')]";
waitForElementToBeClickable(xpath);
driver.findElementByXPath(xpath).click();
return null;
});
LOG.info("Link clicked");
sleep(50);
}
private void verifyPdfUrl() throws Exception {
String parentWindowHandle = driver.getWindowHandle();
WebDriverWait wait = new WebDriverWait(driver, 5);
wait.until(ExpectedConditions.numberOfWindowsToBe(2));
for (String windowHandle : driver.getWindowHandles()) {
if (!windowHandle.equals(parentWindowHandle)) {
driver.switchTo().window(windowHandle);
}
}
String pdfTabUrl;
pdfTabUrl = driver.getCurrentUrl();
assertTrue(pdfTabUrl.contains(
"/app/restrict/application/caseInformation.pdf")); {
System.out.println("Correct url presented: " pdfTabUrl);
}
if (!driver.getWindowHandle().equals(parentWindowHandle)) {
driver.close();
}
driver.switchTo().window(parentWindowHandle);
}
In your case it is updating to the following:
Lp.ClickonMaterialsButton();
Thread.sleep(3000);
Lp.ClickonPDF1();//By clicking on pdf. Pdf opens in new tab
String parentWindowHandle = driver.getWindowHandle();
WebDriverWait wait = new WebDriverWait(driver, 5);
wait.until(ExpectedConditions.numberOfWindowsToBe(2));
for (String windowHandle : driver.getWindowHandles()) {
if (!windowHandle.equals(parentWindowHandle)) {
driver.switchTo().window(windowHandle);
}
}
String pdfTabUrl;
pdfTabUrl = driver.getCurrentUrl(); // to Fetch new url
ExcelLibrary.writeData("Sheet1", 0, 4, pdfTabUrl); //write url to excel sheet?
if (!driver.getWindowHandle().equals(parentWindowHandle)) {
driver.close();
} //Closing new tab
driver.switchTo().window(parentWindowHandle); //Switch back to original tab
CodePudding user response:
You can do that pretty easily with Selenium 4
Code:
driver.get("https://www.google.com");
driver.switchTo().newWindow(WindowType.TAB);
driver.navigate().to("https://www.stackoverflow.com");
System.out.println(driver.getCurrentUrl());
Output:
https://stackoverflow.com/
You can do like below with your code,
Lp.ClickonPDF1();
driver.switchTo().newWindow(WindowType.TAB);
String CurrentUrl = driver.getCurrentUrl();