4

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" />
iRunner
  • 1,472
  • 6
  • 25
  • 40

3 Answers3

4

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

Community
  • 1
  • 1
Ajinkya
  • 22,324
  • 33
  • 110
  • 161
  • Any option by using xpath. I can only xpath for that.As id is auto generated. – iRunner May 21 '14 at 07:17
  • 1
    Note the XPath JS solution you've got isn't going to work in – Arran May 21 '14 at 08:33
  • So any other solution for this ? – iRunner May 21 '14 at 09:31
  • @iRunner Which browser you are using? – Ajinkya May 21 '14 at 09:32
  • Currently Firefox. but may have to automate using IE. – iRunner May 21 '14 at 09:33
  • I am using this executor.executeScript("document.evaluate('//div[contains(@class,'x-grid3-cell-inner x-grid3-col-code')]', document, null, 9, null).singleNodeValue.innerHTML="+ "abc"); It is giving me error as Exception in thread "main" org.openqa.selenium.WebDriverException: missing ) after argument list Am i missing something ? – iRunner May 21 '14 at 09:55
0

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.

Nashibukasan
  • 2,028
  • 23
  • 37
0

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);