无法使用webdriver单击元素

时间:2021-09-15 21:04:06

I am facing a very strange problem. I am trying to open facebook>click on forgotten account link> then opening it in a new tab> click on two textboxes. My code is :

我面临一个非常奇怪的问题。我正在尝试打开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");

    }

}

However, I am unable to send value to

但是,我无法发送价值

driver.findElement(By.cssSelector("#identify_email")).sendKeys("adf");

The element looks like:

该元素看起来像:

<input id="identify_email" class="inputtext" name="email" autofocus="1" type="text">

If I write a similar code like:

如果我写一个类似的代码,如:

System.setProperty("webdriver.chrome.driver","path\\chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        driver.get("http://www.facebook.com");

        driver.findElement(By.linkText("Forgotten account?")).click();

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

It is able to click both the elements properly. I am not seeing any element not found exception too while running it. Please help me to debug this issue. Thanks. UPDATE:

它能够正确地单击这两个元素。运行它时,我没有看到任何元素未找到异常。请帮我调试这个问题。谢谢。更新:

Running this code sometimes show

运行此代码有时会显示

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

1 个解决方案

#1


2  

Wait for this element:

等待这个元素:

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;

Clicking activates this element.

单击可激活此元素。

#1


2  

Wait for this element:

等待这个元素:

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;

Clicking activates this element.

单击可激活此元素。