0
<button id="attachment-trigger-ember1000" class="msg-form__footer-action button-tertiary-medium-round-muted m0" type="button" style="" xpath="1">
  <span class="visually-hidden">Attach a file</span>
  <li-icon aria-hidden="true" type="paperclip-icon"><svg viewBox="0 0 24 24" width="24px" height="24px" x="0" y="0" preserveAspectRatio="xMinYMin meet" class="artdeco-icon" focusable="false"><path d="M8,6h9.5c3,0,5.5,2.5,5.5,5.5S20.5,17,17.5,17H5c-2.2,0-4-1.8-4-4s1.8-4,4-4h9.5c1.4,0,2.5,1.1,2.5,2.5S15.9,14,14.5,14H8v-2h6.5c0.3,0,0.6-0.1,0.6-0.4c0,0,0,0,0-0.1c0-0.3-0.3-0.5-0.5-0.5c0,0-0.1,0-0.1,0H5c-1-0.1-2,0.6-2.1,1.6c0,0.1,0,0.3,0,0.4c-0.1,1,0.7,1.9,1.7,2c0.1,0,0.3,0,0.4,0h12.6c1.9,0,3.5-1.5,3.5-3.3c0-0.1,0-0.1,0-0.2c0-1.9-1.4-3.4-3.3-3.5c-0.1,0-0.1,0-0.2,0H8V6z" class="large-icon" style="fill: currentColor"></path></svg></li-icon>
</button>

Above is html code snippet

and I am trying with following xpath :

driver.findElement(By.xpath("//*[@id=\"attachment-trigger-ember1000\"]")).click();

but it doesn't work.

Error message:

element not found exception
DebanjanB
  • 118,661
  • 30
  • 168
  • 217
klobin
  • 41
  • 1
  • 10
  • 1
    What does "it doesn't work" mean? Please update your question with the relevant error messages. – JeffC Jan 11 '19 at 22:21

2 Answers2

4

You probably want to use By.id instead: https://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/By.html#id-java.lang.String-

driver.findElement(By.id("attachment-trigger-ember1000")).click();
dtanabe
  • 1,421
  • 7
  • 17
  • Are you sure the element is on the page when that code is executed? It's a pretty common issue in Selenium/WebDriver that you are waiting for some JavaScript code to execute before the element makes its way on the page. You may need to wait: https://www.toolsqa.com/selenium-webdriver/wait-commands/ – dtanabe Jan 11 '19 at 23:26
2

As the desired element is a Ember.js enabled element the id attribute is dynamic and you have to induce WebDriverWait and you can use either of the following solutions:

  • cssSelector:

    WebElement myButton = new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("button.msg-form__footer-action.button-tertiary-medium-round-muted.m0[id^='attachment-trigger-ember']")));
    
  • xpath:

    WebElement myButton = new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//button[@class='msg-form__footer-action button-tertiary-medium-round-muted m0' and starts-with(@id, 'attachment-trigger-ember')]")));
    
DebanjanB
  • 118,661
  • 30
  • 168
  • 217
  • Why this is not working, WebElement attachment = new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//button[@class='msg-form__footer-action button-tertiary-medium-round-muted m0' and @id='attachment-trigger-ember3920']"))); – klobin Jan 14 '19 at 23:43
  • @klobin As I mentioned in my answer the **numeric** part i.e. **1000** within the **value** of the _id_ attribute is dynamic. So everytime you access the HTML, the dynamic part will get changed as **1000**, **3920**, etc. So we can't consider this part while constructing the [Locator Strategies](https://stackoverflow.com/questions/48369043/official-locator-strategies-for-the-webdriver/48376890#48376890) – DebanjanB Jan 15 '19 at 06:31