0

Unable to send values in the table column. I got different errors while I am trying to insert a value in the column using Selenium.

  1. I tried to set a new value in the table's column. It shows the error as The element must be user-editable in order to clear it.

    WebElement.clear();
    WebElement.sendKeys("value");
    
      (or)
    
    WebElement.sendKeys(Keys.DELETE);
    WebElement.sendKeys("value");
    
  2. Then to click and edit the value.

    Actions actions = new Actions(getWebDriverEx());
    WebElement TableColumn = Driver.findElement(By.id("element"));
    actions.moveToElement(TableColumn);
    actions.click().build().perform();
    actions.sendKeys(Keys.BACK_SPACE+b+b);
    actions.sendKeys("value");
    

    The value which was passed is not inserted in the Tables column. But I can able to click the Tables column. Here my test passed.

  3. Then tried to set value. It shows the error as timed out.

     WebElement.sendKeys(Keys.DELETE);
     WebElement.sendKeys("15000");
    
  4. Again I used the div/span combination as XPath and I have edited the value. But it does not reflect in the table.

    JavascriptExecutor js = (JavascriptExecutor) getDriver();
    js.executeScript("document.getElementById('element').innerHTML="+15000); 
    

Here I do not get any errors. But the value not reflected after save.

I gave element to various formats.

  1. div//[id]
  2. div//span
  3. XPath
  4. id alone (which was in the div)

HTML:

<div id="element" class="tables_second_column">
    <div class="class_name">
        <div class="class_name">
            <div class="class_name"><span>5000</span></div>
        </div>
    </div>
</div>
Ratmir Asanov
  • 6,237
  • 5
  • 26
  • 40
sejn
  • 2,040
  • 6
  • 28
  • 82
  • First 3 approaches are not correct as sendKeys works only on 'input' html elements like textbox and textarea, not for span and div. In the fourth approach you need to get to the span and then use innerhtml. -- https://stackoverflow.com/questions/23775924/selenium-enter-the-text-in-div-using-xpath – Grasshopper Nov 21 '17 at 13:12
  • @Grasshopper But why to resort to `JavascriptExecutor` without a fair trial with `Java` **`click()`**? – undetected Selenium Nov 21 '17 at 13:14
  • @DebanjanB It is not mentioned if clicking makes a textbox to be visible to enter text. If so then it should be the preferred method. – Grasshopper Nov 21 '17 at 13:23
  • @Grasshopper I don't see any reason for the `textbox` to be `invisible` or `hidden`. IMO, as you mentioned OP simply didn't go deeper into the `` tag. So my guess `Java click()` should work. – undetected Selenium Nov 21 '17 at 13:26
  • 1
    @Grasshopper, `sendKeys` is not limited to any type of HTML element and a `
    ` can become editable by setting the attribute [`contenteditable="true"`](https://mdn.mozillademos.org/en-US/docs/Web/Guide/HTML/Editable_content$samples/How_does_it_work?revision=1323330).
    – Florent B. Nov 21 '17 at 13:56
  • @FlorentB. Did not know that... Thx – Grasshopper Nov 21 '17 at 14:17
  • @DebanjanB Java click() was working. But for my scenario, I want to pass the value in the table column. After the click, I can't able to pass the value. Again it shows the error as focus the element – sejn Nov 22 '17 at 06:28
  • @Jes, was my answer useful? If yes -- check a tick near my answer, please. – Ratmir Asanov Nov 23 '17 at 09:56

4 Answers4

1

As per the HTML you shared, the table column is within a <span> which is inside several <div> tags. Hence we need to construct an unique xpath to identify the WebElement and first send clear() method then use sendKeys() method as follows :

driver.findElement(By.xpath("//div[@id='element']/div[@class='class_name']/div[@class='class_name']/div[@class='class_name']/span[text()='5000']")).clear();    
driver.findElement(By.xpath("//div[@id='element']/div[@class='class_name']/div[@class='class_name']/div[@class='class_name']/span[text()='5000']")).sendKeys("15000");

Update

As you mentioned Text which was passed in the span is not predictable so we would simply omit the clause [text()='5000']. As you have provided a sample HTML with sample classnames I have constructed a nearly absolute xpath. So our code will be :

driver.findElement(By.xpath("//div[@id='element']/div[@class='class_name']/div[@class='class_name']/div[@class='class_name']/span")).clear();    
driver.findElement(By.xpath("//div[@id='element']/div[@class='class_name']/div[@class='class_name']/div[@class='class_name']/span")).sendKeys("15000");
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Text which was passed in the span is not predictable. It is a dynamic value. I need to get the text before passing the sendKeys. – sejn Nov 22 '17 at 05:13
  • Check out my updated Answer and let me know the status – undetected Selenium Nov 22 '17 at 05:19
  • Unable to clear the webelement and it shows the error as "Element must be user-editable in order to clear it" – sejn Nov 22 '17 at 06:33
  • So that means the **sample [DIV]** s which you have provided on your own is uneditable and incorrect & we can't help you in that. So please `Accept` the `Answer` by clicking on the tick mark beside my **`Answer`**, just below the `VoteDown` arrow, so the tick mark turns Green. – undetected Selenium Nov 22 '17 at 06:41
  • Div is correct. Using the XPath which you have mentioned above not working for me. I need to get the element first. Then only I can able to edit the value. – sejn Nov 22 '17 at 06:47
1

Try on the following and it's working for me:

js = "document.querySelector('#element .class_name .class_name .class_name>span').innerHTML = '15000';"
driver.execute_script(js)

Hope it helps you!

Ratmir Asanov
  • 6,237
  • 5
  • 26
  • 40
1

This should be sufficient

WebElement textBox = driver.findElement(By.xpath("//span[text()='5000']"));
textBox.clear();
textBox.sendKeys("");
textBox.sendKeys("15000");

I do send a empty space in order to get the textbox active as sometime the DOM might not reflect immediately as this element is quiet nested.

Automator
  • 176
  • 1
  • 6
0

Thanks for your reply

It works for me with the below method

webElement.sendKeysByAction(value)
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
sejn
  • 2,040
  • 6
  • 28
  • 82