0

In the url http://letskodeit.teachable.com/pages/practice, there is a button element called Open Window.

I can inspect the element button called "openwindow". I tried using "id" and "xpath" for this element. but the error says "can't find the element". I have implicit wait. I can inspect the element button called "openwindow". I tried using "id" and "xpath" for this element. but the error says "can't find the element". I have implicit wait.

import java.util.Set;
import java.util.concurrent.TimeUnit;   

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;     

public class WindowHandles {

       private WebDriver achromeDriver;
       private String baseUrl;   

       @Before
       public void setUp() throws Exception {  

              baseUrl = "http://letskodeit.teachable.com/pages/practice";           
              System.setProperty("webdriver.chrome.driver", "C:\\Selenium\\ChromeDirver\\chromedriver.exe");             

              achromeDriver = new ChromeDriver();               

              achromeDriver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);             

              achromeDriver.manage().window().maximize();   

              System.out.println("setup completed");
       }    

       @Test

       public void test() throws InterruptedException {                       // Get the handle

              String parentHandle = achromeDriver.getWindowHandle();
              System.out.println("Parent Handle: " + parentHandle);                

              // Find Open Window button                
             System.out.println("before finding the element");                

              WebElement openWindow = achromeDriver.findElement(By.xpath("//button[@name='openwindow']"));                

              openWindow.click();    

              // Get all handles

              Set<String> handles = achromeDriver.getWindowHandles();    

              // Switching between handles

              for (String handle: handles) {
                     System.out.println(handle);

              }
              // Switch back to the parent window

       }



       @After

       public void tearDown() throws Exception {

       }

}
Greg Burghardt
  • 17,900
  • 9
  • 49
  • 92
bnath002
  • 57
  • 1
  • 7

2 Answers2

0

To click() on the element with text as Open Window you have to induce WebDriverWait for the elementToBeClickable() and you can use either of the following Locator Strategies:

  • cssSelector:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("button#openwindow"))).click();
    
  • xpath:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//button[@id='openwindow']"))).click();
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
0

I have added explicit wait and achromeDriver.get(baseUrl); method to launch the url. Below code is working for me.

import java.util.Set;
import java.util.concurrent.TimeUnit;   

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;     

public class WindowHandles {

   private WebDriver achromeDriver;
   private String baseUrl;   

   @Before
   public void setUp() throws Exception {  

          baseUrl = "http://letskodeit.teachable.com/pages/practice";           
          System.setProperty("webdriver.chrome.driver", 
"C:\\Selenium\\ChromeDirver\\chromedriver.exe");             

          achromeDriver = new ChromeDriver();               
          achromeDriver.get(baseUrl);


          achromeDriver.manage().window().maximize();   

          System.out.println("setup completed");
   }    

   @Test

   public void test() throws InterruptedException {   // Get the handle

          String parentHandle = achromeDriver.getWindowHandle();
          System.out.println("Parent Handle: " + parentHandle);                
          WebDriverWait w= new WebDriverWait(achromeDriver, 10);
          WebElement openWindow = 
 achromeDriver.findElement(By.xpath("//button[@id='openwindow']")); 
          w.until(ExpectedConditions.elementToBeClickable(openWindow));
          // Find Open Window button                
         System.out.println("before finding the element");                              

          openWindow.click();    

          // Get all handles

          Set<String> handles = achromeDriver.getWindowHandles();    

          // Switching between handles

          for (String handle: handles) {
                 System.out.println(handle);

          }
          // Switch back to the parent window

   }



   @After

   public void tearDown() throws Exception {

   }

}
Prashanth
  • 16
  • 1