java / selenium - 如何点击主页顶部弹出的表单中的元素?

时间:2022-12-10 16:01:01

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":

这是第一个字段“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):

编辑 - 工作代码(感谢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();
            }

1 个解决方案

#1


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

由于您的弹出窗口位于iframe中,您需要告诉webdriver切换到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.

然后,您将能够访问iframe元素。完成框架后,不要忘记返回主页面。

You can find the doc here.

你可以在这里找到这份文件。

Hope that helps.

希望有所帮助。

#1


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

由于您的弹出窗口位于iframe中,您需要告诉webdriver切换到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.

然后,您将能够访问iframe元素。完成框架后,不要忘记返回主页面。

You can find the doc here.

你可以在这里找到这份文件。

Hope that helps.

希望有所帮助。