iRunner
iRunner

Reputation: 1532

Selenium : Enter the text in div using xpath

In selenium for inputbox, we can enter value like :

WebElement inputBox = driver.findElement(By.xpath(xpath)));
inputBox.sendKeys("abc");

but on 1 webpage, I am having one button after clicking on that get one div in which i have to enter through selenium, I am getting the xpath for that div like

WebElement inputDiv = driver.findElement(By.xpath("//div[contains(@class,'x-grid3-cell-inner')]"));
inputDiv.sendKeys("abc");           //This is not working

using xpath,I am getting the div, But how to enter text in that using it's xpath?

Html after adding the text manually :

<div class="x-grid3-cell-inner">ty</div>

The div in which I have to enter the text is:

<div class="x-grid3-cell-inner" />

Upvotes: 4

Views: 10423

Answers (3)

Whizdom Trainings
Whizdom Trainings

Reputation: 61

Sendkeys is used to type in the input field with tag name 'input' You cannot use sendkeys here

Rather you will have to use JavascriptExecutorto do this

JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("document.getElementById('Div_Id').innerHTML="+ DesiredText);

Upvotes: 0

Ajinkya
Ajinkya

Reputation: 22710

You can update the DIV text using JavaScript

JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("document.getElementById('Div_Id').innerHTML="+ DesiredText);

Using XPATH in JavaScript

JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("document.evaluate(xpathExpresion, document, null, 9, null).singleNodeValue.innerHTML="+ DesiredText);

Reference

Upvotes: 4

Nashibukasan
Nashibukasan

Reputation: 2028

You cannot edit this div using send keys as it has no Input to send to. Instead, try and edit the text field of the div.

Upvotes: 0

Related Questions