Reputation: 91
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)
Upvotes: 1
Views: 3247
Reputation: 4586
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.
Upvotes: 2