简体   繁体   English

在不知道url Selenium webDriver的情况下打开新的url窗口

[英]open new url window without knowing the url Selenium webDriver

in all Selenium cod examples if I want open webdriver specific website page its goes like this 在所有Selenium cod示例中,如果我想打开特定于webdriver的网站页面,它会像这样

`chromeDriver.Navigate().GoToUrl(sitename); `chromeDriver.Navigate()。GoToUrl(sitename);

but now I have a problem that when I click on webpage that I'm testing its open new website (3th pary website) and I cant know the URL in advance. 但是现在我有一个问题,当我单击网页时,我正在测试其打开的新网站(第3个parly网站),并且我无法提前知道该URL。 so anyone know ,how can I know what is the URL in the new page that opens ?? 所以任何人都知道,我怎么知道打开的新页面中的URL是什么?

Try this: 尝试这个:

driver.get("https://stackoverflow.com");
Actions action = new Actions(driver);
action.moveToElement(driver.findElement(By.xpath("//*[@id='nav-tags']"))).sendKeys(Keys.SHIFT).click().build().perform();

// You have opened your unknown site in a new window till now

String currentWindowHandle = driver.getWindowHandle();
ArrayList < String > windowHandles = new ArrayList < String > (driver.getWindowHandles());
    for (int i = 0; i < windowHandles.size(); i++) {
    if (windowHandles.get(i).equals(currentWindowHandle)) {

        //SwitchTo new Window
        driver.switchTo().window(windowHandles.get(i + 1));

        //Do your stuff like get URL that tou need
        System.out.println(driver.getCurrentUrl());
        System.out.println(driver.getTitle());

        //Close the newly opened window
        driver.close();
    }
}
driver.switchTo().window(currentWindowHandle);

Here this will open tags window in a new window and get the URL of that window, Assuming that is what you want. 在这里,这将在新窗口中打开标签窗口,并获取该窗口的URL,假设这就是您想要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM