如何復制新標簽 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
}
}'
我試圖復制在新選項卡中打開的 URL,但復制舊選項卡 url。如何復制新標簽 url java selenium?
uj5u.com熱心網友回復:
單擊后Lp.ClickonPDF1()會打開一個新選項卡,您必須將 Selenium 驅動程式切換到打開的選項卡才能在那里執行操作。
所以你的代碼可以是這樣的:
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
不確定如何driver在您的基礎架構中使用該物件,因此我將其用作常規物件,與任何頁面物件實體無關。
uj5u.com熱心網友回復:
以下是我為與您的方法類似的測驗專案(使用cucumber-java 和selenium-java)創建的方法,其中需要:
- 單擊網頁上 pdf 檔案的鏈接,該鏈接在新選項卡中打開
- 列出打開的標簽
- 移動到新標簽
- 獲取 pdf 檔案的 url 并驗證(斷言)它是正確的
- 關閉 pdf 選項卡
- 回傳網頁
隨意使用你需要的
@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);
}
在您的情況下,它正在更新為以下內容:
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
uj5u.com熱心網友回復:
你可以很容易地做到這一點 Selenium 4
代碼:
driver.get("https://www.google.com");
driver.switchTo().newWindow(WindowType.TAB);
driver.navigate().to("https://www.stackoverflow.com");
System.out.println(driver.getCurrentUrl());
輸出:
https://stackoverflow.com/
你可以像下面這樣用你的代碼,
Lp.ClickonPDF1();
driver.switchTo().newWindow(WindowType.TAB);
String CurrentUrl = driver.getCurrentUrl();
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/357381.html
