1

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 :

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)
4
  • Are you getting an error or is the field simply staying empty? Commented Feb 7, 2017 at 17:54
  • I am not getting any error in console, and the field is staying empty too. Commented Feb 7, 2017 at 17:58
  • Selecting by name "email" yields two input fields. The form on the top of the page also has a field named like that. WebDriver will only return the first one using findElement(By.name("email")). Your css selector should work, though. I suggest you try By.id("identify_email") Commented Feb 7, 2017 at 18:04
  • Tried before, no luck, then I tried css. Commented Feb 7, 2017 at 18:06

1 Answer 1

2

Wait for this element:

WebDriverWait wait = new WebDriverWait(driver, 10);
String email = "[email protected]";
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.

Sign up to request clarification or add additional context in comments.

7 Comments

The whole page is built from JS, so this might indeed help. On the other hand, WebDriver should throw an exception if the element cannot be found.
I have tried this with 20 sec as wait time as well, not working. Didn't get any exception either.
@Avishek2585835, I have edited my answer. This code should go into your if-block.
@Avishek2585835, a lot of JS on the Facebook site. Edited my answer another time. This time it should work.
@GrzegorzGórkiewicz Ya. this has worked. Switch to Javascript when nothing works. I forgot to try this while digging. Thanks a lot.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.