yashwanth
yashwanth

Reputation: 35

How to clear the default values in the username textfield and enter my value in selenium webdriver

I have tried the following code but it is throwing the exception (ElementNotVisibleException)

FirefoxDriver dr = new FirefoxDriver();
dr.get("http://54.169.235.143/book.html?v=0.03");
System.out.println("First Testcase");
System.out.println(dr.findElement(By.id("user_name")));
dr.findElement(By.id("user_name"));
dr.findElement(By.id("user_name")).click();
dr.findElement(By.id("user_name")).getAttribute("user_name");
dr.findElement(By.id("user_name")).clear();
dr.findElement(By.id("user_name")).sendKeys("student100");

What am I doing wrong and how to fix it?

Upvotes: 2

Views: 296

Answers (2)

Vishal
Vishal

Reputation: 121

n software testing services this can be achieved by many ways some of the options are displayed above remaining are as follow.

Using java script

driver.executeScript("document.getElementByXpath('element').setAttribute('value', 'abc')");

Using action class Actions actions = new Actions(driver);

actions.click(driver.findElement(element) .keyDown(Keys.CONTROL).sendKeys("a").keyUp(Keys.CONTROL).sendKeys(Keys.BACK_SPACE).build().perform());

Upvotes: 0

Helping Hands
Helping Hands

Reputation: 5396

Actually your page taking time to load so web driver need wait until element gets visible , Below code will solve your issue :

     WebDriverWait wait= new WebDriverWait(dr,30);
     wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("user_name")));
     dr.findElement(By.id("user_name")).clear();
     dr.findElement(By.id("user_name")).sendKeys("test");

     wait= new WebDriverWait(dr,30);
     wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("pass_word")));
     dr.findElement(By.id("pass_word")).clear();
     dr.findElement(By.id("pass_word")).sendKeys("test");

I have just added wait for elements.

Upvotes: 3

Related Questions