title: java/selenium - how can I click on an element from a form that pops up on top of the main page?
body: <p>At a certain point in my automation project, I have to click on an element that is part of a form that pops up on top of the main page (it doesn't go away like those menus that show up when hovering over a certain element and then disappear) after successfully accessing the page and clicking on the element that brings out the form.</p>
  
  <p>I've been trying all kinds of solutions suggested in comments from other questions, such as using offsets and waiting until the element became clickable, but none worked for me.</p>
  
  <p>Here's the code in its current state for the first field, "Name":</p>
  
  <pre><code>try {
                  System.out.println("Filling in mandatory fields of the contact form... ");
                  wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[@id='jcemediabox-popup-frame']")));
  
                  System.out.println("Filling in name... ");
                  wait.until(ExpectedConditions.elementToBeClickable(By.xpath("(//div[@class='formBody']/input)[1]")));
                  WebElement nameField = driver.findElement(By.xpath("(//div[@class='formBody']/input)[1]"));
                  actions.moveToElement(nameField).build().perform();
                  actions.moveByOffset(5, 5).build().perform();
                  nameField.sendKeys(contactInput[1]);
                  nameField.submit();
                  System.out.println("Filled in name");
  
              } catch (Exception e) {
                  System.out.println("Could not complete actions on: contact form");
                  e.printStackTrace();
              }
  </code></pre>
  
  <p><strong>Edit - working code (thanks segalaj):</strong></p>
  
  <pre><code>try {
                  System.out.println("Filling in mandatory fields of the contact form... ");
                  wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//iframe[@id='jcemediabox-popup-iframe']")));
                  driver.switchTo().frame(driver.findElement(By.xpath("//iframe[@id='jcemediabox-popup-iframe']")));
  
                  driver.wait(pauseInSeconds);
  
                  System.out.println("Filling in name... ");
                  wait.until(ExpectedConditions.elementToBeClickable(By.name("form[Name]")));
                  driver.findElement(By.name("form[Name]")).sendKeys(contactInput[0]);
                  System.out.println("Filled in name");
  
                  driver.wait(shorterWait);               
  
                  driver.switchTo().defaultContent();             
  
              } catch (Exception e) {
                  System.out.println("Could not complete actions on: contact form");
                  e.printStackTrace();
              }
  </code></pre>