首页 > 解决方案 > Selenium WebDriver Java - 首次打开页面时出现的注册表单

问题描述

我是第一次打开一个页面,但是在成功加载页面后,一个注册表单马上就来了。我想在打开的页面上工作,但如果不处理页面加载时立即出现的注册表单,这是不可能的,但问题是我无法将控件切换到正在打开的注册表单。我使用 Selenium WebDriver 和 Java 作为脚本语言。

由于这个问题与我的一些官方项目有关,所以我无法分享确切的 URL,但我设法找到了另一个与我面临问题的 URL 类似的 URL。我在下面提供了替代 URL:

http://way2automation.com/way2auto_jquery/index.php

在上面的 URL 中,我想在加载的页面中工作,但在处理注册表单之前无法完成。请告诉我如何使用 Selenium WebDriver 和 Java 作为语言将控制切换到注册表单。

标签: javaseleniumselenium-webdriver

解决方案


您必须使用链式搜索概念查找元素。上述注册弹出窗口在 iframe中不可用。因此,首先您必须找到注册表单的父 WebElement,然后您可以使用父元素引用访问所需的元素。

    //Finding the Parent Element of the Registration Form
    WebElement popUp=driver.findElement(By.className("fancybox-skin"));

    //Finding the Name Input Text Field using the Parent Element
    popUp.findElement(By.xpath(".//*[@id='load_form']/fieldset[1]/input")).sendKeys("Subburaj");

同样,您可以使用弹出元素(父元素)访问所有适用的注册表单元素

注意:我已经测试了上面的代码,它工作正常

编辑:完整代码:

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.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class Way2Automation {

    public static void main(String args[]){
        System.setProperty("webdriver.chrome.driver", "drivers/chromedriver.exe");
        WebDriver driver=new ChromeDriver();
        driver.get("http://way2automation.com/way2auto_jquery/index.php");

        //Explicit wait is added after the Page load
        WebDriverWait wait=new WebDriverWait(driver,20);
        wait.until(ExpectedConditions.titleContains("Welcome"));

        WebElement popUp=driver.findElement(By.className("fancybox-skin"));

        popUp.findElement(By.xpath(".//*[@id='load_form']/fieldset[1]/input")).sendKeys("Subburaj");
        popUp.findElement(By.xpath(".//*[@id='load_form']/fieldset[2]/input")).sendKeys("987654321");

    }
}

推荐阅读