Reputation: 86
I am trying to log in to my profile but I am getting an issue when trying to add in the password. I can get the cursor to show up in the password box but when trying to send the keys, it will throw an exception. Is there some security reason for this? Is the password hidden within the html/xml in order for automation to not take place?
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class mainTester {
static WebDriver driver;
public static void main(String[] args) throws InterruptedException{
setUp();
}
public static void setUp() throws InterruptedException {
// Optional, if not specified, WebDriver will search your path for chromedriver.
System.setProperty("webdriver.chrome.driver", "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chromedriver.exe");
driver = new ChromeDriver();
driver.get("http://www.google.com/xhtml");
// Thread.sleep(5000); // Let the user actually see something!
WebElement searchBox = driver.findElement(By.name("q"));
searchBox.sendKeys("C programming forum");
searchBox.submit();
Thread.sleep(5000);
driver.findElement(By.linkText("C Board - Cprogramming.com")).click();
Thread.sleep(5000); // Let the user actually see something!
WebElement userEnter = driver.findElement(By.name("vb_login_username"));
userEnter.sendKeys("myusername");
WebElement userEnter2 = driver.findElement(By.name("vb_login_password_hint"));
userEnter2.sendKeys("mypassword");
// searchBox.submit();
Thread.sleep(5000); // Let the user actually see something!
System.out.println("Finished");
//driver.quit();
}
}
the html is like so
<input type="text" class="textbox default-value" tabindex="102" name="vb_login_password_hint" id="navbar_password_hint" size="10" value="Password" style="display: inline;">
and the error is
Exception in thread "main" org.openqa.selenium.WebDriverException: unknown error: cannot focus element
Upvotes: 1
Views: 4408
Reputation: 473753
This login form is a bit tricky, there are two password inputs - one is acting like a "hint" being displayed over the actual password field. What you need to do is to click the "hint" first and then send the keys to the real password input:
WebElement passwordHint = driver.findElement(By.name("vb_login_password_hint"));
passwordHint.click();
WebElement passwordInput = driver.findElement(By.name("vb_login_password"));
passwordInput.sendKeys("mypassword");
Upvotes: 3