如何访问Selenium中弹出的表单类型登录/注册

时间:2018-06-29 12:00:05

标签: java selenium selenium-webdriver

我正在尝试访问以下网站并弹出注册信息。在HTML中,它显示为表单类型。我尝试将其处理为警报,但不是,并且由于未打开模式对话框而出现异常。我尝试了窗把手。窗口句柄的大小仅为1。

请帮助我,以便我可以单击注册表格上的“登录”链接,然后登录。

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

System.setProperty("webdriver.gecko.driver", "C:\\mamtha\\Selenium Practice\\GeckoDriver\\geckodriver.exe");
    String URL = "http://way2automation.com/way2auto_jquery/tooltip.php";
    WebDriver driver = new FirefoxDriver();
    driver.get(URL);
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    Thread.sleep(3000);

Set <String> winhandle = driver.getWindowHandles();
System.out.println(winhandle.size());


    WebElement sigin = driver.findElement(By.xpath("//a[contains(text(), 'Signin']"));
    sigin.click();
    driver.findElement(By.xpath("//input[@name = 'username']")).sendKeys("myusername");
    driver.findElement(By.xpath("//input[@name = 'password']")).sendKeys("password");
    driver.findElement(By.xpath("//input[@class = 'button']")).click();

2 个答案:

答案 0 :(得分:1)

模态对话框将在同一页面本身中打开。因此,您不想使用窗口句柄。您需要先将焦点移至模式对话框,然后直接访问所需的元素(还添加一些显式的等待条件)。

工作代码:

    System.setProperty("webdriver.gecko.driver", "C:\\mamtha\\Selenium Practice\\GeckoDriver\\geckodriver.exe");
    String URL = "http://way2automation.com/way2auto_jquery/tooltip.php";
    WebDriver driver = new FirefoxDriver();
    driver.get(URL);
    driver.manage().window().maximize();
    //Explicit wait is added after the Page load
    WebDriverWait wait=new WebDriverWait(driver,20);
    wait.until(ExpectedConditions.titleContains("Welcome"));

    WebElement modalDialogBox=driver.findElement(By.className("fancybox-skin"));
    modalDialogBox.findElement(By.xpath(".//a[text()='Signin']")).click();

    wait.until(ExpectedConditions.visibilityOfElementLocated(By.className("fancybox-skin")));
    WebElement loginDialogBox=driver.findElement(By.className("fancybox-skin"));

    loginDialogBox.findElement(By.name("username")).sendKeys("myusername");
    loginDialogBox.findElement(By.name("password")).sendKeys("987654321");
    loginDialogBox.findElement(By.className("button")).click();

答案 1 :(得分:0)

尝试以下代码。

String URL = "http://way2automation.com/way2auto_jquery/tooltip.php";
WebDriver driver = new ChromeDriver();
driver.get(URL);
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.manage().window().maximize();
Thread.sleep(5000);

WebElement sign=driver.findElement(By.xpath("//p[@class='text_popup']/a[contains(text(),'Signin')]"));
sign.click();
Thread.sleep(5000);
driver.findElement(By.xpath("//div[@id='login']/form/fieldset[1]/input")).sendKeys("myusername");
Thread.sleep(5000);
driver.findElement(By.xpath("//div[@id='login']/form/fieldset[2]/input")).sendKeys("password");
driver.findElement(By.xpath("//div[@id='login']/form/div/div[2]/input")).click();