我正在使用 Selenium 进行自动化。在一种情况下,当我单击页面 ABC 中的链接时,它会在新窗口中打开页面 DEF。现在,当我使用 driver.getCurrentUrl()
方法时,它会为我提供页面 ABC 的 URL。
但我需要检查页面 DEF 的 URL。请帮帮我,我该怎么办。
最佳答案
首先将控件切换到新打开的窗口,然后使用driver.getCurrentUrl()
。它会给你新打开的窗口的 URL
首先使用此开关控制
driver.switchTO().window("windowName/id");
然后执行此命令来获取 URL
driver.getCurrentUrl();
将控件切换到弹出窗口的逻辑
Set<String> beforePopup = driver.getWindowHandles();
//click which causes to open new window
driver.fineElement(<By>).click();
// get all the window handles after the popup window appears
Set<String> afterPopup = driver.getWindowHandles();
// remove all the handles from before the popup window appears afterPopup.removeAll(beforePopup);
// there should be only one window handle left
if(afterPopup.size() == 1)
{
driver.switchTo().window((String)afterPopup.toArray()[0]);
}
关于selenium - 如何从在新窗口中打开的WebDriver实例获取页面的URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15061002/