Eclipse Selenium Facebook登录问题(新页面电子邮件)driver.findElement(By.id(" email"))。sendKeys(""); Java的

时间:2015-06-21 13:39:57

标签: java eclipse email selenium facebook-login

我正在尝试登录facebook但是有问题。

我想写"电子邮件"到facebook登录页面,但是它已写入我网站的登录页面。

我不想给site.com的电子邮件输入写电子邮件,我想给打开弹出窗口的facebook登录输入写电子邮件。

以下是我的代码部分:

        driver.get("http://www.example.com");
    //click login
    driver.findElement(By.className("btnSignIn")).click();
    //If it's not login already with Facebook account; wants username and password.
    driver.findElement(By.className("facebookBtn")).click();

    driver.findElement(By.id("email")).sendKeys("selenium");

所有代码:

package com.example.tests;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class ExampleLogin {

/**
 * @param args
 */
public static void main(String[] args) {
    // TODO Auto-generated method stub
    //Create Firefox driver to drive the browser
    WebDriver driver = new FirefoxDriver();

    //Create Chrome driver to drive the browser
    //System.setProperty("webdriver.chrome.driver", 
    //"C:\\Users\\USER\\Desktop\\selenium-java-2.46.0\\chromedriver.exe");

    //WebDriver driver = new ChromeDriver();

    //Open Site Homepage
    driver.get("http://www.example.com");
    //click login
    driver.findElement(By.className("btnSignIn")).click();
    //If it's not login already with Facebook account; wants username and password.
    driver.findElement(By.className("facebookBtn")).click();

    driver.findElement(By.id("email")).sendKeys("selenium");

    //After login, browser will go http://www.example.com address and will confirm the login status.        

}

}

1 个答案:

答案 0 :(得分:3)

由于它在不同的弹出窗口中打开,因此在进行任何操作之前,您需要先切换到该窗口(弹出窗口)。尝试先获取弹出窗口的窗口对象,然后切换到尝试编写电子邮件的窗口。下面的代码将有助于找到窗口。相应地自定义

Set<String> windowId = driver.getWindowHandles();    // get  window id of current window
    Iterator<String> itererator = windowId.iterator();   

    String mainWinID = itererator.next();
    String  newAdwinID = itererator.next();

    driver.switchTo().window(newAdwinID);
    System.out.println(driver.getTitle());
   // perform the operation on the popup driver.operations