Switch tabs using Selenium WebDriver with Java

前端 未结 21 1457
野性不改
野性不改 2020-11-22 12:09

Using Selenium WebDriver with JAVA. I am trying to automate a functionality where I have to open a new tab do some operations there and come back to previous tab (Parent). I

21条回答
  •  春和景丽
    2020-11-22 13:01

    public class TabBrowserDemo {

    public static void main(String[] args) throws InterruptedException {
        System.out.println("Main Started");
        System.setProperty("webdriver.gecko.driver", "driver//geckodriver.exe");
        WebDriver driver = new FirefoxDriver();
        driver.get("https://www.irctc.co.in/eticketing/userSignUp.jsf");
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    
        driver.findElement(By.xpath("//a[text()='Flights']")).click();
        waitForLoad(driver);
        Set ids = driver.getWindowHandles();
        Iterator iterator = ids.iterator();
        String parentID = iterator.next();
        System.out.println("Parent WIn id " + parentID);
        String childID = iterator.next();
        System.out.println("child win id " + childID);
    
        driver.switchTo().window(childID);
        List hyperlinks = driver.findElements(By.xpath("//a"));
    
        System.out.println("Total links in tabbed browser " + hyperlinks.size());
    
        Thread.sleep(3000);
    //  driver.close();
        driver.switchTo().window(parentID);
        List hyperlinksOfParent = driver.findElements(By.xpath("//a"));
    
        System.out.println("Total links " + hyperlinksOfParent.size());
    
    }
    
    public static void waitForLoad(WebDriver driver) {
        ExpectedCondition pageLoadCondition = new
                ExpectedCondition() {
                    public Boolean apply(WebDriver driver) {
                        return ((JavascriptExecutor)driver).executeScript("return document.readyState").equals("complete");
                    }
                };
        WebDriverWait wait = new WebDriverWait(driver, 30);
        wait.until(pageLoadCondition);
    }
    

提交回复
热议问题