栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

从第二个窗口切换到第三个窗口时如何通过Selenium WebDriver处理多个窗口

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

从第二个窗口切换到第三个窗口时如何通过Selenium WebDriver处理多个窗口

这是切换到

Third Window
并单击
Login
按钮的完整代码块:

package demo;import java.util.HashMap;import java.util.Map;import java.util.Set;import org.openqa.selenium.By;import org.openqa.selenium.JavascriptExecutor;import org.openqa.selenium.WebDriver;import org.openqa.selenium.WebElement;import org.openqa.selenium.chrome.ChromeDriver;import org.openqa.selenium.chrome.ChromeOptions;import org.openqa.selenium.support.ui.ExpectedConditions;import org.openqa.selenium.support.ui.WebDriverWait;public class Q45455402_multiple_windows {    static WebDriver driver;    public static void main(String[] args) throws InterruptedException     {        System.setProperty("webdriver.chrome.driver","C:\Utility\BrowserDrivers\chromedriver.exe");        Map<String, Object> prefs = new HashMap<String, Object>();        prefs.put("credentials_enable_service", false);        prefs.put("password_manager_enabled", false);         ChromeOptions options = new ChromeOptions();        options.setExperimentalOption("prefs", prefs);        options.addArguments("start-maximized");        options.addArguments("disable-infobars");        options.addArguments("--disable-extensions");        driver = new ChromeDriver(options);        driver.get("http://www.universalmusic.com");        driver.findElement(By.xpath("//a[@id='main-menu-open']/span")).click();        WebDriverWait wait = new WebDriverWait(driver, 5);        WebElement our_label = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//div[@id='main-menu-container']//h3[contains(text(),'Our Labels')]")));        our_label.click();        JavascriptExecutor je = (JavascriptExecutor)driver;        WebElement element = driver.findElement(By.xpath("//div[@id='section-items']/div[@]//a[contains(@href,'http://www.universalmusic.com/label/abbey-road-studios/')]"));        je.executescript("arguments[0].scrollIntoView(true);",element);        element.click();        JavascriptExecutor je2 = (JavascriptExecutor)driver;        WebElement element2 = driver.findElement(By.xpath("//div[@id='detail-main']//a[@]"));        je2.executescript("arguments[0].scrollIntoView(true);",element2);        String parent_window = driver.getWindowHandle();        System.out.println("Parent Window ID is : "+parent_window);        element2.click();        Set<String> allWindows = driver.getWindowHandles();        int count = allWindows.size();        System.out.println("Now Total Windows : "+count);        for(String child_1:allWindows) if(!parent_window.equalsIgnoreCase(child_1))     driver.switchTo().window(child_1);        System.out.println(driver.getTitle());        String child1_window = driver.getWindowHandle();        System.out.println("Child 1 Window ID is : "+child1_window);        driver.findElement(By.linkText("Shop")).click();        Set<String> all_Windows = driver.getWindowHandles();        int count_final = all_Windows.size();        System.out.println("Now Total Windows : "+count_final);        for(String child_2:all_Windows) if(!parent_window.equalsIgnoreCase(child_2) && !child1_window.equalsIgnoreCase(child_2))     driver.switchTo().window(child_2);        String child2_window = driver.getWindowHandle();        System.out.println("Child 2 Window ID is : "+child2_window);        driver.findElement(By.xpath("//div[@id='col1']//a[@]")).click();    }}

我的IDE控制台上的输出是:

Parent Window ID is : CDwindow-4663fceb-a7a3-4d72-8236-510790729377Now Total Windows : 2Child 1 Window ID is : CDwindow-c5de0d53-06ab-4e27-9516-b9c7a130cd5fNow Total Windows : 3Child 2 Window ID is : CDwindow-e88831ba-6b53-449c-a2f2-b8ba341c8433


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/636076.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号