简体   繁体   中英

java/selenium - how can I click on an element from a form that pops up on top of the main page?

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.

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.

Here's the code in its current state for the first field, "Name":

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();
            }

Edit - working code (thanks segalaj):

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();
            }

As your popup is in an iframe, you need to tell to you webdriver to switch to the iframe:

<webdriver>.switchTo().frame(<iframe-webelement>);

Then you'll be able to access to iframe elements.
Do not forget to come back to the main page when you're done with the frame.

You can find the doc here .

Hope that helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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