0

I have this in the HTML:

<textarea name="comment" class="form-control" rows="3" id="textarea_1160688690910416779_2159935466"></textarea>

I want to interact with id=textarea_, but the numbers are constantly changing after the "_". To solve this, I used this code:

driver.findElement(By.xpath("[starts-with(@id, 'textarea')")).sendKeys(comment);

However I am getting the error:

org.openqa.selenium.NoSuchElementException: Unable to locate element:
Sandeep Shah
  • 169
  • 4
  • 11
  • First of all, the XPath expression posted is not valid : `//*[starts-with(@id, 'textarea_')]` – har07 Jan 12 '16 at 10:37

3 Answers3

0

Use

driver.findElement(By.xpath("//textarea[starts-with(@id,'textarea_')]")).sendKeys(comment);
Slava Vedenin
  • 58,326
  • 13
  • 40
  • 59
sarjit07
  • 7,511
  • 1
  • 17
  • 15
0

The reason you're getting this error is that you need to add '//' before your current XPath. You could either use starts-with, this way:

("//textarea[starts-with(@id, 'textarea_')]")

You could also give 'contains' a try:

("//textarea[contains(@id, 'textarea_')]")

In both ways, you could use //*[... instead of textarea, for more cases

Skatz1990
  • 212
  • 7
  • 19
0

Your Xpath is not a valid Xpath.

  • It should contain a path and tag name
  • and it should be balanced (your [ is not closed)

So, if your tag is, textarea, and if it is at top: use /textarea

if not at top, use //textarea

This gives: "//textarea[starts-with(@id, 'textarea')]"

To use with selenium, you could also read that: JAVA - How to use xpath in selenium

and that: Webdriver findElements By xpath

Community
  • 1
  • 1