Reputation: 21
I used sendKeys
method, first time when I ran my script it worked but from second time it is not working. Script is finding the element but not entering in text box.
Please suggest if there is any other way to enter text in text box. and why it is not working.
Here is my code:
System.out.println(driver.findElements(By.xpath("//*[@id='logcomments']")).size());
driver.findElement(By.xpath("//*[@id='logcomments']")).sendKeys("Log_Testing"); // textBox
driver.findElement(By.xpath("//*[@id='postLog']/img")).click(); //enter button
HTML code :
<div style="position:relative;top:40px;">
<div>
<span id="actualcommentCount">1</span>
<span> Comments </span>
</div>
<div>
<textarea id="logcomments" type="text" style="resize:none; width:80%;" placeholder="Comments"> </textarea>
<span id="postLog">
<img style="cursor:pointer;width:45px;color:#337ab7;float: right;margin-top:-5px; margin-right:10px;font-size:30px;" src="images/poll_fly.png">
</span>
</div>
</div>
Upvotes: 2
Views: 8601
Reputation: 101
My suggestion is to try with javaScript executed to add text to the text box.
JavascriptExecutor jsExecutor = (JavascriptExecutor) driver;
jsExecutor.executeScript("document.getElementById('logcomments').value='Yourvalue is here'");
And also, plz post if there is an exception or error is there. Without that, it is hard to find a solution.
Upvotes: 0
Reputation: 1
If you want to type on keyboard then you need to use robot class as below. But ensure you have already clicked on textbox before you start typing.
typeKeysStringUsingRobotClass("hey123");
}
public void typeKeysStringUsingRobotClass(String text){
String[] arr = text.split("");
for(int i=0; i<arr.length; i++)
keyPressUsingRobotClass(arr[i]);
}
public void keyPressUsingRobotClass(String key){
Robot rb = new Robot();
if(key.equalsIgnoreCase("A")){ rb.keyPress(KeyEvent.VK_A); rb.keyRelease(KeyEvent.VK_A);
if(key.equalsIgnoreCase("B")){ rb.keyPress(KeyEvent.VK_B); rb.keyRelease(KeyEvent.VK_B);
.
.
.
if(key.equalsIgnoreCase("0")){ rb.keyPress(KeyEvent.VK_0); rb.keyRelease(KeyEvent.VK_0);
}
Upvotes: 0
Reputation: 23805
You should try as below :-
WebElement el = driver.findElement(By.id("logcomments"));
el.click();
el.sendKeys("Log_Testing");
driver.findElement(By.xpath("//*[@id='postLog']/img")).click();
Note :- You need to focus
on text area
before set value so need to use el.click()
to focus on text area
because when you try to .sendKeys()
it goes to set value but due to focus is not on text area
it could not be set.
Hope it will help you..:)
Upvotes: 1
Reputation: 287
Can you share the HTML
of the page?
You could try :-
driver.findElement(By.id("logcomments")).sendKeys("Some Text");
What error
are you getting when you run the test?
Upvotes: 1