Java Can';t使用webdriver单击元素

Java Can';t使用webdriver单击元素,java,selenium,webdriver,Java,Selenium,Webdriver,我面临一个非常奇怪的问题。我正在尝试打开facebook>点击遗忘账户链接>然后在一个新选项卡中打开它>点击两个文本框。 我的代码是: import java.awt.AWTException; import java.awt.Robot; import java.awt.event.KeyEvent; import java.util.ArrayList; import java.util.concurrent.TimeUnit; import org.openqa.selenium.By;

我面临一个非常奇怪的问题。我正在尝试打开facebook>点击遗忘账户链接>然后在一个新选项卡中打开它>点击两个文本框。 我的代码是:

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;


public class OpenLinkInNewTabTest {

    public static void main(String[] args) throws Exception {
        System.setProperty("webdriver.chrome.driver", "<path>\\chromedriver.exe");
        WebDriver driver = new ChromeDriver();

        driver.get("http://www.facebook.com");
        String ParentWindowHandle = driver.getWindowHandle();
        WebElement w = driver.findElement(By.linkText("Forgotten account?"));


        new Actions(driver)
        .keyDown(Keys.CONTROL)
        .keyDown(Keys.SHIFT)
        .click(w)
        .keyUp(Keys.SHIFT)
        .keyUp(Keys.CONTROL)
        .perform();

    new Actions(driver)
        .sendKeys(Keys.CONTROL + "w")
        .perform();




    // ArrayList<String> tabs = new ArrayList<String> (driver.getWindowHandles());
//   
//  driver.switchTo().window(tabs.get(1));
//  WebElement fn = (new WebDriverWait(driver, 20))
//            .until(ExpectedConditions.presenceOfElementLocated(By.cssSelector("#identify_email")));
//  System.out.println(driver.getTitle());
//  fn.sendKeys("abcdejf:");



    for(String winHandle : driver.getWindowHandles()){

         driver.switchTo().window(winHandle);
        if(driver.getTitle().contains("Forgotten Password ")){

            Thread.sleep(5000);


              driver.findElement(By.cssSelector("#identify_email")).sendKeys("adf");
            driver.findElement(By.name("email")).sendKeys("ASF");
            driver.close();
            driver.switchTo().window(ParentWindowHandle);

            break; 

        }
    }


    driver.findElement(By.name("email")).sendKeys("ASF");

    }

}
该元素看起来像:

<input id="identify_email" class="inputtext" name="email" autofocus="1" type="text">
它能够正确地单击这两个元素。 运行时,我没有看到任何元素未找到异常。请帮助我调试此问题。 谢谢 更新:

运行此代码有时会显示

Exception in thread "main" org.openqa.selenium.InvalidElementStateException: invalid element state
  (Session info: chrome=56.0.2924.87)
等待此元素:

WebDriverWait wait = new WebDriverWait(driver, 10);
String email = "you@domain.com";
WebElement emailInputField = wait.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector("#identify_email")));
JavascriptExecutor executor = (JavascriptExecutor) driver;
executor.executeScript("arguments[0].setAttribute('value', ' " + email + "')", emailInputField);
break;

单击可激活此元素。

您是否收到错误消息,或者该字段只是保持为空?我在console中没有收到任何错误消息,该字段也保持为空。按名称选择“电子邮件”将生成两个输入字段。页面顶部的表单也有这样一个字段。WebDriver将仅使用
findElement(By.name(“email”))
返回第一个。不过,您的css选择器应该可以工作。我建议你试试
By.id(“identification_email”)
以前试过,运气不好,然后我试过css。整个页面都是用JS构建的,所以这可能确实有帮助。另一方面,如果找不到元素,WebDriver应该抛出一个异常。我也尝试了20秒作为等待时间,但没有工作。也没有得到任何异常。@Avishe2585835,我已经编辑了我的答案。这段代码应该放在你的if块中。@avishe2585835,Facebook网站上有很多JS。另一次编辑了我的答案。这一次它应该会起作用。@GrzegorzGórkiewicz Ya。这已经奏效了。当一切都不起作用时,切换到Javascript。我挖的时候忘了试试这个。谢谢。
Exception in thread "main" org.openqa.selenium.InvalidElementStateException: invalid element state
  (Session info: chrome=56.0.2924.87)
WebDriverWait wait = new WebDriverWait(driver, 10);
String email = "you@domain.com";
WebElement emailInputField = wait.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector("#identify_email")));
JavascriptExecutor executor = (JavascriptExecutor) driver;
executor.executeScript("arguments[0].setAttribute('value', ' " + email + "')", emailInputField);
break;